Initial commit of FW source files

This commit is contained in:
bzi 2019-02-18 20:18:30 +01:00
parent 50d25b90b9
commit cc268b5cd5
3 changed files with 191 additions and 0 deletions

1
esp_sensor_firmware/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
mqtt_credentials.h

View File

@ -0,0 +1,173 @@
/* ESP 8266 environmental sensor FW
*
* 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 "mqtt_credentials.h"
#define ID_BASE "sensor/window/"
#define ID_TEMP ID_BASE "/temperature"
#define ID_HUMID ID_BASE "/temperature"
#define ID_HEAT ID_BASE "/temperature"
#define OPERATION_BLINK_EN true // blink continously if in correct operation
#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(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
dht.begin();
}
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("ESP8266Client", 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);
}
}
}
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;
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
digitalWrite(LED_BUILTIN, HIGH);
long now = millis();
if (now - lastMsg > 2500) {
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);}
}
if (now - lastMsg > 5000) {
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;
if (lastSent > 12) {
Serial.println(F("Forcing a publish of all values"));
forcePublish = true;
lastSent = 0;
}
if (checkBound(t, t_r, 0.3) or forcePublish) {
t_r = t;
Serial.print("New temperature:");
Serial.println(String(t).c_str());
client.publish(ID_TEMP, String(t).c_str(), true);
published = true;
}
if (checkBound(h, h_r, 2.5) or forcePublish) {
h_r = h;
Serial.print("New humidity:");
Serial.println(String(h).c_str());
client.publish(ID_HUMID, String(h).c_str(), true);
published = true;
}
if (checkBound(hi, hi_r, 0.2) or forcePublish) {
hi_r = hi;
Serial.print("New heat index:");
Serial.println(String(hi).c_str());
client.publish(ID_HEAT, String(hi).c_str(), true);
published = true;
}
if (published == false) {
lastSent = lastSent + 1;
} else {
lastSent = 0;
}
forcePublish = false;
}
}

View File

@ -0,0 +1,17 @@
/* ESP 8266 environmental sensor FW
*
* This header provides the necessary credentials for the esp_sensor_firmware.ino file.
* Before compilation, add your credentials and remove the .example postfix from the filename.
* ** WARNING ** DO NOT COMMIT YOUR mqtt_credentials.h FILE CONTAINING YOUR ACTUAL CREDENTIALS.
*
* (C) 2019 Macrocell - Environmental sensing solutions
* proudly presented by Macrocell - FPGA Innovators
*/
#define wifi_ssid "macrocell_iot"
#define wifi_password "ExamplePassword"
#define mqtt_server "192.168.x.x"
#define mqtt_port 1883
#define mqtt_user "your_username"
#define mqtt_password "your_password"