/*
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);
}
}