diff --git a/WS3/Serial-ESP/Serial-ESP.ino b/WS3/Serial-ESP/Serial-ESP.ino index c348d7c..a99c63c 100644 --- a/WS3/Serial-ESP/Serial-ESP.ino +++ b/WS3/Serial-ESP/Serial-ESP.ino @@ -22,7 +22,7 @@ // // A4095B000C0000D0000E0000F0000G0000H0000I0000J0000K0000L0218M515N09654O.....*52 // -#include +#include #include #include #include @@ -36,10 +36,6 @@ #include #include // Version 5 ONLY (TODO upgrade this) -#ifdef ESP32 - #iclude -#endif /* ESP32 */ - // Domoticz MQTT configuration by WiFiManager const char *mqtt_server = "portbuild.home.oav.net"; #define mqtt_port 1883 @@ -169,6 +165,9 @@ String str_windir(unsigned int WinVal){ if(WinVal >= 337 && WinVal < 359) return "NNW"; //NNW } +// Make compiler ok +void sendMQTTPayload(String msgpayload); + // Setup OTA stuff void setup_ota() { //Port defaults to 8266 @@ -204,101 +203,6 @@ void setup_ota() { ArduinoOTA.begin(); } -// Setup the stuff. -void setup() { - Serial.begin(57600); - while (!Serial) ; // wait for Arduino Serial Monitor to open - Serial.println("\n\n"); - setupSpiffs(); - Serial.println("Weather Station 3 Adapter by Kiwi"); - Serial.println(ESP.getFullVersion()); - - WiFiManager wm; - - wm.setSaveConfigCallback(saveConfigCallback); - - // Setup custom parameters - 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_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")) { - Serial.println("Failer to connect and hit timeout"); - delay(3000); - // if we still have not connected restard and try again - ESP.restart(); - delay(5000); - } - - // Always start configportal for a little while (2 m) - wm.setConfigPortalTimeout(30); -// wm.startConfigPortal("ESP-Weather-Station","ESP"); - wm.startConfigPortal("ESP-Weather-Station"); - - // If we get here we are connected ! - Serial.println("Connected to WiFi !"); - - // Read the updated parameters - strcpy(idx_windir, custom_idx_windir.getValue()); - strcpy(idx_temp, custom_idx_temp.getValue()); - strcpy(idx_rain, custom_idx_rain.getValue()); - - // Save the custom parameters to FS - if (shouldSaveConfig) { - Serial.println("Saving config to FS..."); - DynamicJsonBuffer jsonBuffer; - - JsonObject& json = jsonBuffer.createObject(); - json["idx_windir"] = idx_windir; - json["idx_temp"] = idx_temp; - json["idx_rain"] = idx_rain; - - File configFile = SPIFFS.open("/config.json", "w"); - if (!configFile) { - Serial.println("failed to open config file for writing"); - } - - json.prettyPrintTo(Serial); - json.printTo(configFile); - configFile.close(); - //end save - shouldSaveConfig = false; - } - - // Launch OTA stuff - setup_ota(); - - #if LWIP_IPV6 - Serial.printf("IPV6 is enabled\n"); - #else - Serial.printf("IPV6 is not enabled\n"); - #endif - Serial.print("My IP address: "); - Serial.print(WiFi.localIP()); - Serial.print("/"); - Serial.print(WiFi.subnetMask()); - Serial.print(" GW:"); - Serial.println(WiFi.gatewayIP()); - - // Start the Software Serial for WS3 - WS3.begin(WS3_BAUD); - debugln("WS3 UART is ready..."); - - // Allocate memory for packet - pkt_str.reserve(WS3_PKT_LEN); - debugln(" -> Packet memory allocated!"); - - client.setServer(mqtt_server, mqtt_port); - //client.setCallback(callback); - debugln("MQTT started"); -} - #ifdef DONT_CHKSUM // Validate packet using the checksum. // Work only APRS data on this 51W3 board. @@ -611,57 +515,6 @@ bool process_packet(String pkt, WS3Packet* p) { return true; } -void loop() { - // MQTT - if (!client.connected()) { - reconnect(); - } - - // While data comes in and we don't have a pending packet to process... - while (WS3.available() && pkt_ok !=true) { - - // Pull the bytes off the stream - char inChar = (char)WS3.read(); - - // And build up the packet - pkt_str += inChar; - - // Until we hit the end - if (inChar == '\n') { - pkt_ok = true; - } - } - - // Yay, we now have a packet! - // Now, we attempt to parse out the string into a packet that we can work with - if (pkt_ok) { - debugln("pkt_ok!"); - - // At this point, we have a string of characters that was probably a valid packet - // We set get some memory and attempt to parse the string into the struct - WS3Packet p = {}; - - // Validate the payload, then parse it. - if (process_packet(pkt_str, &p)) { - // print results if parse OK - #ifdef DEBUG - print_weather(&p); - #endif /* DEBUG */ - // Push to MQTT - push_weather(&p); - debugln("processed"); - } else { - debugln("unable to parse packet :("); - } - - - // clear so we can start again - pkt_str = ""; - pkt_ok = false; - clear_pkt(&p); - } - ArduinoOTA.handle(); -} // MQTT Stuff //void callback(char* topic, byte* payload, unsigned int length) { @@ -724,19 +577,19 @@ void saveConfigCallback () { shouldSaveConfig = true; } -void setupSpiffs(){ +void setupLittleFS(){ //clean FS, for testing - //SPIFFS.format(); + //LittleFS.format(); //read configuration from FS json debugln("mounting FS..."); - if (SPIFFS.begin()) { + if (LittleFS.begin()) { debugln("mounted file system"); - if (SPIFFS.exists("/config.json")) { + if (LittleFS.exists("/config.json")) { //file exists, reading and loading debugln("reading config file"); - File configFile = SPIFFS.open("/config.json", "r"); + File configFile = LittleFS.open("/config.json", "r"); if (configFile) { debugln("opened config file"); size_t size = configFile.size(); @@ -744,10 +597,18 @@ void setupSpiffs(){ std::unique_ptr buf(new char[size]); configFile.readBytes(buf.get(), size); +// #ifdef ARDUINOJSON_VERSION_MAJOR >=6 +// DynamicJsonDocument json(1024); +// auto deserializeError = deserializeJson(json, buf.get()); +// serializeJson(json, Serial); +// if ( ! deserializeError) { +// #else +// } DynamicJsonBuffer jsonBuffer; JsonObject& json = jsonBuffer.parseObject(buf.get()); json.printTo(Serial); if (json.success()) { + // #endif debugln("\nparsed json"); strcpy(idx_windir, json["idx_windir"]); @@ -761,4 +622,162 @@ void setupSpiffs(){ } else { Serial.println("failed to mount FS"); } + +} + +// Setup the stuff. +void setup() { + Serial.begin(57600); + while (!Serial) ; // wait for Arduino Serial Monitor to open + Serial.println("\n\n"); + setupLittleFS(); + Serial.println("Weather Station 3 Adapter by Kiwi"); + Serial.println(ESP.getFullVersion()); + + WiFiManager wm; + + wm.setSaveConfigCallback(saveConfigCallback); + + // Setup custom parameters + 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_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")) { + Serial.println("Failer to connect and hit timeout"); + delay(3000); + // if we still have not connected restard and try again + ESP.restart(); + delay(5000); + } + + // Always start configportal for a little while (2 m) + wm.setConfigPortalTimeout(30); +// wm.startConfigPortal("ESP-Weather-Station","ESP"); + wm.startConfigPortal("ESP-Weather-Station"); + + // If we get here we are connected ! + Serial.println("Connected to WiFi !"); + + // Read the updated parameters + strcpy(idx_windir, custom_idx_windir.getValue()); + strcpy(idx_temp, custom_idx_temp.getValue()); + strcpy(idx_rain, custom_idx_rain.getValue()); + + // Save the custom parameters to FS + if (shouldSaveConfig) { + Serial.println("Saving config to FS..."); +// #ifdef ARDUINOJSON_VERSION_MAJOR >= 6 +// DynamicJsonDocument json(1024); +// #else + DynamicJsonBuffer jsonBuffer; + + JsonObject& json = jsonBuffer.createObject(); +// #endif + + json["idx_windir"] = idx_windir; + json["idx_temp"] = idx_temp; + json["idx_rain"] = idx_rain; + + File configFile = LittleFS.open("/config.json", "w"); + if (!configFile) { + Serial.println("failed to open config file for writing"); + } + +//#ifdef ARDUINOJSON_VERSION_MAJOR >= 6 +// serializeJson(json, Serial); +// serializeJson(json, configFile); +//#else + json.prettyPrintTo(Serial); + json.printTo(configFile); +//#endif + configFile.close(); + //end save + shouldSaveConfig = false; + } + + // Launch OTA stuff + setup_ota(); + + #if LWIP_IPV6 + Serial.printf("IPV6 is enabled\n"); + #else + Serial.printf("IPV6 is not enabled\n"); + #endif + Serial.print("My IP address: "); + Serial.print(WiFi.localIP()); + Serial.print("/"); + Serial.print(WiFi.subnetMask()); + Serial.print(" GW:"); + Serial.println(WiFi.gatewayIP()); + + // Start the Software Serial for WS3 + WS3.begin(WS3_BAUD); + debugln("WS3 UART is ready..."); + + // Allocate memory for packet + pkt_str.reserve(WS3_PKT_LEN); + debugln(" -> Packet memory allocated!"); + + client.setServer(mqtt_server, mqtt_port); + //client.setCallback(callback); + debugln("MQTT started"); +} + +void loop() { + // MQTT + if (!client.connected()) { + reconnect(); + } + + // While data comes in and we don't have a pending packet to process... + while (WS3.available() && pkt_ok !=true) { + + // Pull the bytes off the stream + char inChar = (char)WS3.read(); + + // And build up the packet + pkt_str += inChar; + + // Until we hit the end + if (inChar == '\n') { + pkt_ok = true; + } + } + + // Yay, we now have a packet! + // Now, we attempt to parse out the string into a packet that we can work with + if (pkt_ok) { + debugln("pkt_ok!"); + + // At this point, we have a string of characters that was probably a valid packet + // We set get some memory and attempt to parse the string into the struct + WS3Packet p = {}; + + // Validate the payload, then parse it. + if (process_packet(pkt_str, &p)) { + // print results if parse OK + #ifdef DEBUG + print_weather(&p); + #endif /* DEBUG */ + // Push to MQTT + push_weather(&p); + debugln("processed"); + } else { + debugln("unable to parse packet :("); + } + + + // clear so we can start again + pkt_str = ""; + pkt_ok = false; + clear_pkt(&p); + } + ArduinoOTA.handle(); }