Post
by dahult » Sat May 16, 2020 10:24 pm
This is not a direct answer to Chaopi's question but additional info and a related request for help. The SO_BROADCAST option has not been in the socket library since at least 1.9.4. Even though most socket examples show setting the socket option for broadcasts, I have been able to send and receive broadcast packets since uPython version 1.9.4 on ESP8266's and ESP32's using the following code:
Sender:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.sendto(msg, ('10.0.49.255', 1926))
Recvr:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('10.0.49.255', 1926))
while True:
msg, addr = s.recvfrom(1024)
print(msg, addr)
[NOTE: The address ('10.0.49.255', 1926) are the broadcast IP address and PORT I am using for my network. Your values must be
changed to meet your network. The broadcast IP address is set up by your network configuration but the PORT can be any number
(typically > 1024) just needs to be the same for all senders and receiver(s).
I have been using the above code for well over a year without problem until I re-flashed the receiver with version 1.12. The sender code continues to work fine and works on ESP8266's and ESP32's with 1.12 but the receiver code has stopped working entirely. The s.recvfrom() sits in a wait-state and never returns anything. I know the broadcast packets are being sent out because the same receiver code does work on a Raspberry Pi running Debian/Buster (even without the s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)).
So my question to add to Chaopi's is why did my broadcast receiver stop working? I did try re-flashing the receiver back to 1.9.4 (esp8266-20180511-v1.9.4.bin) but the receiver still sits waiting on s.recvfrom().
Any ideas/help is appreciated.
Doug