Last active
October 9, 2022 20:36
-
-
Save PetzJohannes/07a549724d1889e0cac14630dc030b79 to your computer and use it in GitHub Desktop.
ESP32 Luftqualität
This file contains 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
ithub | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include "ArduinoJson.h" | |
#include <Wire.h> | |
#include <SPI.h> | |
#include <Adafruit_Sensor.h> | |
#include "Adafruit_BME680.h" | |
#include <esp_wifi.h> | |
// wireless | |
const char* ssid = ""; | |
const char* pass = ""; | |
WiFiClient espClient; | |
IPAddress ip; | |
// mqtt | |
PubSubClient client(espClient); | |
const char* mqtt_server = ""; | |
const int mqtt_port = 1883; | |
const char* mqtt_user = ""; | |
const char* mqtt_password = ""; | |
// loop delay | |
int loopDelay = 10000; | |
// sensor setup MH-Z19 | |
int DataPin = 23; | |
int ppmrange = 5000; | |
unsigned long pwmtime; | |
// sensor setup BME680 | |
#define SEALEVELPRESSURE_HPA (1013.25) | |
Adafruit_BME680 bme; | |
// living ttyUSB0 | |
uint8_t newMACAddress[] = {0xC8, 0xC9, 0xA3, 0xD2, 0x0C, 0x34}; | |
const char* topic = "sensors/air-quality/living"; | |
// bedroom ttyUSB1 | |
//uint8_t newMACAddress[] = {0xC8, 0xC9, 0xA3, 0xD1, 0xF6, 0xD8}; | |
//const char* topic = "sensors/air-quality/bedroom"; | |
void initWifi() { | |
esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]); | |
WiFi.mode(WIFI_STA); | |
// wifi connect | |
WiFi.begin(ssid, pass); | |
Serial.print("Connecting to WiFi .."); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(1000); | |
} | |
Serial.println("WiFi connected."); | |
Serial.println(WiFi.localIP()); | |
} | |
void mqttConnect() { | |
if (client.connect("thing" , mqtt_user, mqtt_password)) { | |
Serial.println("MQTT connected"); | |
} | |
} | |
void mqttDisconnect() { | |
client.disconnect(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.print("ESP Board MAC Address: "); | |
Serial.println(WiFi.macAddress()); | |
// wifi connect | |
initWifi(); | |
// mqtt client init | |
client.setServer(mqtt_server, mqtt_port); | |
// sensor MH-Z19 setup | |
pinMode(DataPin, INPUT); | |
// sensor BME680 setup | |
if (!bme.begin()) { | |
Serial.println(F("Could not find a valid BME680 sensor, check wiring!")); | |
while (1); | |
} | |
// Set up oversampling and filter initialization | |
bme.setTemperatureOversampling(BME680_OS_8X); | |
bme.setHumidityOversampling(BME680_OS_2X); | |
bme.setPressureOversampling(BME680_OS_4X); | |
bme.setIIRFilterSize(BME680_FILTER_SIZE_3); | |
bme.setGasHeater(320, 150); // 320*C for 150 ms | |
} | |
int getCo2Ppm() { | |
pwmtime = pulseIn(DataPin, HIGH, 2000000) / 1000; | |
float pulsepercent = pwmtime / 1004.0; | |
int ppm = ppmrange * pulsepercent; | |
//char ppmChar[10]; | |
//dtostrf(ppm, 0, 0, ppmChar); | |
return ppm; | |
} | |
void loop() { | |
// Tell BME680 to begin measurement. | |
unsigned long endTime = bme.beginReading(); | |
if (endTime == 0) { | |
Serial.println(F("Failed to begin reading :(")); | |
return; | |
} | |
int ppm = getCo2Ppm(); | |
delay(50); // This represents parallel work. | |
// Obtain measurement results from BME680. Note that this operation isn't | |
// instantaneous even if milli() >= endTime due to I2C/SPI latency. | |
if (!bme.endReading()) { | |
Serial.println(F("Failed to complete reading :(")); | |
return; | |
} | |
StaticJsonDocument<400> dataTransfer; | |
dataTransfer["sensor"] = "air-quality"; | |
dataTransfer["co2"] = ppm; | |
dataTransfer["temperature"] = bme.temperature; | |
dataTransfer["pressure"] = bme.pressure / 100.0; | |
dataTransfer["humidity"] = bme.humidity; | |
dataTransfer["gas_resistance"] = bme.gas_resistance / 1000.0; | |
char jsonBuffer[400]; | |
serializeJson(dataTransfer, jsonBuffer); | |
serializeJson(dataTransfer, Serial); | |
Serial.println(jsonBuffer); | |
if ((WiFi.status() != WL_CONNECTED)) { | |
Serial.println("Reconnecting to WiFi..."); | |
WiFi.disconnect(); | |
WiFi.reconnect(); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(1000); | |
} | |
Serial.println("WiFi reconnected."); | |
} | |
// publish message | |
mqttConnect(); | |
client.publish(topic, jsonBuffer); | |
mqttDisconnect(); | |
delay(loopDelay); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment