diff --git a/WS3/Serial-ESP/Serial-ESP.ino b/WS3/Serial-ESP/Serial-ESP.ino index afdad48..9ce6187 100644 --- a/WS3/Serial-ESP/Serial-ESP.ino +++ b/WS3/Serial-ESP/Serial-ESP.ino @@ -23,7 +23,8 @@ // A4095B000C0000D0000E0000F0000G0000H0000I0000J0000K0000L0218M515N09654O.....*52 // #include -#include // Needed? +//#include // Needed? +#include #include #include #include @@ -40,22 +41,21 @@ #iclude #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"; #define mqtt_port 1883 -// Values -//char cfg_mqtt_server[40]; -//char cfg_mqtt_port[6]= "1883"; +// configuration values +char cfg_mqtt_server[40]; char idx_windir[4]="48"; char idx_temp[4] ="52"; 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 WindGust=0; // wind gust data values to keep track of the strongest gust in the last 10 minutes - // Definitions const char* Mqtt_clientid = "ESP-Weather-Station"; const char* dom_in = "domoticz/in"; @@ -153,8 +153,6 @@ struct WS3Packet { // Return the index according to Wind Angle String str_windir(unsigned int WinVal){ - //debug("str_windir() : "); - //debugln(WinVal); if(WinVal >= 360) return "N"; //N if(WinVal >= 0 && WinVal < 22) return "N"; //N if(WinVal >= 22 && WinVal < 45) return "NNE"; //NNE @@ -187,11 +185,11 @@ void setup_ota() { ArduinoOTA.setPassword((const char *)OTAPASSWORD); ArduinoOTA.onStart([]() { - Serial.println("Start"); + Serial.println("OTA Update is Starting !"); }); ArduinoOTA.onEnd([]() { - Serial.println("\nEnd"); + Serial.println("\nOTA is done. Rebooting..."); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { @@ -199,7 +197,7 @@ void setup_ota() { }); 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"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); @@ -212,7 +210,6 @@ void setup_ota() { // Setup the stuff. void setup() { - //Serial.begin(460800); Serial.begin(57600); while (!Serial) ; // wait for Arduino Serial Monitor to open Serial.println("\n\n"); @@ -225,21 +222,18 @@ void setup() { wm.setSaveConfigCallback(saveConfigCallback); // Setup custom parameters -// 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_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_mqtt_server("server", "Domoticz MQTT server hostname", cfg_mqtt_server, 40); + 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_rain ("idx3", "Domoticz Index for Rain Virtual Module", idx_rain, 4); // Add all parameters -// wm.addParameter(&custom_mqtt_server); -// wm.addParameter(&custom_mqtt_port); + //wm.addParameter(&custom_mqtt_server); wm.addParameter(&custom_idx_windir); wm.addParameter(&custom_idx_temp); wm.addParameter(&custom_idx_rain); // 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")) { Serial.println("Failer to connect and hit timeout"); delay(3000); @@ -257,8 +251,7 @@ void setup() { Serial.println("Connected to WiFi !"); // Read the updated parameters -// strcpy(cfg_mqtt_server, custom_mqtt_server.getValue()); -// strcpy(cfg_mqtt_port, custom_mqtt_port.getValue()); + //strcpy(cfg_mqtt_server, custom_mqtt_server.getValue()); strcpy(idx_windir, custom_idx_windir.getValue()); strcpy(idx_temp, custom_idx_temp.getValue()); strcpy(idx_rain, custom_idx_rain.getValue()); @@ -269,16 +262,11 @@ void setup() { DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.createObject(); -// json["mqtt_server"] = cfg_mqtt_server; -// json["mqtt_port"] = cfg_mqtt_port; + //json["mqtt_server"] = cfg_mqtt_server; json["idx_windir"] = idx_windir; json["idx_temp"] = idx_temp; 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"); if (!configFile) { Serial.println("failed to open config file for writing"); @@ -317,13 +305,14 @@ void setup() { debugln(" -> Packet memory allocated!"); // 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(MQipAddr, mqtt_port); //client.setCallback(callback); debugln("MQTT started"); - - // Initalize WinGust stuff - //loopcount = 0; - //WindGust = 0.0; } #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; // Handle Gust - debug(" B loopcount :"); - debugln(loopcount); if(p->wind_speed > WindGust) { WindGust = p->wind_speed; debugln("Update WindGust"); @@ -457,16 +444,8 @@ void parse_packet(String payload, WS3Packet* p) { WindGust = p->wind_speed; debugln("10 min expired -> reset counter"); } - - - debug("WindGust :"); - debug(WindGust); - debug(" loopcount :"); - debugln(loopcount); - //loopcount++; - loopcount = loopcount + 1; - debug(" A loopcount :"); - debugln(loopcount); + // Increment loopcount for Gust + loopcount++; } // 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 bar_forecast = 0; // Domoticz baro forcast float pression = 0.0; // Pression + float wct = 0.0; // Windchill temperature + // 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) + "\"}"; sendMQTTPayload(MQPayload); + // Temperature / Humidity / Baro // Humidity stuff @@ -562,12 +543,14 @@ void push_weather(WS3Packet* p) { if ((p->humidity >=35) && (p->humidity <50)) { humidity_status = 1; // Confortable } else { - // >=45 -> 70% + // >=50 -> 70% humidity_status = 0; // Normal } } // Correct the pression according to altitude pression = p->air_pressure + (ALTITUDE / 8.3); + // Pression should be adapted as it should be on sea level + // Prediction // 0 : No info // 1 : Sunny @@ -586,16 +569,16 @@ void push_weather(WS3Packet* p) { bar_forecast = 1; // Sunny } // 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)+"\"}"; sendMQTTPayload(MQPayload); // 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)+"\"}"; - 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)+"\"}"; + // Last temperature is WindChill + // 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); } @@ -698,15 +681,16 @@ void loop() { } // MQTT Stuff -void callback(char* topic, byte* payload, unsigned int length) { - debug("Message arrived ["); - Serial.print(topic); - debug("] "); - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - } - debug(" "); -} +//void callback(char* topic, byte* payload, unsigned int length) { +// debug("Message arrived ["); +// Serial.print(topic); +// debug("] "); +// for (int i = 0; i < length; i++) { +// Serial.print((char)payload[i]); +// } +// debug(" "); +//} + void reconnect() { // Loop until we're reconnected while (!client.connected()) { @@ -759,7 +743,7 @@ void saveConfigCallback () { void setupSpiffs(){ //clean FS, for testing - // SPIFFS.format(); + //SPIFFS.format(); //read configuration from FS json debugln("mounting FS..."); @@ -784,21 +768,10 @@ void setupSpiffs(){ debugln("\nparsed json"); //strcpy(cfg_mqtt_server, json["mqtt_server"]); - //strcpy(cfg_mqtt_port, json["mqtt_port"]); strcpy(idx_windir, json["idx_windir"]); strcpy(idx_temp, json["idx_temp"]); strcpy(idx_rain, json["idex_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"); - // } - + Serial.printf("Server : %s, idx (wind/temp/rain) : %s/%s/%s\n", cfg_mqtt_server, idx_windir, idx_temp, idx_rain); } else { Serial.println("failed to load json config"); } @@ -807,5 +780,4 @@ void setupSpiffs(){ } else { Serial.println("failed to mount FS"); } - //end read }