notes:mqtt
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| notes:mqtt [2024/04/18 20:17] – discord | notes:mqtt [2024/04/20 20:46] (current) – discord | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Written by Jacob Haip on April 17, 2024 | + | ====== MQTT and Microcontroller Folk Integrations ====== |
| + | Jacob Haip, April 2024 | ||
| - | I am interested in integrating more physical hardware and sensors into Folk Computer to help expand | + | I am interested in integrating more physical hardware and sensors into Folk Computer to expand |
| WiFi-connected microcontrollers are the primary tool I have been using to integrate sensors and actuators with Folk. Boards I have used include the [[https:// | WiFi-connected microcontrollers are the primary tool I have been using to integrate sensors and actuators with Folk. Boards I have used include the [[https:// | ||
| Line 72: | Line 73: | ||
| </ | </ | ||
| - | === Getting a button input into Folk === | + | ===== Getting a button input into Folk ===== |
| - | TODO: GIF of button press. | + | {{micro-button-press.gif}} |
| <code c rpi-pico-w-micropython-button-press.py> | <code c rpi-pico-w-micropython-button-press.py> | ||
| Line 174: | Line 175: | ||
| </ | </ | ||
| - | === Output from Folk to Microcontroller === | + | ===== Output from Folk to Microcontroller |
| I have this cool [[https:// | I have this cool [[https:// | ||
| Line 349: | Line 350: | ||
| {{matrixportal-live-edit.gif}} | {{matrixportal-live-edit.gif}} | ||
| + | |||
| + | ===== Using MQTT on web pages for bidirectional communication with Folk ===== | ||
| + | |||
| + | Making simple web pages using the [[https:// | ||
| + | |||
| + | First we make a web app that plays a sound when it receives an MQTT messages: | ||
| + | |||
| + | <code html play-sound.html> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <audio id=" | ||
| + | <source src=" | ||
| + | <source src=" | ||
| + | Your browser does not support the audio element. | ||
| + | </ | ||
| + | <script src=" | ||
| + | < | ||
| + | const clientId = ' | ||
| + | const host = ' | ||
| + | const options = { | ||
| + | keepalive: 60, | ||
| + | clientId: clientId, | ||
| + | protocolId: ' | ||
| + | protocolVersion: | ||
| + | clean: true, | ||
| + | reconnectPeriod: | ||
| + | connectTimeout: | ||
| + | will: { | ||
| + | topic: ' | ||
| + | payload: ' | ||
| + | qos: 0, | ||
| + | retain: false | ||
| + | }, | ||
| + | } | ||
| + | console.log(' | ||
| + | const client = mqtt.connect(host, | ||
| + | |||
| + | client.on(' | ||
| + | console.log(' | ||
| + | client.end() | ||
| + | }) | ||
| + | |||
| + | client.on(' | ||
| + | console.log(' | ||
| + | }) | ||
| + | |||
| + | client.on(' | ||
| + | console.log(' | ||
| + | client.subscribe('/ | ||
| + | }); | ||
| + | |||
| + | client.on(' | ||
| + | console.log(' | ||
| + | document.getElementById(" | ||
| + | document.getElementById(" | ||
| + | }); | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | And then in folk you can make every phone and laptop with that web page open play a sound when a button is pressed: | ||
| + | |||
| + | {{button-horse.mp4}} | ||
| + | |||
| + | <code tcl trigger-sound.folk> | ||
| + | When MQTT claims message /message/ topic "/ | ||
| + | Wish $this is labelled " | ||
| + | set ts [clock milliseconds] | ||
| + | Wish MQTT publish "make some noise" on topic "/ | ||
| + | } | ||
| + | </ | ||
notes/mqtt.1713471424.txt.gz · Last modified: by discord
