macrocell_office_iot/espsens/espsens.ino

69 lines
2.0 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"
#include "sensing.h"
2019-02-18 20:18:30 +01:00
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
2019-02-18 20:18:30 +01:00
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();
}
long lastMsg = 0;
2019-02-18 20:18:30 +01:00
void loop() {
if (!client.connected()) {
reconnect(client);
2019-02-18 20:18:30 +01:00
}
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);}
hmid.value = dht.readHumidity();
temp.value = dht.readTemperature();
2019-02-18 20:18:30 +01:00
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);
2019-02-18 20:18:30 +01:00
}
publishSensor(client, temp);
publishSensor(client, hmid);
publishSensor(client, heat);
publishSensor(client, abhu);
2019-02-18 20:18:30 +01:00
}
}