macrocell_office_iot/espsens/espsens.ino

166 lines
4.4 KiB
Arduino
Raw Normal View History

2019-02-18 22:01:30 +01:00
/* ESP8266 environmental sensor project
2019-02-18 20:18:30 +01:00
*
* 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 <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include "DHT.h"
#include "configuration.h"
2019-02-18 20:18:30 +01:00
#define TOPIC_BASE "sensor/" SENSOR_ID "/"
2019-02-18 21:08:51 +01:00
#define TOPIC_TEMP TOPIC_BASE "temperature"
#define TOPIC_HMID TOPIC_BASE "humidity"
#define TOPIC_HEAT TOPIC_BASE "heat_index"
2019-02-18 20:43:41 +01:00
#define DIFF_TEMP 0.3 // 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.2 // this one's actually quite active
2019-02-18 20:18:30 +01:00
#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);
2019-02-18 20:18:30 +01:00
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
setup_wifi();
client.setServer(MQTT_SERVER, MQTT_PORT);
2019-02-18 20:18:30 +01:00
dht.begin();
}
void setup_wifi() {
delay(10);
// Connect to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
2019-02-18 20:18:30 +01:00
WiFi.begin(WIFI_SSID, WIFI_PW);
2019-02-18 20:18:30 +01:00
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)) {
2019-02-18 20:18:30 +01:00
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 checkBound(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}
long lastMsg = 0;
float t_r = 0.0;
float h_r = 0.0;
float hi_r = 0.0;
long lastSent = 0;
bool published = false;
bool forcePublish = false;
bool publish (String desc, char *topic, float val, float &val_r, int diff, bool force){
if (checkBound(val, val_r, 0.3) or force) {
val_r = val;
Serial.print("New " + desc + ": ");
Serial.println(String(val).c_str());
client.publish(topic, String(val).c_str(), true);
return true;
} else {
return false;
}
}
2019-02-18 20:18:30 +01:00
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
digitalWrite(LED_BUILTIN, HIGH);
long now = millis();
2019-02-18 20:43:41 +01:00
if (now - lastMsg > OPERATION_PERIOD / 2) {
2019-02-18 20:18:30 +01:00
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);}
}
2019-02-18 20:43:41 +01:00
if (now - lastMsg > OPERATION_PERIOD) {
2019-02-18 20:18:30 +01:00
lastMsg = now;
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hi = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C Heat index: "));
Serial.print(hi);
Serial.println(F("°C "));
published = false;
2019-02-18 20:43:41 +01:00
if (lastSent > FORCE_PERIOD) {
2019-02-18 20:18:30 +01:00
Serial.println(F("Forcing a publish of all values"));
forcePublish = true;
lastSent = 0;
}
2019-02-18 20:43:41 +01:00
published += publish("temperature", TOPIC_TEMP, t, t_r, DIFF_TEMP, forcePublish);
published += publish("humidity", TOPIC_HMID, h, h_r, DIFF_HMID, forcePublish);
published += publish("heat index", TOPIC_HEAT, hi, hi_r, DIFF_HEAT, forcePublish);
2019-02-18 20:18:30 +01:00
if (published == false) {
lastSent = lastSent + 1;
} else {
lastSent = 0;
}
forcePublish = false;
}
}