JMRI Signals, WiFi and MQTT
i) Demo first:
So you create a Mast in JMRI, with the UserName "mast.0001", run the jython script to attach the listeners, and when the signal mast changes aspects, the event is sent to the MQTT broker, from where every subscriber receive the message. If it is a NodeMCU on the WiFi, and connected to a real signal with LEDs or similar, boom, you got wireless signals!
The MQTT Broker, Router and JMRI could all be running/contained by a single Raspberry Pi 3, so $29 plus an SD Card and you have a signal system. All you need to provide the NodeMCUs is 5Vdc power though a micro USB cable. If you go the smaller route, like an ESP8266-01, then you would need to provide 3V power and something else to take the two GPIOs to the 3 LEDs. It is easy to use two pins to the joints of 3 LEDs in series, with power and ground on the ends, but you can't turn them all off...no Unlit state, and flashing is between two LEDs and not one LED flashing on and off...since, again, you can't have the 'off" state. If you high-Z the GPIO pins, the current will flow straight from Vcc through all 3 LEDs to ground. All now dimly lit.
But if you want to have a bi-polar bi-color red and green LED between the two pins, you can have a search light with dark and flashing, and yellow by switching between red and green faster than someone can say whistle-stop!
ii) The broker part: (also make sure you read all of these MQTT Essentials if you need to be an expert in MQTT)
So one of the first things smart people will tell you, the ESP8266 is not very reliable, or, it does not work on all WiFi routers...so I decided to go the cheap route and put 20 ESP01's to the test (all from Ebay) and also use the cheapest WiFi Access Point I could find...a $10 Raspberry Pi Zero W (and of course, you need a microSD card to, so $15 in total) and guess what, that Access Point can run the MQTT broker too, and JMRI, and check the weather, wait, off topic already!
sudo apt-get install mosquitto is all you need to do for the broker part.
You tell the Pi Zero W to become an Access Point as well as a DHCP server, with
And you give it (the Zero W) a static IP address, so your MQTT broker is at a known location, and you tell it to hand out DHCP addresses to all the "signals" connecting to it ... and we really don't care what addresses they get (as long as it is not the same as the broker). Your Pi Zero W could also be connected to the interweb with another USB to WiFi or LAN device, or maybe the same built-in WiFi device, and then you would do all the bridge-utils or iptables stuff to give all your "signals" access to the interweb as well.
So, back to the original topic, for 2 weeks now, I have had 3 signals connected to the Pi Zero W, and they still change aspects when my phone comes home and publish to their topics with the Android MQTT Dash application. So I guess the next step would be to connect the next 17 as well and move them to the train room to get some more of the 20 feet between them.
Since the whole set up will eventually unfold here, and it has a bit of work involved (you need to install Jython and the Python MQTT Paho library for it, and then point JMRI to use it instead of the Jython that comes with JMRI be default) I want to give a few pointers to tools that might help. First, on a computer somewhere in contact with the broker, or on the broker itself, you can use the mosquitto-clients tools to do some publishing and subscriber to ensure the broker is happy:
sudo apt-get install mosquitto mosquitto-clientsmosquitto_pub -h 192.168.16.152 -t mast.0002 -r -m "Approach" // -r for retain, -p 1883 mosquitto_sub -v -h 192.168.16.152 -t '#' // -p 1883, # subscribes to all messages
Then there are Android apps to act as publishers and subscribers, MQTT Dash my current favorite, and you can add "options" to publish a "Clear", "Approach" or "Stop" to a topic, and it is quite cool to do a
mosquitto_pub -h 192.168.16.152 -t mast.0002 -r -m "Clear"
and see mast.0002 change aspect and the item in the phone application updates too!!!
I would also encourage to you to plan the networking between your "signal" WiFi and home WiFi out a bit, since most of us don't want the rest of the world in our house and neither in our signal system, but as soon as you separate the "signal" WiFi into its own, your computer doing all the interweb and testing, has to choose which one it is connected to. So I have a USB WiFi dongle to connect Ubuntu to the signals and the LAN cable goes towards the cloud. And of course the subnets and masks are chosen appropriately to make sure the PC can access both. If you are using Windows, good luck.
Ok, I have to go solder a bunch of resistors to LEDs to make sure the UUTs are more than 3 over the long weekend!
Note about the DHCP installation above...I actually installed these apps on my Pi Zero W (JMRI and the separate Jython is not included yet):
sudo apt-get install emacs mosquitto x11vnc // favorite editor, broker and vnc for remote desktop if needed
sudo apt-get install hostapd isc-dhcp-server // access point and dhcp
sudo apt-get install mosquitto-clients // mosquitto_sub and _pub
sudo apt-get install iptables-persistent // in case you want to provide to bridge to the cloud
so, this might come in handy as your signals get their DHCP addresses, or not:
sudo service isc-dhcp-server status
May 23 05:26:08 pizerorrr dhcpd: DHCPREQUEST for 192.168.16.12 (192.168.16.152) from 38:..:f3:ff (mast.0002) via wlan0
May 23 05:26:08 pizerorrr dhcpd: DHCPACK on 192.168.16.12 to 38:..:f3:ff via wlan0
and pizerorrr is not and error, but the W's hostname: pi-zero-rrr and we can tell mast.0002 got .12 for today!