diff --git a/examples/advancedExample/advancedExample.ino b/examples/advancedExample/advancedExample.ino index 587a4a1..1e87041 100644 --- a/examples/advancedExample/advancedExample.ino +++ b/examples/advancedExample/advancedExample.ino @@ -1,81 +1,125 @@ #include //#include "WifiConfig.h" +#define USE_ETHERNET false /* false: WiFi or true: lwIP_Etherner */ +#define USE_STATIC false /* false: DHCP or true: STATIC */ + #include + #ifdef ESP32 -#include + #include + + #undef USE_ETHERNET + #define USE_ETHERNET false #else -#include -#endif + #include + + #if (USE_ETHERNET) + #include + #include + + #define ETH_SS_PIN 4 + ENC28J60lwIP eth(ETH_SS_PIN); + #endif +#endif //ESP32 #ifndef WIFI_CONFIG_H -#define YOUR_WIFI_SSID "YOUR_WIFI_SSID" -#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD" + #define YOUR_WIFI_SSID "YOUR_WIFI_SSID" + #define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD" + + // YOUR_ROUTER_SETTINGS + #if (USE_STATIC) + IPAddress apIP(192, 168, 1, 10); + IPAddress gwIP(192, 168, 1, 1); + IPAddress snIP(255, 255, 255, 0); + IPAddress dnIP1(192, 168, 1, 1); + IPAddress dnIP2( 8, 8, 8, 8); + #endif #endif // !WIFI_CONFIG_H #ifdef ESP32 -#define ONBOARDLED 5 // Built in LED on some ESP-32 boards + #define ONBOARDLED 5 // Built in LED on some ESP-32 boards #else -#define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07 + #define ONBOARDLED 2 // Built in LED on ESP-12/ESP-07 #endif + #define SHOW_TIME_PERIOD 1000 #define NTP_TIMEOUT 5000 const PROGMEM char* ntpServer = "pool.ntp.org"; -bool wifiFirstConnected = false; +bool conFirstConnected = false; +bool conLastStatus = false; boolean syncEventTriggered = false; // True if a time even has been triggered NTPEvent_t ntpEvent; // Last triggered event double offset; double timedelay; -#ifdef ESP32 -void onWifiEvent (system_event_id_t event, system_event_info_t info) { -#else -void onWifiEvent (WiFiEvent_t event) { -#endif - Serial.printf ("[WiFi-event] event: %d\n", event); +// weak functions to get connection status, reconnect and IP address of device +#if (USE_ETHERNET) + bool connectionStatus() { + return eth.connected (); + } + + bool connectionReconnect() { + return true; + } + + IPAddress getDeviceIP() { + return eth.localIP (); + } +#endif //USE_ETHERNET - switch (event) { -#ifdef ESP32 - case SYSTEM_EVENT_STA_CONNECTED: - Serial.printf ("Connected to %s. Asking for IP address.\r\n", info.connected.ssid); - break; - case SYSTEM_EVENT_STA_GOT_IP: - Serial.printf ("Got IP: %s\r\n", IPAddress (info.got_ip.ip_info.ip.addr).toString ().c_str ()); - Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no"); - digitalWrite (ONBOARDLED, LOW); // Turn on LED - wifiFirstConnected = true; - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - Serial.printf ("Disconnected from SSID: %s\n", info.disconnected.ssid); - Serial.printf ("Reason: %d\n", info.disconnected.reason); - digitalWrite (ONBOARDLED, HIGH); // Turn off LED - //NTP.stop(); // NTP sync can be disabled to avoid sync errors - WiFi.reconnect (); - break; -#else - case WIFI_EVENT_STAMODE_CONNECTED: - Serial.printf ("Connected to %s. Asking for IP address.\r\n", WiFi.BSSIDstr().c_str()); - break; - case WIFI_EVENT_STAMODE_GOT_IP: - Serial.printf ("Got IP: %s\r\n", WiFi.localIP().toString().c_str ()); - Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no"); - digitalWrite (ONBOARDLED, LOW); // Turn on LED - wifiFirstConnected = true; - break; - case WIFI_EVENT_STAMODE_DISCONNECTED: - Serial.printf ("Disconnected from SSID: %s\n", WiFi.BSSIDstr ().c_str ()); - //Serial.printf ("Reason: %d\n", info.disconnected.reason); - digitalWrite (ONBOARDLED, HIGH); // Turn off LED - //NTP.stop(); // NTP sync can be disabled to avoid sync errors - WiFi.reconnect (); - break; -#endif - default: - break; - } -} +#if (!USE_ETHERNET) + #ifdef ESP32 + void onWifiEvent (system_event_id_t event, system_event_info_t info) { + #else + void onWifiEvent (WiFiEvent_t event) { + #endif + Serial.printf ("[WiFi-event] event: %d\n", event); + + switch (event) { + #ifdef ESP32 + case SYSTEM_EVENT_STA_CONNECTED: + Serial.printf ("Connected to %s. Asking for IP address.\r\n", info.connected.ssid); + break; + case SYSTEM_EVENT_STA_GOT_IP: + Serial.printf ("Got IP: %s\r\n", IPAddress (info.got_ip.ip_info.ip.addr).toString ().c_str ()); + Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no"); + digitalWrite (ONBOARDLED, LOW); // Turn on LED + conFirstConnected = true; + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + Serial.printf ("Disconnected from SSID: %s\n", info.disconnected.ssid); + Serial.printf ("Reason: %d\n", info.disconnected.reason); + digitalWrite (ONBOARDLED, HIGH); // Turn off LED + //NTP.stop(); // NTP sync can be disabled to avoid sync errors + WiFi.reconnect (); + break; + #else + case WIFI_EVENT_STAMODE_CONNECTED: + Serial.printf ("Connected to %s. Asking for IP address.\r\n", WiFi.BSSIDstr().c_str()); + break; + case WIFI_EVENT_STAMODE_GOT_IP: + Serial.printf ("Got IP: %s\r\n", WiFi.localIP().toString().c_str ()); + Serial.printf ("Connected: %s\r\n", WiFi.status () == WL_CONNECTED ? "yes" : "no"); + digitalWrite (ONBOARDLED, LOW); // Turn on LED + conFirstConnected = true; + break; + case WIFI_EVENT_STAMODE_DISCONNECTED: + Serial.printf ("Disconnected from SSID: %s\n", WiFi.BSSIDstr ().c_str ()); + //Serial.printf ("Reason: %d\n", info.disconnected.reason); + digitalWrite (ONBOARDLED, HIGH); // Turn off LED + //NTP.stop(); // NTP sync can be disabled to avoid sync errors + WiFi.reconnect (); + break; + #endif + default: + break; + } + } + +#endif //!USE_ETHERNET void processSyncEvent (NTPEvent_t ntpEvent) { switch (ntpEvent.event) { @@ -92,9 +136,29 @@ void processSyncEvent (NTPEvent_t ntpEvent) { void setup() { Serial.begin (115200); - Serial.println (); - WiFi.mode (WIFI_STA); - WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD); + Serial.println ("\r\n"); + + #if (USE_ETHERNET) + SPI.begin(); + + eth.setDefault(); // use ethernet for default route + + #if (USE_STATIC) + eth.config(apIP, gwIP, snIP, dnIP1, dnIP2); + #endif + + eth.begin(); + + #else + WiFi.mode (WIFI_STA); + + #if (USE_STATIC) + WiFi.config(apIP, gwIP, snIP, dnIP1, dnIP2); + #endif + + WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD); + + #endif //USE_ETHERNET pinMode (ONBOARDLED, OUTPUT); // Onboard LED digitalWrite (ONBOARDLED, HIGH); // Switch off LED @@ -103,15 +167,21 @@ void setup() { ntpEvent = event; syncEventTriggered = true; }); - WiFi.onEvent (onWifiEvent); + + #if (USE_ETHERNET) + conLastStatus = !eth.connected (); + + #else + WiFi.onEvent (onWifiEvent); + #endif } void loop() { static int i = 0; static int last = 0; - if (wifiFirstConnected) { - wifiFirstConnected = false; + if (conFirstConnected) { + conFirstConnected = false; NTP.setTimeZone (TZ_Europe_Madrid); NTP.setInterval (600); NTP.setNTPTimeout (NTP_TIMEOUT); @@ -129,8 +199,49 @@ void loop() { last = millis (); Serial.print (i); Serial.print (" "); Serial.print (NTP.getTimeDateStringUs ()); Serial.print (" "); - Serial.print ("WiFi is "); - Serial.print (WiFi.isConnected () ? "connected" : "not connected"); Serial.print (". "); + + #if (USE_ETHERNET) + if(conLastStatus != eth.connected ()) + { + conLastStatus = eth.connected (); + + if(conLastStatus) + { + Serial.printf ("\r\nGot IP: %s\r\n", eth.localIP().toString().c_str ()); + + #if (USE_STATIC) + Serial.println ("Ethernet is connected [STATIC]"); + #else + Serial.println ("Ethernet is connected [DHCP]"); + #endif + + digitalWrite (ONBOARDLED, LOW); // Turn on LED + conFirstConnected = true; + } + else + { + #if (USE_STATIC) + Serial.println ("Ethernet is not connected [STATIC]"); + #else + Serial.println ("Ethernet is not connected [DHCP]"); + #endif + + digitalWrite (ONBOARDLED, HIGH); // Turn off LED + } + } + + #else + Serial.print ("WiFi is "); + Serial.print (WiFi.isConnected () ? "connected " : "not connected "); + + #if (USE_STATIC) + Serial.println ("[STATIC]"); + #else + Serial.println ("[DHCP]"); + #endif + + #endif //USE_ETHERNET + Serial.print ("Uptime: "); Serial.print (NTP.getUptimeString ()); Serial.print (" since "); Serial.println (NTP.getTimeDateString (NTP.getFirstSyncUs ())); @@ -138,4 +249,4 @@ void loop() { i++; } delay (0); -} \ No newline at end of file +} diff --git a/examples/basicExample/basicExample.ino b/examples/basicExample/basicExample.ino index f6355f6..e0fdbad 100644 --- a/examples/basicExample/basicExample.ino +++ b/examples/basicExample/basicExample.ino @@ -1,25 +1,85 @@ #include //#include "WifiConfig.h" +#define USE_ETHERNET false /* false: WiFi or true: lwIP_Etherner */ +#define USE_STATIC false /* false: DHCP or true: STATIC */ #include + #ifdef ESP32 -#include + #include + + #undef USE_ETHERNET + #define USE_ETHERNET false #else -#include -#endif + #include + + #if (USE_ETHERNET) + #include + #include + + #define ETH_SS_PIN 4 + ENC28J60lwIP eth(ETH_SS_PIN); + #endif +#endif //ESP32 #ifndef WIFI_CONFIG_H -#define YOUR_WIFI_SSID "YOUR_WIFI_SSID" -#define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD" + #define YOUR_WIFI_SSID "YOUR_WIFI_SSID" + #define YOUR_WIFI_PASSWD "YOUR_WIFI_PASSWD" + + // YOUR_ROUTER_SETTINGS + #if (USE_STATIC) + IPAddress apIP(192, 168, 1, 10); + IPAddress gwIP(192, 168, 1, 1); + IPAddress snIP(255, 255, 255, 0); + IPAddress dnIP1(192, 168, 1, 1); + IPAddress dnIP2( 8, 8, 8, 8); + #endif #endif // !WIFI_CONFIG_H #define SHOW_TIME_PERIOD 1000 +// weak functions to get connection status, reconnect and IP address of device +#if (USE_ETHERNET) + bool connectionStatus() { + return eth.connected (); + } + + bool connectionReconnect() { + return true; + } + + IPAddress getDeviceIP() { + return eth.localIP (); + } +#endif //USE_ETHERNET + void setup() { Serial.begin (115200); Serial.println (); - WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD); + + #if (USE_ETHERNET) + SPI.begin(); + + eth.setDefault(); // use ethernet for default route + + #if (USE_STATIC) + eth.config(apIP, gwIP, snIP, dnIP1, dnIP2); + #endif + + eth.begin(); + + #else + WiFi.mode (WIFI_STA); + + #if (USE_STATIC) + WiFi.config(apIP, gwIP, snIP, dnIP1, dnIP2); + #endif + + WiFi.begin (YOUR_WIFI_SSID, YOUR_WIFI_PASSWD); + + #endif //USE_ETHERNET + NTP.setTimeZone (TZ_Etc_UTC); NTP.begin (); } @@ -31,4 +91,4 @@ void loop() { last = millis (); Serial.println (NTP.getTimeDateStringUs ()); } -} \ No newline at end of file +} diff --git a/examples/ledFlasher/ledFlasher.ino b/examples/ledFlasher/ledFlasher.ino index 82046ff..e90f5a5 100644 --- a/examples/ledFlasher/ledFlasher.ino +++ b/examples/ledFlasher/ledFlasher.ino @@ -27,7 +27,6 @@ void processSyncEvent (NTPEvent_t ntpEvent) { Serial.printf ("[NTP-event] %s\n", NTP.ntpEvent2str(ntpEvent)); } - void setup () { Serial.begin (115200); Serial.println (); diff --git a/src/ESPNtpClient.cpp b/src/ESPNtpClient.cpp index 8a760e4..5e39f13 100644 --- a/src/ESPNtpClient.cpp +++ b/src/ESPNtpClient.cpp @@ -186,14 +186,14 @@ bool NTPClient::begin (const char* ntpServerName, bool manageWifi) { } DEBUGLOGI ("NTP socket created"); - if (WiFi.isConnected ()) { + if (connectionStatus ()) { ip_addr_t localAddress; #ifdef ESP32 - localAddress.u_addr.ip4.addr = WiFi.localIP (); + localAddress.u_addr.ip4.addr = getDeviceIP (); localAddress.type = IPADDR_TYPE_V4; DEBUGLOGI ("Bind UDP port %d to %s", DEFAULT_NTP_PORT, IPAddress (localAddress.u_addr.ip4.addr).toString ().c_str ()); #else - localAddress.addr = WiFi.localIP (); + localAddress.addr = getDeviceIP (); DEBUGLOGI ("Bind UDP port %d to %s", DEFAULT_NTP_PORT, IPAddress (localAddress.addr).toString ().c_str ()); #endif result = udp_bind (udp, /*IP_ADDR_ANY*/ &localAddress, DEFAULT_NTP_PORT); @@ -528,7 +528,7 @@ void NTPClient::s_getTimeloop (void* arg) { lastGotTime = ::millis (); DEBUGLOGI ("Periodic loop. Millis = %d", lastGotTime); if (self->isConnected) { - if (WiFi.isConnected ()) { + if (connectionStatus ()) { self->getTime (); } else { DEBUGLOGE ("DISCONNECTED"); @@ -540,7 +540,7 @@ void NTPClient::s_getTimeloop (void* arg) { self->isConnected = false; } } else { - if (WiFi.isConnected ()) { + if (connectionStatus ()) { DEBUGLOGD ("CONNECTED. Binding"); if (self->udp) { udp_disconnect (self->udp); @@ -556,10 +556,10 @@ void NTPClient::s_getTimeloop (void* arg) { ip_addr_t localAddress; #ifdef ESP32 - localAddress.u_addr.ip4.addr = WiFi.localIP (); + localAddress.u_addr.ip4.addr = getDeviceIP (); localAddress.type = IPADDR_TYPE_V4; #else // ESP8266 - localAddress.addr = WiFi.localIP (); + localAddress.addr = getDeviceIP (); #endif // ESP32 err_t result = udp_bind (self->udp, /*IP_ADDR_ANY*/ &localAddress, DEFAULT_NTP_PORT); DEBUGLOGI ("Bind UDP port"); @@ -617,7 +617,7 @@ void NTPClient::getTime () { dnsErrors = 0; if (manageWifi) { DEBUGLOGW ("Reconnecting WiFi"); - WiFi.reconnect (); + connectionReconnect (); } } return; @@ -1201,3 +1201,22 @@ char* NTPClient::ntpEvent2str (NTPEvent_t e) { return result; } + +/// weak functions to get connection status, reconnect and IP address of device +extern "C" +{ + bool connectionStatus() __attribute__ ((weak, alias("__connectionStatus"))); + bool __connectionStatus() { + return WiFi.isConnected (); + } + + bool connectionReconnect() __attribute__ ((weak, alias("__connectionReconnect"))); + bool __connectionReconnect() { + return WiFi.reconnect (); + } + + IPAddress getDeviceIP() __attribute__ ((weak, alias("__getDeviceIP"))); + IPAddress __getDeviceIP() { + return WiFi.localIP (); + } +} diff --git a/src/ESPNtpClient.h b/src/ESPNtpClient.h index c8012ec..ff9e088 100644 --- a/src/ESPNtpClient.h +++ b/src/ESPNtpClient.h @@ -236,6 +236,14 @@ typedef std::function onSyncEvent_t; ///< @brief Event notifi static char strBuffer[35]; ///< @brief Temporary buffer for time and date strings +/// weak functions to get connection status, reconnect and IP address of device +extern "C" +{ + bool connectionStatus(); + bool connectionReconnect(); + IPAddress getDeviceIP(); +} + /** * @brief NTPClient class */