User Tools

Site Tools


esp8266:esp8266-setting-static-ip

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
esp8266:esp8266-setting-static-ip [2019/10/11 02:43]
Ilias Iliopoulos [Connecting to a Wireless Access Point]
esp8266:esp8266-setting-static-ip [2024/02/02 21:49] (current)
Ilias Iliopoulos
Line 11: Line 11:
  
 ===== Connecting to a Wireless Access Point ===== ===== Connecting to a Wireless Access Point =====
-The process of connecting to an Access Point is not something that happens once in the lifetime of a project. Most examples found on-line show a few lines of code in the ''​setup()''​ section. But in real life, an Access Point may stop functioning due to a power-down, or even be replaced. Our client must try to connect in any case it is not connected. As a result, the connection code must reside in the ''​loop()''​ section instead of the ''​setup()''​. This is the typical approach for any connection-oriented communication methods.+The process of connecting to an Access Point is not something that happens once in the lifetime of a project. Most examples found on-line show a few lines of code in the ''​setup()''​ section. But in real life, an Access Point may stop functioning due to a power-down, or even be replaced. Our client must try to connect in any case it is not connected. ​
  
-In addition, the micro-controller may have other tasks to perform besides the WiFi interface. ​I prefer to have complete ​control ​over them.+ESP8266 is equipped with several functions that take place in the backgroundassisting in automating the WiFi re-connection. But we should not forget that sometimes ​the micro-controller may have other tasks to perform besides the WiFi interface. ​Especially in cases where actions must be performed with strict timing requirements,​ it is obvious that we should maintain ​control ​at all times, both at the WiFi connection and re-connection code, as well as the rest of our software
  
-My connection code is in a function called ''​connectWiFi()''​ which is called ​in the ''​loop()''​ section ​when there is no WiFi connection. One could also experiment with the ''​WiFi.isConnected()''​ function ​instead of the ''​WiFi.status()'' ​and let me know the resultThe ''​ethernetReceive()'' ​is my function which checks if there is an incoming packet and process it. I will discuss about the ''​connectWiFi()''​ below. For this method to work, we must instruct the libraries to avoid automatic ​connection ​and re-connection. See below.+To cope with this issue, my approach disables the automatic re-connection functions of the ESP8266 and does everything manually. As a result, the connection code must reside ​in the ''​loop()''​ section instead of the ''​setup()''​. ​This is the typical approach for any connection-oriented communication methods.
  
-<​code>​+My connection code is in a function called ''​connectWiFi()''​ which is called in the ''​loop()''​ section when there is no WiFi connection, or when I choose to do so. One could also experiment with the ''​WiFi.isConnected()''​ function instead of the ''​WiFi.status()''​ and let me know the result. The ''​ethernetReceive()''​ is my function which checks if there is an incoming packet and process it. I will discuss about the ''​connectWiFi()''​ below. For this method to work, we must instruct the libraries to avoid automatic connection and re-connection. See [[esp8266:​esp8266-setting-static-ip#​WiFi persistence and auto-connection|below]]. 
 + 
 +The example below is a bit simplistic but it conveys the idea. 
 + 
 +<​code ​c++>
 void loop() { void loop() {
  
Line 23: Line 27:
     ethernetReceive();​ // Function to process incoming packets     ethernetReceive();​ // Function to process incoming packets
   } else {   } else {
-    ​connectWiFi();​+      if (i_want_to_do_so && nothing_more_important_to_do) { 
 +        ​connectWiFi();​ 
 +      }  ​
   }    } 
   ​   ​
Line 47: Line 53:
 See [[https://​arduino-esp8266.readthedocs.io/​en/​latest/​esp8266wifi/​generic-class.html|Generic Class - persistent]] for details regarding this feature. In addition, we might have an additional reason to disable this feature for projects that are intended to operate 24x7, due to this: See [[https://​arduino-esp8266.readthedocs.io/​en/​latest/​esp8266wifi/​generic-class.html|Generic Class - persistent]] for details regarding this feature. In addition, we might have an additional reason to disable this feature for projects that are intended to operate 24x7, due to this:
  
-<​code>​ +  Quote: 
-Frequently calling these functions could cause wear on the flash memory... +  "​Frequently calling these functions could cause wear on the flash memory..." 
-</​code>​+
  
 As a result, I decide to set: As a result, I decide to set:
  
-<​code>​+<​code ​c++>
 WiFi.persistent(false);​ WiFi.persistent(false);​
 WiFi.setAutoConnect(false);​ WiFi.setAutoConnect(false);​
 +WiFi.setAutoReconnect(false);​
 </​code>​ </​code>​
  
Line 62: Line 69:
 Library function ''​WiFi.config()''​ is supposed to disable the ESP8266 DHCP client and set the IP configuration of station interface to user defined arbitrary values. The syntax, as shown [[https://​arduino-esp8266.readthedocs.io/​en/​latest/​esp8266wifi/​station-class.html#​config|here]] is: Library function ''​WiFi.config()''​ is supposed to disable the ESP8266 DHCP client and set the IP configuration of station interface to user defined arbitrary values. The syntax, as shown [[https://​arduino-esp8266.readthedocs.io/​en/​latest/​esp8266wifi/​station-class.html#​config|here]] is:
  
-<​code>​+<​code ​c++>
 WiFi.config(local_ip,​ gateway, subnet, dns1, dns2) WiFi.config(local_ip,​ gateway, subnet, dns1, dns2)
 </​code>​ </​code>​
Line 68: Line 75:
 Please note the order of the arguments which differs from the syntax of the [[https://​www.arduino.cc/​en/​Reference/​WiFiConfig|Arduino WiFi library]], which is: Please note the order of the arguments which differs from the syntax of the [[https://​www.arduino.cc/​en/​Reference/​WiFiConfig|Arduino WiFi library]], which is:
  
-<​code>​+<​code ​c++>
 WiFi.config(ip,​ dns, gateway, subnet); WiFi.config(ip,​ dns, gateway, subnet);
 </​code> ​ </​code> ​
Line 74: Line 81:
 The reason that the designers of the ESP8266 core classes decided on a different argument order is beyond my understanding! Anyway, there is something tricky about ''​WiFi.config()''​ that I have never seen it mentioned in any online article: The reason that the designers of the ESP8266 core classes decided on a different argument order is beyond my understanding! Anyway, there is something tricky about ''​WiFi.config()''​ that I have never seen it mentioned in any online article:
  
-<​code>​ + 
-Function will return true if configuration change is applied successfully. +  Function will return true if configuration change is applied successfully. 
-If configuration can not be applied, because e.g. module is not in station  +  If configuration can not be applied, because e.g. module is not in station  
-or station + soft access point mode, then false will be returned. +  or station + soft access point mode, then false will be returned. 
-</​code>​+
  
 This tells us that sometimes the configuration change may not be applied successfully. The documentation does not provide an exhaustive list of all reasons that may result to this situation. Therefore, our code must check for the return value. In the example code below, it is assumed that if the project is not able to communicate,​ there is no reason to continue, so it re-tries until the configuration change is applied. No DNS server is set in this snippet. Other projects may exit, perform some other activities and try to connect at time intervals. This tells us that sometimes the configuration change may not be applied successfully. The documentation does not provide an exhaustive list of all reasons that may result to this situation. Therefore, our code must check for the return value. In the example code below, it is assumed that if the project is not able to communicate,​ there is no reason to continue, so it re-tries until the configuration change is applied. No DNS server is set in this snippet. Other projects may exit, perform some other activities and try to connect at time intervals.
  
-<​code>​+<​code ​c++>
   while (!WiFi.config(localIPaddress,​ gateway, subnetMask)) {   while (!WiFi.config(localIPaddress,​ gateway, subnetMask)) {
     delay(500);     delay(500);
Line 93: Line 100:
 Make sure that the IP addresses provided to ''​WiFi.config()''​ are proper IP addresses. In my experiments,​ it happened that instead of a gateway IP address, the code submitted the subnet mask 255.255.255.0. This is considered as an error by ''​WiFi.config()''​ and if the function is not inserted in the error-checking loop shown above, the ESP8266 will activate the DHCP client and you will try desperately to figure out what has happened!!! Make sure that the IP addresses provided to ''​WiFi.config()''​ are proper IP addresses. In my experiments,​ it happened that instead of a gateway IP address, the code submitted the subnet mask 255.255.255.0. This is considered as an error by ''​WiFi.config()''​ and if the function is not inserted in the error-checking loop shown above, the ESP8266 will activate the DHCP client and you will try desperately to figure out what has happened!!!
  
-===== Final connectWiFi() function ===== +===== Example ​connectWiFi() function ===== 
-<​code>​+<​code ​c++>
  
 #define WIFI_RECONNECT_TIMER 500 #define WIFI_RECONNECT_TIMER 500
Line 118: Line 125:
   WiFi.persistent(false);​   WiFi.persistent(false);​
   WiFi.setAutoConnect(false);​   WiFi.setAutoConnect(false);​
 +  WiFi.setAutoReconnect(false);​
  
   while (!WiFi.config(staticIP,​ gateway, subnetMask)) {   while (!WiFi.config(staticIP,​ gateway, subnetMask)) {
Line 148: Line 156:
  
  
 +~~DISQUS~~
   ​   ​
  
  
esp8266/esp8266-setting-static-ip.1570751008.txt.gz · Last modified: 2019/10/11 02:43 by Ilias Iliopoulos