69 lines
2.0 KiB
C++
69 lines
2.0 KiB
C++
/* 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 <ESP8266WiFi.h>
|
|
#include <Wire.h>
|
|
#include <PubSubClient.h>
|
|
#include "DHT.h"
|
|
#include "configuration.h"
|
|
#include "sensing.h"
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
DHT dht(DHTPIN, DHTTYPE);
|
|
|
|
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
|
|
|
|
void setup() {
|
|
Serial.begin(SERIAL_BAUDRATE);
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
digitalWrite(LED_BUILTIN, LOW);
|
|
setup_wifi();
|
|
client.setServer(MQTT_SERVER, MQTT_PORT);
|
|
dht.begin();
|
|
}
|
|
|
|
long lastMsg = 0;
|
|
|
|
void loop() {
|
|
if (!client.connected()) {
|
|
reconnect(client);
|
|
}
|
|
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);
|
|
}
|
|
|
|
publishSensor(client, temp);
|
|
publishSensor(client, hmid);
|
|
publishSensor(client, heat);
|
|
publishSensor(client, abhu);
|
|
|
|
}
|
|
}
|