-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi_esp32.cpp
77 lines (65 loc) · 1.91 KB
/
wifi_esp32.cpp
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
#include <WiFi.h>
#include <HTTPClient.h>
#include <string.h>
using namespace std;
const int COMM = 23;
const char* ssid = "wifi_name";
const char* password = "wifi_password";
String serverName = "server_url";
unsigned long lastTime = 0;
unsigned long timerDelay = 1000;
int isOn = 0;
void setup() {
Serial.begin(115200);
pinMode(COMM, INPUT);
while (!Serial);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
bool send = 0;
void loop() {
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName;
http.begin(serverPath.c_str());
http.addHeader("Content-Type", "application/json");
std::string body = "{\"rssi_value\": " + to_string(isOn) + "}";
// string body = "{\"rssi_value\": ";
// body += "\"Something\"";
// body += "}";
int httpResponseCode = http.POST(body.c_str());
// int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
if (digitalRead(COMM) == HIGH) {
Serial.println("Baby in");
isOn = 1;
} else {
isOn = 0;
Serial.println("Baby out");
}
}
}