This repository was archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplantyESP32.ino
More file actions
52 lines (45 loc) · 1.46 KB
/
plantyESP32.ino
File metadata and controls
52 lines (45 loc) · 1.46 KB
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
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "Wifi_SSID";
const char* password = "Wifi_password";
const char* serverUrl = "http://localhost:30566/post";
int offset = 0;
int pin = 32;
int id = 123;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
AsyncWebServer server(80);
server.on("/offset", HTTP_GET, [](AsyncWebServerRequest *request){
String offsetString = request->getParam("offset")->value();
if (offsetString.toInt()) {
offset = offsetString.toInt();
request->send(200, "text/plain", "Offset saved successfully");
} else {
request->send(400, "text/plain", "Invalid offset value, must be an integer");
}
});
server.begin();
Serial.println("Server started");
}
void loop() {
int value = analogRead(pin) + offset;
String body = "value=" + String(value) + "&id=" + String(id);
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(body);
if (httpCode == 200) {
Serial.println("Value sent successfully, entering deep sleep for 4 hours...");
ESP.deepSleep(4*60*60*1000000); // 4 hours in microseconds
} else {
Serial.println("Error sending value, HTTP code: " + String(httpCode));
delay(300000); // retry after 5 minutes
}
http.end();
}