as you may know, we introduced recently some commands / functions enabling to manage UDP traffic.
Basically, these commands permits to receive and/or transmit an UDP message to another ESP8266 or another device (PC, tablet, ...)
I'll try to put a working example to show how this can be done very easily.
I order to support this example, I developed a simple utility that can be found here : https://github.com/cicciocb/UDP_tester/raw/master/UDP_tester/bin/Debug/UDP_controller.exe
This is a picture of the utility
This utility permit to send and receive messages to/from a group of ESPs;
in my case I tested with 3 at the same time.
This is the program installed in the 1st ESP
memclear
udpbegin 5001
udpbranch [udp]
wait
end
[udp]
serialprintln "UDP received"
serialprintln udpremote() & " " & ramfree()
rec = udpread()
serialprintln rec
if left(rec, 12) <> "Who are you?" then goto [next_test]
udpreply "ID=ESP_LIGHT"
return
[next_test]
if left(rec,6) <> "LIGHT_" then goto [next_test2]
li = mid(rec,7)
serialprintln li
udpreply "MSG_RECEIVED"
return
[next_test2]
serialprintln "Message not valid!"
return
This is the program installed in the 2nd ESP
memclear
udpbegin 5001
udpbranch [udp]
wait
end
[udp]
serialprintln "UDP received"
serialprintln udpremote() & " " & ramfree()
rec = udpread()
serialprintln rec
if left(rec, 12) <> "Who are you?" then goto [next_test]
udpreply "ID=ESP_TEMPERATURE"
return
[next_test]
if left(rec,4) <> "TEMP" then goto [next_test2]
li = mid(rec,5)
serialprintln li
udpreply "TEMP=" & str(rnd(155))
return
[next_test2]
serialprintln "Message not valid!"
return
And this is the program installed in the 3rd ESP
print flashfree()
memclear
udpbegin 5001
udpbranch [udp]
wait
end
[udp]
serialprintln "UDP received"
serialprintln udpremote() & " " & ramfree()
rec = udpread()
serialprintln rec
if left(rec, 12) <> "Who are you?" then goto [next_test]
udpreply "ID=ESP_DISPLAY"
return
[next_test]
if left(rec,5) <> "PRINT" then goto [next_test2]
li = mid(rec,6)
serialprintln li
udpreply "MSG_PRINTED"
return
[next_test2]
serialprintln "Message not valid!"
return
As you can see into the basic code, all modules shares nearly the same code.
Each modules contains an ID, (ESP_LIGHT, ESP_TEMPERATURE, ESP_DISPLAY).
This correspond to a particular function dedicated at each module (light relay, temperature sensor, external display).
As soon as they receive the message "Who are you?", they answer back their ID.
This happens clicking on the button "Who is Connected"
What happens when clicking on the "Who is Connected?" button ?
A broadcast message is sent on the local IP (the PC IP with .255 at the end) on the port defined (5001).
All the modules are configured to receive messages on this port so all the ESPs receive it.
After that each module send back its ID and the utility understand that and fill the "ESP8266 list"
Now, we can send messages to the modules.
Each module has been "instructed" to understand a simple message :
- LIGHT_ON / LIGHT_OFF for the ESP_LIGHT
- TEMP for ESP_TEMPERATURE
- PRINT for ESP_DISPLAY
Clicking in the ESP8266 List permit to select the module we want communicate with.
For example we can click on the ESP_LIGHT and then select the demo message LIGHT_ON; Clicking on "Send" will send the message to this unique module; the module will send back a message "MSG_RECEIVED"
The same will be for the temperature module; in this case the message to select is TEMP and the message received will be the temperature (in fact a random number)
For the last module, same history, PRINT message.
Hoping this will permit to better understand how manage the ESP UDP communications
Regards
CiccioCB