ENC28J60 – Ethernet Module MQTT voorbeeld
Op deze pagina vind je een voorbeeld om via MQTT data te verzenden met een Arduino, ENC28J60 Ethernet module en BME280 sensor.
In onderstaande code wel het MQTT adres nog aanpassen:
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 |
/* 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 <UIPEthernet.h> #include "PubSubClient.h" #include <BME280I2C.h> #include <Wire.h> #define CLIENT_ID "UnoMQTT" #define INTERVAL 3000 // 3 sec delay between publishing uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; char msgBuffer[20]; char tempchar[10]; char humchar[10]; EthernetClient ethClient; PubSubClient mqttClient; BME280I2C bme; //Default : forced mode, standby time = 1000 ms Oversampling = pressure x1, temperature x1, humidity x1, filter off, long previousMillis; void setup() { // setup ethernet communication using DHCP Ethernet.begin(mac); // setup mqtt client mqttClient.setClient(ethClient); mqttClient.setServer("192.168.2.6",1883); //MQTT SERVER // start BME280 Wire.begin(); bme.begin(); previousMillis = millis(); } void loop() { // check interval if(millis() - previousMillis > INTERVAL) { sendData(); previousMillis = millis(); } mqttClient.loop(); } void sendData() { 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); // Converteer String to Array tempstring.toCharArray(tempchar, tempstring.length() + 1); humstring.toCharArray(humchar, humstring.length() + 1); if(mqttClient.connect(CLIENT_ID)) { mqttClient.publish("inblaastemperatuur", tempchar); mqttClient.publish("inblaasluchtvochtigheid", humchar); } } |
Resultaat in Node-red:
1 |
[{"id":"a7721f5c.10366","type":"mqtt in","z":"9c025a4c.21ac28","name":"","topic":"inblaastemperatuur","qos":"2","broker":"cfdab20e.ecec8","x":270,"y":280,"wires":[["5eab5a09.c39854"]]},{"id":"5eab5a09.c39854","type":"debug","z":"9c025a4c.21ac28","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":280,"wires":[]},{"id":"19e96d25.d95a43","type":"mqtt in","z":"9c025a4c.21ac28","name":"","topic":"inblaasluchtvochtigheid","qos":"2","broker":"cfdab20e.ecec8","x":280,"y":340,"wires":[["5eab5a09.c39854"]]},{"id":"cfdab20e.ecec8","type":"mqtt-broker","z":"","name":"Local MQTT","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}] |