Lab 10 - MQTT and Node-RED
- Software part
- Get a working Linux environment for your services (yes, we need full Linux for running IoTempower later; Node-RED and mosquitto you could run natively - though it's easier in Linux)
- If you use Windows install a Linux distro like Ubuntu in WSL 1
- If you user a Mac, try using brew (https://brew.sh/) or get a virtual machine (you might later have to do some work in testing how much works with IoTempower under brew)
- If you are using Linux, ignore this step (just say "I use Linux as main OS" in report)
- If you installed WSL 2 already then change it to WSL 1. From Windows admin-rights terminal:
wsl --terminate ubuntu
(Stop running Ubuntu WSL 2 instance)wsl --set-version Ubuntu 1
(Specify Ubuntu instance to use WSL v1)
- Setup the mqtt broker mosquitto, careful with its permissions, the defaults are changing all the time (try first without modifications).
- You might need the following in its configuration file /etc/mosquitto/mosquitto.conf (use nano or vim to edit):
listener 1883 allow_anonymous true
- (Only necessary if you have WSL2) : forward traffic to the WSL VM:
- To access the MQTT broker from a remote machine, we need to forward the host Windows traffic on port 1883 to the WSL VM, we can do this with
netsh interface portproxy add v4tov4 listenport=1883 listenaddress=0.0.0.0 connectport=1883 connectaddress=XX.XX.XX.XX
, where XX.XX.XX.XX is the IP of the WSL VM (e.g. run ip a from the VM orwsl hostname -I
from Windows) - Similarly, forward the Node-RED web dashboard port (1880)
- To access the MQTT broker from a remote machine, we need to forward the host Windows traffic on port 1883 to the WSL VM, we can do this with
- Setup nodejs and npm and Node-RED (and node-red-dashboard in there) - best start from Node-RED tutorial (https://nodered.org/docs/getting-started/raspberrypi or https://nodered.org/docs/getting-started/local - or on Arch Linux install directly from the AUR). You might have to run
node-red
in a terminal after installation manually (or not, check processes running withpgrep -aif node-red
). Careful here, the order seems to matter. So either do the script orsudo apt get install npm; npm install node-red
. Note from Kelian - the first script from the node-red raspberrypi link works the best ;) - Set-up mqtt sender and receiver (using debug and inject nodes on Node-RED or cli with
mosquitto_pub
andmosquitto_sub
)- Prove installation success by sending and receiving messages via mosquitto between at least two participants
- Create cases to use wildcards (+ and #) and show them in action
- Build two simulators having cli or gui (use your preferred language and respective mqtt library or even build something on Android or use Node-RED with gui)
- Sensor simulator: Temperature sensor simulator
- Input start temp, end temp, time to linearly rise → then run this simulation
- Relay simulator: Relay switch (like AC) simulator
- Showing on and off → can be switched to these states remotely
- Sensor simulator: Temperature sensor simulator
- Build an integrator component in Node-RED: switch at specific temperature (can be hardcoded) → needs to connect the upper two
- Get a working Linux environment for your services (yes, we need full Linux for running IoTempower later; Node-RED and mosquitto you could run natively - though it's easier in Linux)
- Hardware (or hybrid) part
- Create physical button device that uses mqtt and make it play a sound and show a notification in the Node-RED dashboard (test the dashboard both on the desktop and on a phone)
- Recommended Arduino MQTT client library: "PubSubClient"
- Figure out how to use given OLED I2C display and implement a mqtt text receiver (send a short message and show it on the screen) (It is a SSD1306. I used this library for the display, others might be simpler though - https://github.com/olikraus/u8g2 - I think this one would be easier: https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED)
- Create Node-RED dashboard to enter and send a specific message
- Make this message also show up on the display
- Get remote data from the DHT22 onewire temperature/humidity device and show on dashboard as graph as well as the remote OLED display (as value) (I used this library before, but you can try others and report your success: https://github.com/beegee-tokyo/DHTesp)
- Extend your button example to trigger from Node-RED a notification in an app on your phone (even if your phone is not logged into your local WiFi network). Use either Telegram, Discord, simplepush.io, hivemq + mqtt dashboard, or slack [or bonus point for a google api and use internal/firebase mechanism]
- Create physical button device that uses mqtt and make it play a sound and show a notification in the Node-RED dashboard (test the dashboard both on the desktop and on a phone)
- Storytime Develop a detailed story for the following sketch: John and Jane are interested in establishing a greenhouse for growing vegetables and plants. They want to have a deep insight into the health of their plants - by monitoring the plants’ environmental conditions and taking into account factors such as the weather, both realtime and prediction-based. Additionally, they like to travel often, so the routine activities such as watering of plants need to be automated. Further, J&J want to see the current status from anywhere in the world, so that in case something goes astray with the automation, they may ask their neighbor Bob to step in.
As usual, finish open tasks until next lab and document all steps in your shared portfolio (and link from both personal portfolios to those). Some of the work here can be distributed, but we still encourage you to work on the tasks in a pair in a extreme programming manner (and swapping roles about every 30 minutes) - compare https://en.wikipedia.org/wiki/Extreme_programming.