Created
July 8, 2020 22:29
-
-
Save JonasDoesThings/3a3de532ad891ae8ac39fd847b8c060a to your computer and use it in GitHub Desktop.
MQTT Air Sensor using an ESP8266 board with SSD1306 display, DHT22 temp/humdity sensor, SDS011 particulate matter sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Wire.h> | |
#include "SSD1306Wire.h" | |
#include <Adafruit_Sensor.h> | |
#include <DHT.h> | |
#include <SDS011.h> | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#define mqttUser "MQTT USER" | |
#define mqttPassword "MQTT PASSWORD" | |
#define mqttIP "MQTT IP" | |
#define mqttPort 1883 | |
#define wifiSSID "WIFI SSID" | |
#define wifiPassword "WIFI PASSWORD" | |
WiFiClient client; | |
PubSubClient mqttClient(client); | |
#define DISPLAY_SDA D1 | |
#define DISPLAY_SCL D2 | |
SSD1306Wire display(0x3C, DISPLAY_SDA, DISPLAY_SCL); | |
#define D22_DATA D7 | |
DHT dht22(D22_DATA, DHT22); | |
float temperature, humidity; | |
// These two values have to be dividable through 5 without remain | |
#define sdsSleepTime 5*60 | |
#define sdsMeasureTime 30 | |
#define SDS011_TX D5 | |
#define SDS011_RX D6 | |
SDS011 sds011Sensor; | |
float p10, p25; | |
int sds011Error; | |
int sdsCounter = 0; | |
void mqttConnect() { | |
Serial.println("Connecting to MQTT!"); | |
while(!mqttClient.connected()) { | |
String clientId = "airsensor"; | |
clientId += "-" + String(millis()); | |
if(mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) { | |
Serial.println("Connected to MQTT!"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(mqttClient.state()); | |
Serial.print("\n"); | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
display.init(); | |
display.setContrast(100); | |
display.setFont(ArialMT_Plain_10); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.drawString(7, 11, String("Starting up...")); | |
display.display(); | |
dht22.begin(); | |
sds011Sensor.begin(SDS011_TX, SDS011_RX); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(wifiSSID, wifiPassword); | |
Serial.println("Connecting to WiFi"); | |
while(WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(150); | |
} | |
Serial.println(); | |
Serial.print("Connected to WiFi! (IP="); | |
Serial.print(WiFi.localIP()); | |
Serial.println(");"); | |
display.clear(); | |
display.drawString(7, 11, String("Connected to WiFi!")); | |
display.drawString(7, 25, WiFi.localIP().toString()); | |
display.display(); | |
mqttClient.setServer(mqttIP, mqttPort); | |
Serial.print("Connecting to MQTT..."); | |
mqttConnect(); | |
while(!mqttClient.connected()) { | |
Serial.print("."); | |
} | |
Serial.println(); | |
delay(1000); | |
} | |
void loop() { | |
if(!WiFi.isConnected()) { | |
Serial.println("Lost WiFi Connection"); | |
WiFi.reconnect(); | |
} | |
if(!mqttClient.connected()) { | |
Serial.println("MQTTClient lost connection!"); | |
mqttConnect(); | |
delay(50); | |
} | |
mqttClient.loop(); | |
temperature = dht22.readTemperature(); | |
humidity = dht22.readHumidity(); | |
Serial.println(); | |
Serial.println("Humidity=" + String(humidity) + "&Temperature=" + String(temperature)); | |
char buf[8]; | |
dtostrf(temperature, 6, 2, buf); | |
mqttClient.publish("workspace/air/temperature", buf); | |
dtostrf(humidity, 6, 2, buf); | |
mqttClient.publish("workspace/air/humidity", buf); | |
if(sdsCounter == sdsSleepTime) { | |
Serial.println("Waking up SDS011 sensor..."); | |
sds011Sensor.wakeup(); | |
} else if(sdsCounter == (sdsSleepTime+sdsMeasureTime)) { | |
Serial.println("Reading out SDS011 sensor..."); | |
sds011Error = sds011Sensor.read(&p25, &p10); | |
if(!sds011Error) { | |
Serial.println("P2.5=" + String(p25) + "P10=" + String(p10)); | |
dtostrf(p25, 6, 2, buf); | |
mqttClient.publish("workspace/air/pm25", buf); | |
dtostrf(p10, 6, 2, buf); | |
mqttClient.publish("workspace/air/pm10", buf); | |
} else { | |
Serial.println("Error while reading SDS011!"); | |
} | |
Serial.println("Putting SDS011 sensor to sleep..."); | |
sds011Sensor.sleep(); | |
sdsCounter = 0; | |
} | |
display.clear(); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.drawString(7, 7, String("Temp: ") + String(temperature)); | |
display.drawString(7, 20, String("Humidity: ") + String(humidity)); | |
display.drawString(7, 33, String("PM10: ") + String(p10)); | |
display.drawString(7, 46, String("PM25: ") + String(p25)); | |
display.display(); | |
sdsCounter += 5; | |
delay(1000*5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment