111 lines
3.3 KiB
C++
111 lines
3.3 KiB
C++
/* ESP8266 environmental sensor project
|
|
|
|
This file provides functions for all the sensor publication used in espsens and envisens.
|
|
|
|
(C) 2019 Macrocell - Environmental sensing solutions
|
|
proudly presented by Macrocell - FPGA Innovators
|
|
*/
|
|
|
|
#include <WString.h>
|
|
#include "Arduino.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
#include "configuration.h"
|
|
#include "sensing.h"
|
|
|
|
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_PW);
|
|
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(PubSubClient client) {
|
|
// Loop until reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect(MQTT_CLIENT, MQTT_USER, MQTT_PW)) {
|
|
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 checkVariation(float newValue, float prevValue, float maxDiff) {
|
|
return !isnan(newValue) &&
|
|
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
|
|
}
|
|
|
|
bool checkPlausibility(float newValue, float prevValue, float maxDiff) {
|
|
return (newValue < prevValue + (maxDiff * 10) || newValue > prevValue - (maxDiff * 10));
|
|
}
|
|
|
|
void publishSensor (PubSubClient &client, Sensor &sensor){
|
|
bool force = false;
|
|
Serial.print("Sample " + sensor.desc + " : ");
|
|
Serial.print(String(sensor.value).c_str());
|
|
Serial.print(" last : ");
|
|
Serial.print(String(sensor.lastValue).c_str());
|
|
Serial.print(" samples since last update : ");
|
|
Serial.println(String(sensor.lastUpdate).c_str());
|
|
|
|
// check if first sample, set lastValue and return
|
|
if (sensor.lastUpdate == -1){
|
|
sensor.lastValue = sensor.value;
|
|
sensor.lastUpdate = 0;
|
|
return;
|
|
}
|
|
|
|
// force an update if the number of samples without update is exceeded
|
|
force = sensor.lastUpdate >= FORCE_PERIOD;
|
|
if (force) {
|
|
Serial.println(" * Forcing publication if sensor value is plausible");
|
|
}
|
|
|
|
// update the sample counter, will be reset if sample is published
|
|
sensor.lastUpdate += 1;
|
|
|
|
// check if there was sufficient variation on the signal and if the value is plausible
|
|
if (checkVariation(sensor.value, sensor.lastValue, sensor.diff) or force) {
|
|
Serial.println(" Sufficient variation on " + sensor.desc + ": " + String(sensor.value).c_str());
|
|
if (checkPlausibility(sensor.value, sensor.lastValue, sensor.diff)) {
|
|
Serial.println(" Value published");
|
|
client.publish(sensor.topic, String(sensor.value).c_str(), true);
|
|
sensor.lastUpdate = 0;
|
|
} else {
|
|
Serial.print(" ** ERROR: Value out of bounds, not published. Last value :");
|
|
Serial.println(String(sensor.lastValue).c_str());
|
|
}
|
|
sensor.lastValue = sensor.value;
|
|
}
|
|
}
|