ESP32 MQTT – Publish and Subscribe with Arduino IDE
[{"id":"f0d37edca8bd827a","type":"ui_gauge","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":1,"width":"3","height":"2","gtype":"gage","title":"濕度","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"65","seg2":"75","className":"","x":370,"y":440,"wires":[]},{"id":"2858a2ae8ebe4d8f","type":"ui_button","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":0,"width":"2","height":"1","passthru":false,"label":"LED 開","tooltip":"","color":"white","bgcolor":"","className":"","icon":"fa-circle","payload":"on","payloadType":"str","topic":"","topicType":"str","x":140,"y":80,"wires":[["f20d6295bed43e34","4411678fdec67d84","9610c96e24aa9818"]]},{"id":"9e48ae5668dd1d79","type":"ui_button","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":0,"width":"2","height":"1","passthru":false,"label":"LED 關","tooltip":"","color":"black","bgcolor":"","className":"","icon":"fa-circle-o","payload":"off","payloadType":"str","topic":"","topicType":"str","x":140,"y":120,"wires":[["f20d6295bed43e34","4411678fdec67d84","9610c96e24aa9818"]]},{"id":"f20d6295bed43e34","type":"mqtt out","z":"17c01e65ea7d6deb","name":"Control LED","topic":"alex9ufo/esp32/led","qos":"1","retain":"true","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"70940176.2b2d3","x":390,"y":180,"wires":[]},{"id":"7451665529bc4d94","type":"ui_button","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":0,"width":"2","height":"1","passthru":false,"label":"LED 開關反向","tooltip":"","color":"blue","bgcolor":"","className":"","icon":"fa-circle-o","payload":"TOGGLE","payloadType":"str","topic":"","topicType":"str","x":160,"y":160,"wires":[["f20d6295bed43e34","4411678fdec67d84","9610c96e24aa9818"]]},{"id":"88929da799604451","type":"ui_button","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":0,"width":"2","height":"1","passthru":false,"label":"LED 開5秒鐘","tooltip":"","color":"purple","bgcolor":"","className":"","icon":"fa-circle-o","payload":"TIMER","payloadType":"str","topic":"","topicType":"str","x":150,"y":240,"wires":[["f20d6295bed43e34","4411678fdec67d84","9610c96e24aa9818"]]},{"id":"1848f7d50b01e54d","type":"comment","z":"17c01e65ea7d6deb","name":"Node-Red publish 到 HiveMQ Broker , ESP32訂閱","info":"將 alex9ufo/inTopic 到(publish)HiveMQ Broker \n給 Arduino 訂閱(Subscribe)","x":270,"y":280,"wires":[]},{"id":"9861fa8e41cac265","type":"ui_button","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":0,"width":"2","height":"1","passthru":false,"label":"LED 閃爍","tooltip":"","color":"yellow","bgcolor":"","className":"","icon":"fa-circle-o","payload":"FLASH","payloadType":"str","topic":"","topicType":"str","x":140,"y":200,"wires":[["f20d6295bed43e34","4411678fdec67d84","9610c96e24aa9818"]]},{"id":"4411678fdec67d84","type":"ui_audio","z":"17c01e65ea7d6deb","name":"","group":"6c9116b.b62d4e8","voice":"zh-TW","always":true,"x":400,"y":240,"wires":[]},{"id":"9610c96e24aa9818","type":"ui_text","z":"17c01e65ea7d6deb","group":"d783cb980c3979fc","order":0,"width":0,"height":0,"name":"","label":"Node-RED發行到MQTT的資料","format":"{{msg.payload}}","layout":"row-left","className":"","x":450,"y":120,"wires":[]},{"id":"b9d0a81d2ded2e3a","type":"ui_audio","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","voice":"en-US","always":"","x":380,"y":320,"wires":[]},{"id":"8d3c364c24da2ae4","type":"mqtt in","z":"17c01e65ea7d6deb","name":"Humidity","topic":"alex9ufo/esp32/humidity","qos":"2","datatype":"utf8","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":160,"y":460,"wires":[["f0d37edca8bd827a","b9d0a81d2ded2e3a","4136d882ef1c9ca1"]]},{"id":"c9459b13380fcebf","type":"mqtt in","z":"17c01e65ea7d6deb","name":"Temperature","topic":"alex9ufo/esp32/temperature","qos":"2","datatype":"utf8","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":170,"y":380,"wires":[["1c1b88400941a7c1","b9d0a81d2ded2e3a","54c0f79e889b0aac"]]},{"id":"1c1b88400941a7c1","type":"ui_gauge","z":"17c01e65ea7d6deb","name":"","group":"d783cb980c3979fc","order":1,"width":"3","height":"2","gtype":"gage","title":"溫度","label":"℃","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"26","seg2":"50","className":"","x":370,"y":360,"wires":[]},{"id":"61b5589a2c26df1d","type":"mqtt in","z":"17c01e65ea7d6deb","name":"LED Status","topic":"alex9ufo/esp32/led","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":150,"y":40,"wires":[["050db70fc3c0868a"]]},{"id":"050db70fc3c0868a","type":"ui_text","z":"17c01e65ea7d6deb","group":"d783cb980c3979fc","order":0,"width":0,"height":0,"name":"","label":"ESP32發行到MQTT的LED狀態","format":"{{msg.payload}}","layout":"row-left","className":"","x":450,"y":40,"wires":[]},{"id":"54c0f79e889b0aac","type":"debug","z":"17c01e65ea7d6deb","name":"debug 91","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":380,"y":400,"wires":[]},{"id":"4136d882ef1c9ca1","type":"debug","z":"17c01e65ea7d6deb","name":"debug 92","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":380,"y":480,"wires":[]},{"id":"d783cb980c3979fc","type":"ui_group","name":"Demo","tab":"0449b3b1fea99d52","order":1,"disp":true,"width":"6","collapse":false},{"id":"70940176.2b2d3","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6c9116b.b62d4e8","type":"ui_group","name":"Storing IOT Data ","tab":"eeb8e179.a47a4","order":1,"disp":true,"width":"6","collapse":true},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"0449b3b1fea99d52","type":"ui_tab","name":"Demo","icon":"dashboard","disabled":false,"hidden":false},{"id":"eeb8e179.a47a4","type":"ui_tab","name":"eWeLink","icon":"dashboard","disabled":false,"hidden":false}]
Installing the PubSubClient Library
The PubSubClient library provides a client for doing simple publish/subscribe messaging with a server that supports MQTT (basically allows your ESP32 to talk with Node-RED).
- Click here to download the PubSubClient library. You should have a .zip folder in your Downloads folder
- Unzip the .zip folder and you should get pubsubclient-master folder
- Rename your folder from
pubsubclient-masterto pubsubclient - Move the pubsubclient folder to your Arduino IDE installation libraries folder
- Then, re-open your Arduino IDE
The library comes with a number of example sketches. See File >Examples > PubSubClient within the Arduino IDE software.
Important: PubSubClient is not fully compatible with the ESP32, but the example provided in this tutorial is working very reliably during our tests.
沒有留言:
張貼留言