/* ESP8266 environmental sensor project * * This firmware provides features to use the NodeMCU ESP12 unit as an environmental sensor. * Currently supported is the DHT22 temperature and humidity sensor. * The sensor is to be connected to pin D2 of the NodeMCU ESP12 breakout card pin. * * (C) 2019 Macrocell - Environmental sensing solutions * proudly presented by Macrocell - FPGA Innovators */ #include #include #include #include "DHT.h" #include "configuration.h" #define TOPIC_BASE "sensor/" SENSOR_ID "/" #define TOPIC_TEMP TOPIC_BASE "temperature" #define TOPIC_HMID TOPIC_BASE "humidity" #define TOPIC_ABHU TOPIC_BASE "abs_humidity" #define TOPIC_HEAT TOPIC_BASE "heat_index" #define DIFF_TEMP 0.3 // sensor has only 0.5°C accuracy, triggering on less is ludicrous #define DIFF_HMID 1.5 // humidity doesn't change that much anyways #define DIFF_ABHU 0.2 // small changes mean quite something #define DIFF_HEAT 0.3 // this one's actually quite active struct Sensor { String desc; // sensor description char* topic; // sensor topic float diff; // value difference for an update float value; // current values float lastValue; // value at last update int lastUpdate; // number of samples since last update }; Sensor temp = {"temperature (°C)", TOPIC_TEMP, DIFF_TEMP, 0.0, 0.0, -1}; // temperature Sensor hmid = {"humidity (%)", TOPIC_HMID, DIFF_HMID, 0.0, 0.0, -1}; // humidity Sensor abhu = {"absolute humidity (gr/m³)", TOPIC_ABHU, DIFF_ABHU, 0.0, 0.0, -1}; // absolute humidity Sensor heat = {"heat index (°C)", TOPIC_HEAT, DIFF_HEAT, 0.0, 0.0, -1}; // heat index #define DHTPIN 4 // Digital pin connected to the DHT sensor #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 DHT dht(DHTPIN, DHTTYPE); WiFiClient espClient; PubSubClient client(espClient); void setup() { Serial.begin(SERIAL_BAUDRATE); pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); setup_wifi(); client.setServer(MQTT_SERVER, MQTT_PORT); dht.begin(); } void setup_wifi() { delay(10); // Connect to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PW); WiFi.mode(WIFI_STA); // blink LED fast until WiFi is connected while (WiFi.status() != WL_CONNECTED) { delay(200); digitalWrite(LED_BUILTIN, HIGH); Serial.print("."); delay(200); digitalWrite(LED_BUILTIN, LOW); } // WiFi connected Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(MQTT_CLIENT, MQTT_USER, MQTT_PW)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); digitalWrite(LED_BUILTIN, HIGH); // Wait 5 seconds before retrying delay(4000); digitalWrite(LED_BUILTIN, LOW); delay(1000); } } } bool checkVariation(float newValue, float prevValue, float maxDiff) { return !isnan(newValue) && (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); } bool checkPlausibility(float newValue, float prevValue, float maxDiff) { return (newValue < prevValue + (maxDiff * 10) || newValue > prevValue - (maxDiff * 10)); } long lastMsg = 0; void publish (Sensor &sensor){ bool force = false; Serial.println("Sample " + sensor.desc + ": " + String(sensor.value).c_str()); // check if first sample, set lastValue and return if (sensor.lastUpdate == -1){ sensor.lastValue = sensor.value; return; } // force an update if the number of samples without update is exceeded Serial.println(" * Forcing publication if sensor value is plausible"); force = sensor.lastUpdate >= FORCE_PERIOD; // update the sample counter, will be reset if sample is published sensor.lastUpdate += 1; // check if there was sufficient variation on the signal and if the value is plausible if (checkVariation(sensor.value, sensor.lastValue, sensor.diff) or force) { Serial.println(" Sufficient variation on " + sensor.desc + ": " + String(sensor.value).c_str()); if (checkPlausibility(sensor.value, sensor.lastValue, sensor.diff)) { Serial.println(" Value published"); client.publish(sensor.topic, String(sensor.value).c_str(), true); sensor.lastUpdate = 0; } else { Serial.print(" ** ERROR: Value out of bounds, not published. Last value :"); Serial.println(String(sensor.lastValue).c_str()); } sensor.lastValue = sensor.value; } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); digitalWrite(LED_BUILTIN, HIGH); long now = millis(); if (now - lastMsg > OPERATION_PERIOD / 2) { if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);} } if (now - lastMsg > OPERATION_PERIOD) { lastMsg = now; if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);} hmid.value = dht.readHumidity(); temp.value = dht.readTemperature(); if (!isnan(hmid.value) && !isnan(temp.value)){ abhu.value = (6.112 * ( pow(2.71828, ((17.67 * temp.value) / (temp.value + 243.5))) * hmid.value * 2.1674)) / (273.15 + temp.value); heat.value = dht.computeHeatIndex(temp.value, hmid.value, false); } publish(temp); publish(hmid); publish(heat); publish(abhu); } }