Removed some debug.

Added WindChill formula.
Misc
This commit is contained in:
2020-12-15 20:54:08 +01:00
parent 981a83c80f
commit 2fb3c98097

View File

@ -23,7 +23,8 @@
// A4095B000C0000D0000E0000F0000G0000H0000I0000J0000K0000L0218M515N09654O.....*52 // A4095B000C0000D0000E0000F0000G0000H0000I0000J0000K0000L0218M515N09654O.....*52
// //
#include <FS.h> #include <FS.h>
#include <ESP8266WiFi.h> // Needed? //#include <ESP8266WiFi.h> // Needed?
#include <DNSServer.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
@ -40,22 +41,21 @@
#iclude <SPIFFS.h> #iclude <SPIFFS.h>
#endif /* ESP32 */ #endif /* ESP32 */
// Domoticz MQTT configuration overidded by WiFiManager // Domoticz MQTT configuration by WiFiManager
//IPAddress MQipAddr; // MQTT IP address from WiFiManager
const char *mqtt_server = "portbuild.home.oav.net"; const char *mqtt_server = "portbuild.home.oav.net";
#define mqtt_port 1883 #define mqtt_port 1883
// Values // configuration values
//char cfg_mqtt_server[40]; char cfg_mqtt_server[40];
//char cfg_mqtt_port[6]= "1883";
char idx_windir[4]="48"; char idx_windir[4]="48";
char idx_temp[4] ="52"; char idx_temp[4] ="52";
char idx_rain[4] ="51"; char idx_rain[4] ="51";
// To handle Gust
volatile unsigned int loopcount=0; // number of loops (300 = 10 minutes, since every Serial loops is 2s) volatile unsigned int loopcount=0; // number of loops (300 = 10 minutes, since every Serial loops is 2s)
volatile unsigned int WindGust=0; // wind gust data values to keep track of the strongest gust in the last 10 minutes volatile unsigned int WindGust=0; // wind gust data values to keep track of the strongest gust in the last 10 minutes
// Definitions // Definitions
const char* Mqtt_clientid = "ESP-Weather-Station"; const char* Mqtt_clientid = "ESP-Weather-Station";
const char* dom_in = "domoticz/in"; const char* dom_in = "domoticz/in";
@ -153,8 +153,6 @@ struct WS3Packet {
// Return the index according to Wind Angle // Return the index according to Wind Angle
String str_windir(unsigned int WinVal){ String str_windir(unsigned int WinVal){
//debug("str_windir() : ");
//debugln(WinVal);
if(WinVal >= 360) return "N"; //N if(WinVal >= 360) return "N"; //N
if(WinVal >= 0 && WinVal < 22) return "N"; //N if(WinVal >= 0 && WinVal < 22) return "N"; //N
if(WinVal >= 22 && WinVal < 45) return "NNE"; //NNE if(WinVal >= 22 && WinVal < 45) return "NNE"; //NNE
@ -187,11 +185,11 @@ void setup_ota() {
ArduinoOTA.setPassword((const char *)OTAPASSWORD); ArduinoOTA.setPassword((const char *)OTAPASSWORD);
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
Serial.println("Start"); Serial.println("OTA Update is Starting !");
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {
Serial.println("\nEnd"); Serial.println("\nOTA is done. Rebooting...");
}); });
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
@ -199,7 +197,7 @@ void setup_ota() {
}); });
ArduinoOTA.onError([](ota_error_t error) { ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error); Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
@ -212,7 +210,6 @@ void setup_ota() {
// Setup the stuff. // Setup the stuff.
void setup() { void setup() {
//Serial.begin(460800);
Serial.begin(57600); Serial.begin(57600);
while (!Serial) ; // wait for Arduino Serial Monitor to open while (!Serial) ; // wait for Arduino Serial Monitor to open
Serial.println("\n\n"); Serial.println("\n\n");
@ -226,20 +223,17 @@ void setup() {
// Setup custom parameters // Setup custom parameters
//WiFiManagerParameter custom_mqtt_server("server", "Domoticz MQTT server hostname", cfg_mqtt_server, 40); //WiFiManagerParameter custom_mqtt_server("server", "Domoticz MQTT server hostname", cfg_mqtt_server, 40);
// WiFiManagerParameter custom_mqtt_port("port", "Domoticz MQTT port", cfg_mqtt_port, 6);
WiFiManagerParameter custom_idx_windir ("idx1", "Domoticz Index for Wind Virtual Module", idx_windir, 4); WiFiManagerParameter custom_idx_windir ("idx1", "Domoticz Index for Wind Virtual Module", idx_windir, 4);
WiFiManagerParameter custom_idx_temp ("idx2", "Domoticz Index for Temp/Hum/Baro Virtual Module", idx_temp, 4); WiFiManagerParameter custom_idx_temp ("idx2", "Domoticz Index for Temp/Hum/Baro Virtual Module", idx_temp, 4);
WiFiManagerParameter custom_idx_rain ("idx3", "Domoticz Index for Rain Virtual Module", idx_rain, 4); WiFiManagerParameter custom_idx_rain ("idx3", "Domoticz Index for Rain Virtual Module", idx_rain, 4);
// Add all parameters // Add all parameters
//wm.addParameter(&custom_mqtt_server); //wm.addParameter(&custom_mqtt_server);
// wm.addParameter(&custom_mqtt_port);
wm.addParameter(&custom_idx_windir); wm.addParameter(&custom_idx_windir);
wm.addParameter(&custom_idx_temp); wm.addParameter(&custom_idx_temp);
wm.addParameter(&custom_idx_rain); wm.addParameter(&custom_idx_rain);
// Automatic connect using saved stuff otherwise start as an AP to configure it // Automatic connect using saved stuff otherwise start as an AP to configure it
// if (!wm.autoConnect("ESP-Weather-Station","ESP")) {
if (!wm.autoConnect("ESP-Weather-Station")) { if (!wm.autoConnect("ESP-Weather-Station")) {
Serial.println("Failer to connect and hit timeout"); Serial.println("Failer to connect and hit timeout");
delay(3000); delay(3000);
@ -258,7 +252,6 @@ void setup() {
// Read the updated parameters // Read the updated parameters
//strcpy(cfg_mqtt_server, custom_mqtt_server.getValue()); //strcpy(cfg_mqtt_server, custom_mqtt_server.getValue());
// strcpy(cfg_mqtt_port, custom_mqtt_port.getValue());
strcpy(idx_windir, custom_idx_windir.getValue()); strcpy(idx_windir, custom_idx_windir.getValue());
strcpy(idx_temp, custom_idx_temp.getValue()); strcpy(idx_temp, custom_idx_temp.getValue());
strcpy(idx_rain, custom_idx_rain.getValue()); strcpy(idx_rain, custom_idx_rain.getValue());
@ -270,15 +263,10 @@ void setup() {
JsonObject& json = jsonBuffer.createObject(); JsonObject& json = jsonBuffer.createObject();
//json["mqtt_server"] = cfg_mqtt_server; //json["mqtt_server"] = cfg_mqtt_server;
// json["mqtt_port"] = cfg_mqtt_port;
json["idx_windir"] = idx_windir; json["idx_windir"] = idx_windir;
json["idx_temp"] = idx_temp; json["idx_temp"] = idx_temp;
json["idx_rain"] = idx_rain; json["idx_rain"] = idx_rain;
// json["ip"] = WiFi.localIP().toString();
// json["gateway"] = WiFi.gatewayIP().toString();
// json["subnet"] = WiFi.subnetMask().toString();
File configFile = SPIFFS.open("/config.json", "w"); File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) { if (!configFile) {
Serial.println("failed to open config file for writing"); Serial.println("failed to open config file for writing");
@ -317,13 +305,14 @@ void setup() {
debugln(" -> Packet memory allocated!"); debugln(" -> Packet memory allocated!");
// Now connect to MQTT // Now connect to MQTT
//if(WiFi.hostByName(cfg_mqtt_server, MQipAddr) != 1)
// Serial.println("ERROR: Unable to resolv MQTT Hostname");
//else
// Serial.printf("INFO: Hostname %s resolved to %s\n",cfg_mqtt_server, MQipAddr.toString().c_str());
client.setServer(mqtt_server, mqtt_port); client.setServer(mqtt_server, mqtt_port);
//client.setServer(MQipAddr, mqtt_port);
//client.setCallback(callback); //client.setCallback(callback);
debugln("MQTT started"); debugln("MQTT started");
// Initalize WinGust stuff
//loopcount = 0;
//WindGust = 0.0;
} }
#ifdef DONT_CHKSUM #ifdef DONT_CHKSUM
@ -446,8 +435,6 @@ void parse_packet(String payload, WS3Packet* p) {
p->air_pressure = payload.substring(pressure_idx+1, pressure_idx+6).toInt()*.1; p->air_pressure = payload.substring(pressure_idx+1, pressure_idx+6).toInt()*.1;
// Handle Gust // Handle Gust
debug(" B loopcount :");
debugln(loopcount);
if(p->wind_speed > WindGust) { if(p->wind_speed > WindGust) {
WindGust = p->wind_speed; WindGust = p->wind_speed;
debugln("Update WindGust"); debugln("Update WindGust");
@ -457,16 +444,8 @@ void parse_packet(String payload, WS3Packet* p) {
WindGust = p->wind_speed; WindGust = p->wind_speed;
debugln("10 min expired -> reset counter"); debugln("10 min expired -> reset counter");
} }
// Increment loopcount for Gust
loopcount++;
debug("WindGust :");
debug(WindGust);
debug(" loopcount :");
debugln(loopcount);
//loopcount++;
loopcount = loopcount + 1;
debug(" A loopcount :");
debugln(loopcount);
} }
// Clear the packet before working on the next // Clear the packet before working on the next
@ -543,10 +522,12 @@ void push_weather(WS3Packet* p) {
byte humidity_status = 0; // Domoticz humdity status byte humidity_status = 0; // Domoticz humdity status
byte bar_forecast = 0; // Domoticz baro forcast byte bar_forecast = 0; // Domoticz baro forcast
float pression = 0.0; // Pression float pression = 0.0; // Pression
float wct = 0.0; // Windchill temperature
// Rain is mm x 100 -> Have to compute this. // Rain is mm x 100 -> Have to compute this.
// MQPayload = "{ \"idx\" : "+ String(domoticz_rain) +",\"nvalue\" : 0, \"svalue\" : \"" + String(p->rain_1m) + ";" + String(p->rain_1m) + "\"}";
MQPayload = "{ \"idx\" : "+ String(idx_rain) +",\"nvalue\" : 0, \"svalue\" :\"" + String(p->rain_1h) + ";" + String(p->rain_1m) + "\"}"; MQPayload = "{ \"idx\" : "+ String(idx_rain) +",\"nvalue\" : 0, \"svalue\" :\"" + String(p->rain_1h) + ";" + String(p->rain_1m) + "\"}";
sendMQTTPayload(MQPayload); sendMQTTPayload(MQPayload);
// Temperature / Humidity / Baro // Temperature / Humidity / Baro
// Humidity stuff // Humidity stuff
@ -562,12 +543,14 @@ void push_weather(WS3Packet* p) {
if ((p->humidity >=35) && (p->humidity <50)) { if ((p->humidity >=35) && (p->humidity <50)) {
humidity_status = 1; // Confortable humidity_status = 1; // Confortable
} else { } else {
// >=45 -> 70% // >=50 -> 70%
humidity_status = 0; // Normal humidity_status = 0; // Normal
} }
} }
// Correct the pression according to altitude // Correct the pression according to altitude
pression = p->air_pressure + (ALTITUDE / 8.3); pression = p->air_pressure + (ALTITUDE / 8.3);
// Pression should be adapted as it should be on sea level
// Prediction // Prediction
// 0 : No info // 0 : No info
// 1 : Sunny // 1 : Sunny
@ -586,16 +569,16 @@ void push_weather(WS3Packet* p) {
bar_forecast = 1; // Sunny bar_forecast = 1; // Sunny
} }
// See : https://github.com/G6EJD/ESP32-Weather-Forecaster // See : https://github.com/G6EJD/ESP32-Weather-Forecaster
//SLpressure_mB = (((p->air_pressure)/pow((1-((float)(ELEVATION))/44330), 5.255))/100.0);
//SLpressure_mB = p->air_pressure;
// MQPayload = "{ \"idx\" : "+ String(domoticz_temp) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->temp_f) + ";" + String(p->humidity) + ";0;" + String(SLpressure_mB) +";0\"}";
// MQPayload = "{ \"idx\" : "+ String(idx_temp) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->temp_f) + ";" + String(p->humidity) + ";"+String(humidity_status)+ ";" + String(p->air_pressure) +";0\"}";
MQPayload = "{ \"idx\" : "+ String(idx_temp) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->temp_f) + ";" + String(p->humidity) + ";"+String(humidity_status)+ ";" + String(pression) +";"+String(bar_forecast)+"\"}"; MQPayload = "{ \"idx\" : "+ String(idx_temp) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->temp_f) + ";" + String(p->humidity) + ";"+String(humidity_status)+ ";" + String(pression) +";"+String(bar_forecast)+"\"}";
sendMQTTPayload(MQPayload); sendMQTTPayload(MQPayload);
// Wind // Wind
// MQPayload = "{ \"idx\" : "+ String(domoticz_windir) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->wind_angle) + ";" + String(str_windir(p->wind_angle)) + ";" + String(p->wind_speed*10) + ";" + String(WindGust*10) +";" + String(p->temp_f) + ";"+String(p->temp_f)+"\"}"; // Last temperature is WindChill
MQPayload = "{ \"idx\" : "+ String(idx_windir) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->wind_angle) + ";" + String(str_windir(p->wind_angle)) + ";" + String(p->wind_speed) + ";" + String(WindGust) +";" + String(p->temp_f) + ";"+String(p->temp_f)+"\"}"; // Have to compute it https://www.alpiniste.fr/windchill-calculateur/
//wct = p->temp_f;
wct = 13.12 + 0.6215*p->temp_f - 11,37*pow((p->wind_speed * 3.6),0.16)+0.3965*p->temp_f*pow((p->wind_speed * 3.6),0.16);
MQPayload = "{ \"idx\" : "+ String(idx_windir) +",\"nvalue\" : 0, \"svalue\": \"" + String(p->wind_angle) + ";" + String(str_windir(p->wind_angle)) + ";" + String(p->wind_speed) + ";" + String(WindGust) +";" + String(p->temp_f) + ";"+String(wct)+"\"}";
sendMQTTPayload(MQPayload); sendMQTTPayload(MQPayload);
} }
@ -698,15 +681,16 @@ void loop() {
} }
// MQTT Stuff // MQTT Stuff
void callback(char* topic, byte* payload, unsigned int length) { //void callback(char* topic, byte* payload, unsigned int length) {
debug("Message arrived ["); // debug("Message arrived [");
Serial.print(topic); // Serial.print(topic);
debug("] "); // debug("] ");
for (int i = 0; i < length; i++) { // for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]); // Serial.print((char)payload[i]);
} // }
debug(" "); // debug(" ");
} //}
void reconnect() { void reconnect() {
// Loop until we're reconnected // Loop until we're reconnected
while (!client.connected()) { while (!client.connected()) {
@ -784,21 +768,10 @@ void setupSpiffs(){
debugln("\nparsed json"); debugln("\nparsed json");
//strcpy(cfg_mqtt_server, json["mqtt_server"]); //strcpy(cfg_mqtt_server, json["mqtt_server"]);
//strcpy(cfg_mqtt_port, json["mqtt_port"]);
strcpy(idx_windir, json["idx_windir"]); strcpy(idx_windir, json["idx_windir"]);
strcpy(idx_temp, json["idx_temp"]); strcpy(idx_temp, json["idx_temp"]);
strcpy(idx_rain, json["idex_rain"]); strcpy(idx_rain, json["idex_rain"]);
Serial.printf("Server : %s, idx (wind/temp/rain) : %s/%s/%s\n", cfg_mqtt_server, idx_windir, idx_temp, idx_rain);
// if(json["ip"]) {
// Serial.println("setting custom ip from config");
// strcpy(static_ip, json["ip"]);
// strcpy(static_gw, json["gateway"]);
// strcpy(static_sn, json["subnet"]);
// Serial.println(static_ip);
// } else {
// Serial.println("no custom ip in config");
// }
} else { } else {
Serial.println("failed to load json config"); Serial.println("failed to load json config");
} }
@ -807,5 +780,4 @@ void setupSpiffs(){
} else { } else {
Serial.println("failed to mount FS"); Serial.println("failed to mount FS");
} }
//end read
} }