Added documentation. Added keep-alive cheking to thermo_sensor

This commit is contained in:
ftc
2021-04-13 21:48:00 +03:00
parent 2d0a529ec3
commit c44ec14a9d
2 changed files with 65 additions and 1 deletions
+40 -1
View File
@@ -6,12 +6,15 @@
#define DHTPIN 5
#define DHTTYPE DHT22
#define READ_INTERVAL 1000
#define UDP_PORT 7000
#define PING_INTERVAL 60000 // one minute seems to be ok for us
WiFiUDP udp;
DHT dht(DHTPIN, DHTTYPE);
char id[64];
long last_ping;
void setup()
{
Serial.begin(115200);
@@ -34,6 +37,9 @@ void setup()
WiFi.macAddress(macAddr);
sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
Serial.printf("Connected, mac address: %s\n", id);
udp.begin(UDP_PORT);
last_ping = millis();
}
char payload[1024];
@@ -73,6 +79,28 @@ void ReadAndSend()
udp.endPacket();
}
char incoming_packet[4096];
void ParseIncomingPacket()
{
int len = udp.read(incoming_packet, sizeof(incoming_packet));
if (len == 0) return;
// find command
int pos = 0;
while (pos < len && incoming_packet[pos] != 0x01) pos++;
if (strncmp(incoming_packet, "ping", pos) == 0)
{
Serial.println("Ping command received");
}
else
{
// unknown command
Serial.println("Unknown command received");
}
}
unsigned long last_read = 0;
void loop()
@@ -83,4 +111,15 @@ void loop()
ReadAndSend();
last_read = current;
}
int packetSize = udp.parsePacket();
if (packetSize)
{
ParseIncomingPacket();
}
if (current < last_ping || current - last_ping >= PING_INTERVAL)
{
// no pings for long time, reboot
Serial.println("No pings for long time, reboot");
ESP.restart();
}
}