diff --git a/ESP32-Serial-WiFi-Client/ESP32-Serial-WiFi-Client.ino b/ESP32-Serial-WiFi-Client/ESP32-Serial-WiFi-Client.ino index c8c58bd..f3abe14 100644 --- a/ESP32-Serial-WiFi-Client/ESP32-Serial-WiFi-Client.ino +++ b/ESP32-Serial-WiFi-Client/ESP32-Serial-WiFi-Client.ino @@ -13,6 +13,7 @@ #include #include #include +#include "esp_system.h" #include "client_config.h" @@ -29,16 +30,25 @@ uint8_t buf[BUFFERSIZE]; uint16_t num = 0; +const int wdtTimeout = 30000; // reboot after 30 seconds of no host +hw_timer_t *timer = NULL; + +void ARDUINO_ISR_ATTR resetModule() { + debug.println("Rebooting..."); + esp_restart(); +} + #ifdef PROTOCOL_TCP WiFiClient client; void connect_to_host() { - debug.printf("Connecting to %s:%d...", HOST_IP.toString(), HOST_PORT); - while (!client.connect(HOST_IP, HOST_PORT)) { - delay(500); - debug.print('.'); - } - debug.println("connected\n"); - delay(1000); + debug.printf("Connecting to %s:%d...", HOST_IP.toString(), HOST_PORT); + debug.println("while, connect_to_host"); + while (!client.connect(HOST_IP, HOST_PORT)) { + delay(500); + debug.print('.'); + } + debug.println("connected, connect_to_host\n"); + delay(1000); } #endif @@ -47,112 +57,131 @@ AsyncUDP udp; #endif void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { - debug.print("WiFi disconnected: "); - debug.println(info.wifi_sta_disconnected.reason); - debug.println("Trying to reconnect.."); - WiFi.begin(SSID, PASSWD); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - debug.print("."); + debug.print("WiFi disconnected: "); + debug.println(info.wifi_sta_disconnected.reason); + debug.println("Trying to reconnect.."); + WiFi.begin(SSID, PASSWD); + debug.println("while, WiFiStationDisconnected"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + debug.print("."); + if(client.connect(HOST_IP, HOST_PORT)) { + break; } - debug.println("connected"); - debug.print("IP address: "); - debug.println(WiFi.localIP()); + } + debug.println("connected, WiFiStationDisconnected"); + debug.print("IP address: "); + debug.println(WiFi.localIP()); } void setup() { - delay(500); + delay(500); - Serial.begin(CLIENT_BAUD, CLIENT_PARAM, CLIENT_RXPIN, CLIENT_TXPIN); - - debug.print("\n\nWiFi serial bridge client "); - debug.println(VERSION); - - debug.println("Open ESP Station Mode"); - WiFi.mode(WIFI_STA); - WiFi.onEvent(WiFiStationDisconnected, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); - WiFi.begin(SSID, PASSWD); - debug.print("Connecting to: "); - debug.print(SSID); - debug.print(".."); - while (WiFi.status() != WL_CONNECTED) { - delay(500); - debug.print("."); - } - debug.println("connected"); - debug.print("IP address: "); - debug.println(WiFi.localIP()); + Serial.begin(CLIENT_BAUD, CLIENT_PARAM, CLIENT_RXPIN, CLIENT_TXPIN); -#ifdef OTA_HANDLER - ArduinoOTA.onStart([]() { - String type; - if (ArduinoOTA.getCommand() == U_FLASH) - type = "sketch"; - else // U_SPIFFS - type = "filesystem"; - - // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS - // using SPIFFS.end() - Serial.println("Start updating " + type); - }); - ArduinoOTA.onEnd([]() { Serial.println("\nEnd"); }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - Serial.printf("Progress: %u%%\r", (progress / (total / 100))); - }); - ArduinoOTA.onError([](ota_error_t error) { - Serial.printf("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"); - else if (error == OTA_RECEIVE_ERROR) - Serial.println("Receive Failed"); - else if (error == OTA_END_ERROR) - Serial.println("End Failed"); - }); + debug.print("\n\nWiFi serial bridge client "); + debug.println(VERSION); - ArduinoOTA.begin(); + debug.println("Open ESP Station Mode"); + WiFi.mode(WIFI_STA); + WiFi.onEvent(WiFiStationDisconnected, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); + WiFi.begin(SSID, PASSWD); + debug.print("Connecting to: "); + debug.print(SSID); + debug.print(".."); + + timer = timerBegin(1000000); //timer 1MHz + timerAttachInterrupt(timer, &resetModule); //attach callback + timerAlarm(timer, wdtTimeout * 1000, false, 0); //set time in us + debug.println("Watch dog timer Setup done"); + + debug.println("while, setup"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + debug.print("."); + } + timerWrite(timer, 0); //reset timer (feed watchdog) + debug.println("connected, setup"); + debug.print("IP address: "); + debug.println(WiFi.localIP()); + +#ifdef OTA_HANDLER + ArduinoOTA.onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; + + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS + // using SPIFFS.end() + Serial.println("Start updating " + type); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("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"); + else if (error == OTA_RECEIVE_ERROR) + Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) + Serial.println("End Failed"); + }); + + ArduinoOTA.begin(); #endif #ifdef PROTOCOL_UDP - if (udp.listen(HOST_PORT)) { - debug.printf("Listening on UDP port %d\n", HOST_PORT); - udp.onPacket([](AsyncUDPPacket packet) { - if (packet.localPort() == HOST_PORT) - Serial.write(packet.data(), packet.length()); - }); - } + if (udp.listen(HOST_PORT)) { + debug.printf("Listening on UDP port %d\n", HOST_PORT); + udp.onPacket([](AsyncUDPPacket packet) { + if (packet.localPort() == HOST_PORT) + Serial.write(packet.data(), packet.length()); + }); + } #endif } void loop() { #ifdef OTA_HANDLER - ArduinoOTA.handle(); + ArduinoOTA.handle(); #endif -#ifdef PROTOCOL_TCP - if (!client.connected()) connect_to_host(); + timerWrite(timer, 0); //reset timer (feed watchdog) - while (client.available()) { - buf[num] = client.read(); - num++; - if (num == BUFFERSIZE - 1) break; - } - if (num > 0) Serial.write(buf, num); - num = 0; +#ifdef PROTOCOL_TCP + if (!client.connected()) { + connect_to_host(); + } + + while (client.available()) { + buf[num] = client.read(); + num++; + if (num == BUFFERSIZE - 1) break; + } + if (num > 0) Serial.write(buf, num); + num = 0; #endif - while (Serial.available()) { - buf[num] = Serial.read(); - num++; - if (num == BUFFERSIZE - 1) break; - } + while (Serial.available()) { + buf[num] = Serial.read(); + num++; + if (num == BUFFERSIZE - 1) break; + } #ifdef PROTOCOL_TCP - if (num > 0) client.write(buf, num); + if (num > 0) client.write(buf, num); #elif defined(PROTOCOL_UDP) - udp.broadcastTo(buf, num, HOST_PORT + 1); + //udp.broadcastTo(buf, num, HOST_PORT + 1); + udp.broadcastTo(buf, num, HOST_PORT); #endif - num = 0; + num = 0; } diff --git a/ESP32-Serial-WiFi-Client/client_config.h b/ESP32-Serial-WiFi-Client/client_config.h index 7c6220c..7083b77 100644 --- a/ESP32-Serial-WiFi-Client/client_config.h +++ b/ESP32-Serial-WiFi-Client/client_config.h @@ -20,9 +20,11 @@ #define VERSION "2.0-ESP32" -#define PROTOCOL_UDP // PROTOCOL_TCP or PROTOCOL_UDP +#define PROTOCOL_TCP // PROTOCOL_TCP or PROTOCOL_UDP +//#define PROTOCOL_UDP // PROTOCOL_TCP or PROTOCOL_UDP #define HOST_IP IPAddress(192, 168, 4, 1) // only used for PROTOCOL_TCP -#define HOST_PORT 14550 // TCP or UDP port +//#define HOST_PORT 14550 // UDP port +#define HOST_PORT 8880 // TCP port #define CLIENT_BAUD 115200 #define CLIENT_PARAM SERIAL_8N1 #define CLIENT_TXPIN 1 diff --git a/config.h b/config.h index 968644e..b8aed6a 100644 --- a/config.h +++ b/config.h @@ -31,10 +31,10 @@ #define VERSION "2.0-ESP8266" #endif -#define MODE_STA // MODE_STA or MODE_AP +#define MODE_AP // MODE_STA or MODE_AP #define PROTOCOL_TCP // uncomment to enable TCP server #define MAX_NMEA_CLIENTS 4 // max TCP clients -#define PROTOCOL_UDP // uncomment to enable UDP broadcast (ESP32 only) +//#define PROTOCOL_UDP // uncomment to enable UDP broadcast (ESP32 only) //#define BLUETOOTH 0 // uncomment to create a bluetooth serial bridge on the indicated serial port (ESP32 only) //#define BATTERY_SAVER // uncomment to reduce wifi power