Skip to content

Instantly share code, notes, and snippets.

@Dvorson
Last active March 18, 2019 18:54
Show Gist options
  • Save Dvorson/ae73fbd09f7a7514e9b3951bc93f4eed to your computer and use it in GitHub Desktop.
Save Dvorson/ae73fbd09f7a7514e9b3951bc93f4eed to your computer and use it in GitHub Desktop.
#include <MQTT.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
#define PUB_INTERVAL 30000
unsigned long lastPublishMillis = 0;
Adafruit_BMP280 bme;
Adafruit_Si7021 si7021 = Adafruit_Si7021();
WiFiClient net;
MQTTClient client;
void setup() {
// Serial.begin(115200);
if (!bme.begin(D3, D4)) {
// Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
if (!si7021.begin(D3, D4)) {
// Serial.println("Did not find Si7021 sensor!");
while (true);
}
WiFi.begin("*", "*");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
// Serial.println(WiFi.localIP());
client.begin("192.168.1.125", net);
while (!client.connect("ESP8266")) {
// Serial.print(".");
delay(1000);
}
// Serial.println("MQTT Connected");
}
void loop() {
client.loop();
if (lastPublishMillis + PUB_INTERVAL < millis()) {
lastPublishMillis = millis();
client.publish("home/temperature1", "{ \"value\": " + String(bme.readTemperature()) + ", \"unit\": \"C\" }");
client.publish("home/temperature2", "{ \"value\": " + String(si7021.readTemperature(), 2) + ", \"unit\": \"C\" }");
client.publish("home/humidity2", "{ \"value\": " + String(si7021.readHumidity(), 2) + ", \"unit\": \"%\" }");
client.publish("home/pressure2", "{ \"value\": " + String(bme.readPressure() * 0.00750062) + ", \"unit\": \"mmhg\" }");
client.publish("home/altitude", "{ \"value\": " + String(bme.readAltitude(1013.25)) + ", \"unit\": \"m\" }");
// Serial.println("T1: " + String(bme.readTemperature()) + " T2: " + String(si7021.readTemperature(), 2) + " H: " + String(si7021.readHumidity(), 2) + " P: " + String(bme.readPressure() * 0.00750062) + " A: " + String(bme.readAltitude(1013.25)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment