2019-02-21 23:03:40 +01:00
|
|
|
/* Macrocell environmental sensor project EnviSens
|
|
|
|
*
|
|
|
|
* This firmware provides features to use the NodeMCU ESP12 unit as an environmental sensor broker.
|
|
|
|
* It currently collects all the sensor information via I2C from a connected ATMEGA.
|
|
|
|
*
|
|
|
|
* (C) 2019 Macrocell - Environmental sensing solutions
|
|
|
|
* proudly presented by Macrocell - FPGA Innovators
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include "DHT.h"
|
2019-02-24 20:46:39 +01:00
|
|
|
#include "configuration.h"
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
#define TOPIC_BASE "sensor/" sensor_id "/"
|
|
|
|
#define TOPIC_TEMP TOPIC_BASE "temperature"
|
|
|
|
#define TOPIC_HMID TOPIC_BASE "humidity"
|
|
|
|
#define TOPIC_HEAT TOPIC_BASE "heat_index"
|
2019-02-28 21:27:49 +01:00
|
|
|
#define TOPIC_ABHU TOPIC_BASE "abs_humidity"
|
2019-03-01 10:02:34 +01:00
|
|
|
#define TOPIC_AIRQ TOPIC_BASE "air_gas"
|
2019-02-21 23:03:40 +01:00
|
|
|
|
2019-03-01 15:44:03 +01:00
|
|
|
#define DIFF_TEMP 0.5 // sensor has only 0.5°C accuracy, triggering on less is ludicrous
|
|
|
|
#define DIFF_HMID 2.5 // humidity doesn't change that much anyways
|
|
|
|
#define DIFF_HEAT 0.5 // this one's actually quite active
|
|
|
|
#define DIFF_ABHU 0.5 // small changes mean quite something
|
|
|
|
#define DIFF_AIRQ 5 // this one changes in integers
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
float hmid = 0; // humidity
|
|
|
|
float hmid_r = 0; // humidity
|
2019-02-28 21:27:49 +01:00
|
|
|
float abhu = 0; // absolute humidity
|
|
|
|
float abhu_r = 0; // absolute humidity
|
2019-02-21 23:03:40 +01:00
|
|
|
float temp = 0; // temperature
|
|
|
|
float temp_r = 0; // temperature
|
|
|
|
float heat = 0; // heat index
|
|
|
|
float heat_r = 0; // heat index
|
|
|
|
float airq = 0; // air quality / gas
|
|
|
|
float airq_r = 0; // air quality / gas
|
|
|
|
|
|
|
|
char data[8];
|
|
|
|
char sensor_name[5];
|
|
|
|
float sensor_value;
|
|
|
|
|
|
|
|
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);
|
|
|
|
Wire.begin(); // join i2c bus as master
|
|
|
|
}
|
|
|
|
|
|
|
|
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_password);
|
|
|
|
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_password)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:42:39 +01:00
|
|
|
bool checkVariation(float newValue, float prevValue, float maxDiff) {
|
2019-02-21 23:03:40 +01:00
|
|
|
return !isnan(newValue) &&
|
|
|
|
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
|
|
|
|
}
|
|
|
|
|
2019-02-28 20:42:39 +01:00
|
|
|
bool checkPlausibility(float newValue, float prevValue, float maxDiff) {
|
2019-03-01 15:44:22 +01:00
|
|
|
return (newValue < prevValue + (maxDiff * 10) || newValue > prevValue - (maxDiff * 10) || prevValue == 0.0);
|
2019-02-28 20:42:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 23:03:40 +01:00
|
|
|
long lastMsg = 0;
|
|
|
|
long lastSent = 0;
|
|
|
|
bool published = false;
|
|
|
|
bool forcePublish = false;
|
|
|
|
|
2019-02-28 21:10:04 +01:00
|
|
|
bool publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){
|
2019-03-01 09:17:30 +01:00
|
|
|
bool ret = false;
|
2019-03-01 09:33:30 +01:00
|
|
|
Serial.println("Sample " + desc + ": " + String(val).c_str() + " vs. last " + String(lastVal).c_str());
|
2019-02-28 21:10:04 +01:00
|
|
|
if (checkVariation(val, lastVal, diff) or force) {
|
2019-03-01 09:14:41 +01:00
|
|
|
Serial.println("Sufficient variation on " + desc + ": " + String(val).c_str());
|
2019-02-28 21:10:04 +01:00
|
|
|
if (checkPlausibility(val, lastVal, diff) or force) {
|
2019-03-01 09:33:30 +01:00
|
|
|
Serial.println("Value " + desc + " averaged and published : " + String(val).c_str());
|
2019-02-21 23:03:40 +01:00
|
|
|
client.publish(topic, String(val).c_str(), true);
|
2019-03-01 09:17:30 +01:00
|
|
|
ret = true;
|
2019-02-21 23:03:40 +01:00
|
|
|
} else {
|
2019-03-01 09:33:30 +01:00
|
|
|
Serial.println("** ERROR: Value " + desc + " out of bounds, not published. Last : " + String(lastVal).c_str());
|
2019-02-21 23:03:40 +01:00
|
|
|
}
|
2019-03-01 09:17:30 +01:00
|
|
|
lastVal = val;
|
2019-02-28 20:42:39 +01:00
|
|
|
}
|
2019-03-01 09:17:30 +01:00
|
|
|
return ret;
|
2019-02-21 23:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);}
|
|
|
|
|
|
|
|
Wire.requestFrom(8, 8); // request 6 bytes from slave device #8
|
|
|
|
int i = 0;
|
|
|
|
while(Wire.available()) { // slave may send less than requested
|
|
|
|
char c = Wire.read(); // receive a byte as character
|
|
|
|
data[i] = byte(c);
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
union sensor_tag {byte sensor_b[4]; float sensor_float;} sensor_union; //DRPS = Drum Revs per Second
|
|
|
|
sensor_name[0] = char(data[0]);
|
|
|
|
sensor_name[1] = char(data[1]);
|
|
|
|
sensor_name[2] = char(data[2]);
|
|
|
|
sensor_name[3] = char(data[3]);
|
2019-02-24 20:46:39 +01:00
|
|
|
sensor_name[4] = '\0';
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
sensor_union.sensor_b[0] = data[4];
|
|
|
|
sensor_union.sensor_b[1] = data[5];
|
|
|
|
sensor_union.sensor_b[2] = data[6];
|
|
|
|
sensor_union.sensor_b[3] = data[7];
|
|
|
|
sensor_value = sensor_union.sensor_float;
|
|
|
|
|
2019-02-24 20:46:39 +01:00
|
|
|
Serial.print("Received sensor reading: ");
|
2019-02-21 23:03:40 +01:00
|
|
|
Serial.print(sensor_name);
|
|
|
|
Serial.print(" - ");
|
|
|
|
Serial.println(sensor_value);
|
|
|
|
|
2019-02-24 20:46:39 +01:00
|
|
|
if (strcmp(sensor_name, "hmid") == 0){
|
|
|
|
hmid = sensor_value;
|
|
|
|
} else if (strcmp(sensor_name, "temp") == 0){
|
|
|
|
temp = sensor_value;
|
|
|
|
} else if (strcmp(sensor_name, "heat") == 0){
|
|
|
|
heat = sensor_value;
|
|
|
|
} else if (strcmp(sensor_name, "airq") == 0){
|
|
|
|
airq = sensor_value;
|
|
|
|
} else{
|
|
|
|
Serial.println("ERR: Sensor could not be identified");
|
|
|
|
}
|
|
|
|
|
2019-03-01 15:44:22 +01:00
|
|
|
if (!isnan(hmid) && !isnan(temp) && hmid != 0.0 && temp != 0.0){
|
2019-02-28 21:27:49 +01:00
|
|
|
abhu = (6.112 * ( pow(2.71828, ((17.67 * temp) / (temp + 243.5))) * hmid * 2.1674)) / (273.15 + temp);
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:03:40 +01:00
|
|
|
published = false;
|
|
|
|
if (lastSent > FORCE_PERIOD) {
|
|
|
|
Serial.println(F("Forcing a publish of all values"));
|
|
|
|
forcePublish = true;
|
|
|
|
lastSent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
published += publish("temperature", TOPIC_TEMP, temp, temp_r, DIFF_TEMP, forcePublish);
|
|
|
|
published += publish("humidity", TOPIC_HMID, hmid, hmid_r, DIFF_HMID, forcePublish);
|
|
|
|
published += publish("heat index", TOPIC_HEAT, heat, heat_r, DIFF_HEAT, forcePublish);
|
|
|
|
published += publish("air quality", TOPIC_AIRQ, airq, airq_r, DIFF_AIRQ, forcePublish);
|
2019-02-28 21:27:49 +01:00
|
|
|
published += publish("absolute humidity", TOPIC_ABHU, abhu, abhu_r, DIFF_ABHU, forcePublish);
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
if (published == false) {
|
|
|
|
lastSent = lastSent + 1;
|
|
|
|
} else {
|
|
|
|
lastSent = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
forcePublish = false;
|
|
|
|
}
|
|
|
|
}
|