2020-11-11, 07:05 PM
Hi all
I got it working. I'll make a guide within the next week explaining this in more detail. Here is my final code:
Many thanks for the help guys
Peter
I got it working. I'll make a guide within the next week explaining this in more detail. Here is my final code:
Code:
#include "WiFi.h"
#include "AsyncUDP.h"
const char * ssid = "INSERT YOUR SSID HERE";
const char * password = "INSERT YOUR WIFI PASSWORD HERE";
AsyncUDP udp;
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while(1) {
delay(1000);
}
}
if(udp.connect(IPAddress(10,10,10,1), 3333)) {
Serial.println("UDP connected");
udp.onPacket([](AsyncUDPPacket packet) {
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP());
Serial.print(":");
Serial.print(packet.remotePort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
Serial.print(", Data: ");
Serial.write(packet.data(), packet.length());
Serial.println();
//reply to the client
packet.printf("Got %u bytes of data", packet.length());
});
//Send unicast
udp.print("Hello Server!");
}
}
void loop()
{
delay(1000);
long temp = (millis()/1000);
char udpmessage[1024];
sprintf(udpmessage, "{\"updates\":[{\"$source\":\"esp32.outside.temp\",\"values\":[{\"path\":\"environment.outside.temperature\",\"value\":%d}]}]}", temp);
udp.print(udpmessage);
Serial.println(udpmessage);
}
Many thanks for the help guys
Peter