Browse Source

Reconnect based on wifi events

Handle reconnects now via Wifi events.
master
Felix 4 months ago
parent
commit
54977b0730
Signed by: phoenix
GPG Key ID: 6E77A590E3F6D71C
  1. 162
      Sensors/ESP32/meteo-esp32.ino

162
Sensors/ESP32/meteo-esp32.ino

@ -1,24 +1,23 @@
#include <WiFi.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <Wire.h>
#include <WebServer.h>
/* ==== CONFIGURE HERE ====================================================== */
#define SERIAL_BAUD 115200
#define LED_BUILTIN 2
#define N_SAMPLES 2 // Average report over this samples
#define SAMPLE_DELAY 5000 // Milliseconds between samples
#define SAMPLE_ALPHA 0.75 // Sampling average alpha
#define N_SAMPLES 2 // Average report over this samples
#define SAMPLE_DELAY 5000 // Milliseconds between samples
#define SAMPLE_ALPHA 0.75 // Sampling average alpha
// TODO: Set your Wifi SSID and password
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
#define WIFI_RECONNECT_DELAY 10000 // Reconnection delay in milliseconds
#define WIFI_RECONNECT_DELAY 10000 // Reconnection delay in milliseconds
// TODO: Configure your node here
@ -33,9 +32,9 @@
/* ========================================================================== */
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
BME280I2C
bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
WiFiClient espClient;
PubSubClient client(espClient);
@ -45,60 +44,78 @@ WebServer server(80);
static float temp(0), hum(0), pres(0);
static void led_toggle(const bool is_on) {
digitalWrite(LED_BUILTIN, is_on?HIGH:LOW);
digitalWrite(LED_BUILTIN, is_on ? HIGH : LOW);
}
void setup() {
Serial.begin(SERIAL_BAUD);
while(!Serial) {} // Wait for serial port
while (!Serial) {
} // Wait for serial port
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
led_toggle(false);
// Init BME280
Wire.begin();
while(!bme.begin())
{
while (!bme.begin()) {
Serial.println("ERR: BME280");
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("BME280");
break;
case BME280::ChipModel_BMP280:
Serial.println("BMP280");
break;
default:
Serial.println("ERR: Sensor");
switch (bme.chipModel()) {
case BME280::ChipModel_BME280:
Serial.println("BME280");
break;
case BME280::ChipModel_BMP280:
Serial.println("BMP280");
break;
default:
Serial.println("ERR: Sensor");
}
read_bme280(temp,hum,pres); // Initial read
read_bme280(temp, hum, pres); // Initial read
// Wifi connection
while(true) {
WiFi.onEvent(wifi_connected, SYSTEM_EVENT_STA_CONNECTED);
WiFi.onEvent(wifi_got_ip, SYSTEM_EVENT_STA_GOT_IP);
WiFi.onEvent(wifi_disconnected, SYSTEM_EVENT_STA_DISCONNECTED);
WiFi.mode(WIFI_STA);
wifi_connect();
client.setServer(MQTT_REMOTE, MQTT_PORT);
server.on("/", www_handle);
server.on("/csv", csv_handle);
server.on("/json", json_handle);
server.begin();
}
void wifi_connect() {
while (true) {
Serial.println("Wifi...");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("ERR: Wifi");
delay(5000);
continue;
Serial.println("ERR: Wifi");
delay(5000);
continue;
} else {
break;
}
}
Serial.print("Wifi OK "); Serial.println(WiFi.localIP());
client.setServer(MQTT_REMOTE, MQTT_PORT);
server.on("/", www_handle);
server.on("/csv", csv_handle);
server.on("/json", json_handle);
server.begin();
}
void wifi_connected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("Wifi: Connected");
}
void wifi_got_ip(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void wifi_disconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.print("WiFi lost: ");
Serial.println(info.disconnected.reason);
wifi_connect();
}
int read_bme280(float &temp, float &hum, float &pres) {
@ -108,14 +125,13 @@ int read_bme280(float &temp, float &hum, float &pres) {
return 0;
}
void reconnect_mqtt() {
while (!client.connected()) {
if (client.connect(MQTT_CLIENTID)) {
} else {
Serial.print("MQTT connect failed, rc=");
Serial.print("MQTT connect failed: error code ");
Serial.print(client.state());
delay(5000); // wait 5 seconds to not flood the network
delay(5000); // wait 5 seconds to not flood the network
}
}
}
@ -124,14 +140,25 @@ void report(float temp, float hum, float pres) {
char message[256];
char topic[64];
snprintf(topic, 64, "meteo/%d", NODE_ID);
snprintf(message, 255, "{\"id\":%d,\"name\":\"%s\",\"t\":%.2f,\"hum\":%.2f,\"p\":%.2f}", NODE_ID, NODE_NAME, temp, hum, pres);
snprintf(message, 255,
"{\"id\":%d,\"name\":\"%s\",\"t\":%.2f,\"hum\":%.2f,\"p\":%.2f}",
NODE_ID, NODE_NAME, temp, hum, pres);
Serial.println(message);
client.publish(topic, message);
}
void www_handle() {
char html[512];
snprintf(html, 512, "<!DOCTYPE html>\n<html>\n<body><h1>ESP32 Meteo Node</h1>\n<table><tr><td>Node</td><td>%d <b>%s</b></td></tr> <tr><td>Temperature</td><td>%.2f deg C</td></tr> <tr><td>Humidity</td><td>%.2f %% rel</td></tr> <tr><td>Pressure</td><td>%.2f hPa</td></tr> <tr><td>Pressure</td><td>%.2f hPa</td></tr> </table>\n<p>Readings: <a href=\"/csv\">[csv]</a> <a href=\"/json\">[json]</a></p></body></html>", NODE_ID, NODE_NAME, temp, hum, pres);
snprintf(
html, 512,
"<!DOCTYPE html>\n<html>\n<body><h1>ESP32 Meteo "
"Node</h1>\n<table><tr><td>Node</td><td>%d <b>%s</b></td></tr> "
"<tr><td>Temperature</td><td>%.2f deg C</td></tr> "
"<tr><td>Humidity</td><td>%.2f %% rel</td></tr> "
"<tr><td>Pressure</td><td>%.2f hPa</td></tr> "
"<tr><td>Pressure</td><td>%.2f hPa</td></tr> </table>\n<p>Readings: <a "
"href=\"/csv\">[csv]</a> <a href=\"/json\">[json]</a></p></body></html>",
NODE_ID, NODE_NAME, temp, hum, pres);
server.send(200, "text/html", html);
}
@ -143,55 +170,36 @@ void csv_handle() {
void json_handle() {
char json[256];
snprintf(json, 255, "{\"node\":%d,\"name\":\"%s\",\"t\":%.2f,\"hum\":%.2f,\"p\":%.2f}\n", NODE_ID, NODE_NAME, temp, hum, pres);
snprintf(json, 255,
"{\"node\":%d,\"name\":\"%s\",\"t\":%.2f,\"hum\":%.2f,\"p\":%.2f}\n",
NODE_ID, NODE_NAME, temp, hum, pres);
server.send(200, "text/json", json);
}
void wifi_reconnect() {
Serial.print("Wifi reconnect ... ");
WiFi.disconnect();
WiFi.reconnect();
// XXX: Test if this is somehow behaving weird
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("failed");
} else {
Serial.println("ok");
}
}
void loop() {
// wifi reconnect, if required
if (WiFi.status() != WL_CONNECTED) {
// But don't spam reconnections
static unsigned long previousMillis = 0;
const unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= WIFI_RECONNECT_DELAY) {
wifi_reconnect();
previousMillis = currentMillis;
}
}
// mqtt reconnect
if (!client.connected()) { reconnect_mqtt(); }
if (!client.connected()) {
reconnect_mqtt();
}
client.loop();
server.handleClient();
// Sensor readout
const long timestamp = millis();
static long next_sample = timestamp + SAMPLE_DELAY;
if(timestamp > next_sample) {
if (timestamp > next_sample) {
next_sample = timestamp + SAMPLE_DELAY;
float t(0), h(0), p(0);
if(read_bme280(t, h, p) != 0) {
if (read_bme280(t, h, p) != 0) {
Serial.println("ERR: BME280");
} else {
temp = SAMPLE_ALPHA*temp + (1.0-SAMPLE_ALPHA)*t;
hum = SAMPLE_ALPHA*hum + (1.0-SAMPLE_ALPHA)*h;
pres = SAMPLE_ALPHA*pres + (1.0-SAMPLE_ALPHA)*p;
temp = SAMPLE_ALPHA * temp + (1.0 - SAMPLE_ALPHA) * t;
hum = SAMPLE_ALPHA * hum + (1.0 - SAMPLE_ALPHA) * h;
pres = SAMPLE_ALPHA * pres + (1.0 - SAMPLE_ALPHA) * p;
static int n = 0;
if(++n >= N_SAMPLES) {
report(temp,hum,pres);
if (++n >= N_SAMPLES) {
report(temp, hum, pres);
n = 0;
}
}

Loading…
Cancel
Save