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>
|
|
|
|
|
2019-03-08 22:33:50 +01:00
|
|
|
#include "configuration.h"
|
|
|
|
#include "sensing.h"
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
char data[8];
|
|
|
|
char sensor_name[5];
|
|
|
|
float sensor_value;
|
2019-03-08 22:33:50 +01:00
|
|
|
long lastMsg = 0;
|
2019-02-21 23:03:40 +01:00
|
|
|
|
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient client(espClient);
|
|
|
|
|
2019-03-08 22:33:50 +01:00
|
|
|
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
|
|
|
|
Sensor airq = {"air quality (ppm)", TOPIC_AIRQ, DIFF_AIRQ, 0.0, 0.0, -1}; // air quality
|
|
|
|
|
2019-02-21 23:03:40 +01:00
|
|
|
void setup() {
|
|
|
|
Serial.begin(SERIAL_BAUDRATE);
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
setup_wifi();
|
2019-03-08 22:33:50 +01:00
|
|
|
client.setServer(MQTT_SERVER, MQTT_PORT);
|
2019-02-21 23:03:40 +01:00
|
|
|
Wire.begin(); // join i2c bus as master
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
if (!client.connected()) {
|
2019-03-08 22:33:50 +01:00
|
|
|
reconnect(client);
|
2019-02-21 23:03:40 +01:00
|
|
|
}
|
|
|
|
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){
|
2019-03-08 22:33:50 +01:00
|
|
|
hmid.value = sensor_value;
|
2019-02-24 20:46:39 +01:00
|
|
|
} else if (strcmp(sensor_name, "temp") == 0){
|
2019-03-08 22:33:50 +01:00
|
|
|
temp.value = sensor_value;
|
2019-02-24 20:46:39 +01:00
|
|
|
} else if (strcmp(sensor_name, "heat") == 0){
|
2019-03-08 22:33:50 +01:00
|
|
|
heat.value = sensor_value;
|
2019-02-24 20:46:39 +01:00
|
|
|
} else if (strcmp(sensor_name, "airq") == 0){
|
2019-03-08 22:33:50 +01:00
|
|
|
airq.value = sensor_value;
|
2019-02-24 20:46:39 +01:00
|
|
|
} else{
|
|
|
|
Serial.println("ERR: Sensor could not be identified");
|
|
|
|
}
|
|
|
|
|
2019-03-08 22:33:50 +01:00
|
|
|
if (!isnan(hmid.value) && !isnan(temp.value) && hmid.value != 0.0 && temp.value != 0.0){
|
|
|
|
abhu.value = (6.112 * ( pow(2.71828, ((17.67 * temp.value) / (temp.value + 243.5))) * hmid.value * 2.1674)) / (273.15 + temp.value);
|
2019-02-21 23:03:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 22:33:50 +01:00
|
|
|
publishSensor(client, temp);
|
|
|
|
publishSensor(client, hmid);
|
|
|
|
publishSensor(client, heat);
|
|
|
|
publishSensor(client, abhu);
|
|
|
|
publishSensor(client, airq);
|
2019-02-21 23:03:40 +01:00
|
|
|
}
|
|
|
|
}
|