ESP32 MQTT – Publish BMP280 Sensor Readings & send message to Line Bot
源自於https://randomnerdtutorials.com/esp32-mqtt-publish-bme280-arduino/
安裝PubSubClient Library
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.
Node-Red程式
[{"id":"b30645bac3aa1085","type":"mqtt in","z":"51a158a3aefe25e1","name":"","topic":"alex9ufo/esp32/bmp280/temperature","qos":"1","datatype":"auto","broker":"2db287b.addf978","nl":false,"rap":false,"x":340,"y":100,"wires":[["20912703a013bcb1","bfa3c15020ae16c1","118415c187405aee"]]},{"id":"20912703a013bcb1","type":"ui_gauge","z":"51a158a3aefe25e1","name":"","group":"8ea9cc0540356bef","order":2,"width":0,"height":0,"gtype":"gage","title":"攝氏溫度","label":"ºC","format":"{{value}}ºC ","min":0,"max":"60","colors":["#00b500","#f7df09","#ca3838"],"seg1":"","seg2":"","className":"","x":600,"y":100,"wires":[]},{"id":"546055255ca8ac0a","type":"mqtt in","z":"51a158a3aefe25e1","name":"","topic":"alex9ufo/esp32/bmp280/pressure","qos":"1","datatype":"auto","broker":"2db287b.addf978","nl":false,"rap":false,"x":330,"y":160,"wires":[["b631824e122f37dd"]]},{"id":"b631824e122f37dd","type":"ui_gauge","z":"51a158a3aefe25e1","name":"","group":"8ea9cc0540356bef","order":2,"width":0,"height":0,"gtype":"gage","title":"大氣壓力百帕(hPa)","label":"%","format":"{{value}} hPa","min":"30","max":"2000","colors":["#53a4e6","#1d78a9","#4e38c9"],"seg1":"","seg2":"","className":"","x":630,"y":160,"wires":[]},{"id":"db175d90cc7c5fc4","type":"mqtt in","z":"51a158a3aefe25e1","name":"","topic":"alex9ufo/esp32/bmp280/altitude","qos":"1","datatype":"auto","broker":"2db287b.addf978","nl":false,"rap":false,"x":330,"y":220,"wires":[["9e2d12a6e22edd52"]]},{"id":"9e2d12a6e22edd52","type":"ui_gauge","z":"51a158a3aefe25e1","name":"","group":"8ea9cc0540356bef","order":3,"width":0,"height":0,"gtype":"gage","title":"大概海拔高度","label":"units","format":"{{value}} 米","min":"-500","max":"1000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":620,"y":220,"wires":[]},{"id":"edaaedcc778611a5","type":"comment","z":"51a158a3aefe25e1","name":"[BMP280 大氣壓強度(高度計)感測模組 ]","info":"[BMP280 大氣壓強度(高度計)感測模組 ]","x":350,"y":40,"wires":[]},{"id":"bfa3c15020ae16c1","type":"function","z":"51a158a3aefe25e1","name":"傳送信息","func":"//CHANNEL_ACCESS_TOKEN = 'Messaging API Token';\nCHANNEL_ACCESS_TOKEN = 'UwdpPeFsI2wuLNy+2pMf+KKl64ATWBaqmJCHs8u3yHWl28YfeN2B3Ghklw2zeJ7FS8oD8J4DOoB5ex1bVwEU8XDyFQ1REpYqOxpKmOJ9ZRn4fiEeki9QTtiUtJ0fV9aW4w+ixGoOIvKh+/QWXba5dmQdB04t89/1O/w1cDnyilFU=';\nUSER_ID = 'U1bc09946a1ebc28d0fba29ec62d84a4d8'; //'使用者ID(不是Line ID)';\nmessage = {\n type:'text',\n text:'目前BMP280溫度:'+msg.payload\n};\nheaders = {\n 'Content-Type': 'application/json; charset=UTF-8',\n 'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN,\n};\npayload = {\n 'to': USER_ID,\n 'messages': [message]\n};\nmsg.headers = headers;\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":300,"wires":[["69d9661826a23fcb","e299f6edcc14cd03"]]},{"id":"69d9661826a23fcb","type":"http request","z":"51a158a3aefe25e1","name":"Messaging API 傳送","method":"POST","ret":"txt","paytoqs":false,"url":"https://api.line.me/v2/bot/message/push","tls":"","persist":false,"proxy":"","authType":"","x":800,"y":300,"wires":[["4b32a6f0620bd078"]]},{"id":"4b32a6f0620bd078","type":"debug","z":"51a158a3aefe25e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":300,"wires":[]},{"id":"702760b3f5dd3be6","type":"comment","z":"51a158a3aefe25e1","name":"developers.line.biz","info":"https://developers.line.biz/zh-hant/\n\n\nNews - LINE Developershttps://developers.line.biz › zh-hant\nThe LINE Developers site is a portal site for developers. It contains documents and tools that will help you use our various developer products.\n產品 · LINE Login · Messaging API · 文件\n\nhttps://developers.line.biz/console/channel/1656701433/basics\n\nhttps://developers.line.biz/console/channel/1656701433/messaging-api","x":590,"y":360,"wires":[]},{"id":"118415c187405aee","type":"debug","z":"51a158a3aefe25e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":60,"wires":[]},{"id":"e299f6edcc14cd03","type":"debug","z":"51a158a3aefe25e1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":820,"y":240,"wires":[]},{"id":"2db287b.addf978","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"/vm/mqtt/birth","birthQos":"0","birthPayload":"birth","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"8ea9cc0540356bef","type":"ui_group","name":"BMP280","tab":"53b8c8f9.cfbe48","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"53b8c8f9.cfbe48","type":"ui_tab","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]
沒有留言:
張貼留言