I want to send a broadcast message from my computer and then reply it from the ESP32
I'm managing to receive the broadcast message but I can't send the reply message (I don't see it in the computer's wireshark)
I used a popular version from the Internet so the code should be fine but I don't understand what's wrong.
I'm fairly new to Arduino so I don't really understand all aspects
this is the code I'm running.
It might be worth mentioning that I'm running the computer and the ESP32 wifi on my iPhone Hotspot.
#include "WiFi.h"
#define led 2
#include <Thread.h>
#include <WiFiUdp.h>
const char* ssid = "ssid";
const char* password = "password";
char packetBuffer[255];
WiFiUDP Udp;
WiFiServer wifiServer(10100);
Thread myThread = Thread();
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(115200);
myThread.enabled = true;
myThread.setInterval(10);
myThread.ThreadName = "myThread tag";
myThread.onRun(listen_broad);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
Serial.println(WiFi.localIP());
wifiServer.begin();
Udp.begin(10100);
}
void listen_broad(){
int packetSize = Udp.parsePacket();
if (packetSize){
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
int len = Udp.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
int i = 0;
char ReplyBuffer[] = "ACK";
while (ReplyBuffer[i] != 0)
Udp.write((uint8_t)ReplyBuffer[i++]);
Udp.endPacket();
}
}
void loop() {
if(myThread.shouldRun()){
myThread.run();
}
WiFiClient client = wifiServer.available();
if (client) {
client.write("nonono");
while (client.connected()) {
while (client.available()>0){
char c = client.read();
Serial.write(c);
switch (c) {
case '1':
//TOGGLE LIGHT//
if(digitalRead(led)){
Serial.write("Turning OFF");
digitalWrite(led, LOW);
}
else{
Serial.write("Turning ON");
digitalWrite(led, HIGH);
}
break;
/////////////////
case '2':
//CLOSE////////
client.stop();
break;
////////////////
default:
Serial.write("DEF");
break;
}
}
delay(10);
}
Serial.println("Client disconnected");
}
}