Comment 8 for bug 902916

Revision history for this message
greypanda (3-joh8-x) wrote :

Yes, it is almost a year later, but for those who may be interested, I am building a HA system using Arduinos with an ethernet connection. Rather than try to discover clients, I think the easiest way to establish connections is for the clients to advertise themselves. Well, that is fine if you want to embed the broker IP in the code, but I like to install the clients and never touch them again. (Well, for a long, long time.) So I added a little Python beacon that broadcasts the broker address on a UDP port.
Then, the client just listens for the broadcast and gets the path to the broker. This makes the Arduino code small since there is no DNS lookups or other exotic machinations. If the broker moves, the Arduino connection fails and it trys again, this time getting the IP of the new broker.

from socket import *
import time

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while True:
        s.sendto('mqtt:192.168.1.81.1883', ('255.255.255.255', 41303))
        time.sleep(10)