Node-RED – Voorbeeld – BME280 data via MQTT visualiseren
Op deze pagina vind je een voorbeeld hoe je met Node-RED data dat binnenkomt met MQTT koppelt aan een meter/grafiek, in dit geval een BME280 sensor via NodeMCU (ESP8266)
Wat heb je nodig?
Stap 1
Op een NodeMCU (ESP8266) zit een BME280 sensor gekoppeld, gebruik onderstaand arduino script om de data uit te lezen en te versturen via MQTT.
Configureer:
- WiFi gegevens
- MQTT Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/* BME280 MQTT Script Connecting the BME280 Sensor: Sensor -> Board ----------------------------- Vin (Voltage In) -> 3.3V Gnd (Ground) -> Gnd SDA (Serial Data) -> D2 on NODEMCU / A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro SCK/SCL (Serial Clock) -> D1 on NodeMCU / A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro */ #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <BME280I2C.h> #include <Wire.h> // Update these with values suitable for your network. const char* ssid = "SSID"; const char* password = "PASS"; const char* mqtt_server = "raspberrypi.local"; BME280I2C bme; //Default : forced mode, standby time = 1000 ms Oversampling = pressure x1, temperature x1, humidity x1, filter off, WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; char tempchar[10]; char humchar[10]; char barchar[10]; void setup() { pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); Wire.begin(); bme.begin(); setup_wifi(); client.setServer(mqtt_server, 1883); } void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("ESP8266Client")) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); // Wait 5 seconds before retrying } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); float temp(NAN), hum(NAN), pres(NAN); BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); BME280::PresUnit presUnit(BME280::PresUnit_Pa); bme.read(pres, temp, hum, tempUnit, presUnit); // Converteer float naar string String tempstring = String(temp); String humstring = String(hum); String barstring = String(pres / 100); // Converteer String to Array tempstring.toCharArray(tempchar, tempstring.length() + 1); humstring.toCharArray(humchar, humstring.length() + 1); barstring.toCharArray(barchar, barstring.length() + 1); long now = millis(); if (now - lastMsg > 2000) { lastMsg = now; client.publish("bme280temp", tempchar); client.publish("bme280hum", humchar); client.publish("bme280bar", barchar); } } |
Stap 2
Voeg in Node-RED een MQTT ingang (subscribe) toe, en “abonneer” je op de topics, configureer de MQTT server als localhost (potloodje)
Op het dashboard kan je dan alle data zien:
Project in Node-RED clipboard:
1 |
[{"id":"5941e2c9.8924dc","type":"mqtt in","z":"9c025a4c.21ac28","name":"","topic":"bme280temp","qos":"2","broker":"cfdab20e.ecec8","x":470,"y":260,"wires":[["4564b128.49c78"]]},{"id":"4564b128.49c78","type":"ui_gauge","z":"9c025a4c.21ac28","name":"Temperatuur","group":"7b863557.a679dc","order":0,"width":0,"height":0,"gtype":"gage","title":"Temperatuur","label":"Graden C","format":"{{value}}","min":"5","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":650,"y":260,"wires":[]},{"id":"ded8ce36.4f162","type":"mqtt in","z":"9c025a4c.21ac28","name":"","topic":"bme280hum","qos":"2","broker":"cfdab20e.ecec8","x":470,"y":320,"wires":[["13dc274.c52bdd9"]]},{"id":"13dc274.c52bdd9","type":"ui_gauge","z":"9c025a4c.21ac28","name":"Luchtvochtigheid","group":"7b863557.a679dc","order":0,"width":0,"height":0,"gtype":"gage","title":"Luchtvochtigheid","label":"%","format":"{{value}}","min":"10","max":"95","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":670,"y":320,"wires":[]},{"id":"eb5cefc.c95b91","type":"mqtt in","z":"9c025a4c.21ac28","name":"","topic":"bme280bar","qos":"2","broker":"cfdab20e.ecec8","x":470,"y":380,"wires":[["83cd2ba5.0baca8"]]},{"id":"83cd2ba5.0baca8","type":"ui_gauge","z":"9c025a4c.21ac28","name":"Luchtdruk","group":"7b863557.a679dc","order":0,"width":0,"height":0,"gtype":"gage","title":"Luchtdruk","label":"hPa","format":"{{value}}","min":"960","max":"1100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":640,"y":380,"wires":[]},{"id":"cfdab20e.ecec8","type":"mqtt-broker","z":"","name":"Local MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"7b863557.a679dc","type":"ui_group","z":"","name":"BME280","tab":"317e4be0.76c9d4","order":1,"disp":true,"width":"6","collapse":false},{"id":"317e4be0.76c9d4","type":"ui_tab","z":"","name":"MQTT TAB","icon":"dashboard","order":2}] |