-
Notifications
You must be signed in to change notification settings - Fork 0
/
smart_config_rf_over_ethernet.ino
107 lines (93 loc) · 2.41 KB
/
smart_config_rf_over_ethernet.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <RH_ASK.h>
#include <SPI.h > // Not actualy used but needed to compile
//RH_ASK driver;
RH_ASK driver(2000, 0);
// RH_ASK driver(2000, 2, 4, 5); // ESP8266 or ESP32: do not use pin 11
//Set-up connects nodemcu to mobile-wifi
void setup()
{
Serial.begin(115200);
/*Set ESP8266 to WiFi Station mode */
WiFi.mode(WIFI_STA);
/*start SmartConfig */
WiFi.beginSmartConfig();
/*Wait for SmartConfig packet from mobile */
Serial.println("Waiting for SmartConfig.");
while (!WiFi.smartConfigDone())
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig done.");
/*Wait for WiFi to connect to AP */
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
if (!driver.init())
{
Serial.println("init failed");
}
}
void loop()
{
delay(1000);
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
char buf_char[10] = "\0";
int i = 0;
//Serial.print("buf is");
//Recieving data via rf reciever and converting it to a string
if (driver.recv(buf, &buflen))
{
driver.printBuffer("Got:", buf, buflen);
for (i = 0; i < buflen; i++)
{
buf_char[i] = (char) buf[i];
// buf++;
}
Serial.println("recieved data is ");
//Serial.println((char)*buf);
//Serial.println("buflen");
//Serial.print(buflen);
// Serial.print("buf_char is ");
Serial.print(buf_char);
//sending the data over to the server
HTTPClient http;
String str1 = "{\"product_id\":\"100\",\"level\":\"";
String data2 = (String) buf_char;
String name2 = "\"}";
String name3 = str1 + data2 + name2;
String post_var = name3;
Serial.print("[HTTP] begin...\n");
http.begin("54.70.29.64", 9000, "/postLevel"); //HTTP
http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] Post...\n");
int httpCode = http.POST(post_var);
if (httpCode)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == 200)
{
String payload = http.getString();
Serial.println(payload);
}
}
else
{
Serial.print("[HTTP] GET... failed, no connection or no HTTP server\n");
}
// Serial.println("buflen");
}
}