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
Next revision Both sides next revision
esp8266:esp8266-setting-static-ip [2019/10/11 02:43]
Ilias Iliopoulos [Connecting to a Wireless Access Point]
esp8266:esp8266-setting-static-ip [2019/10/11 02:54]
Ilias Iliopoulos Changed title
Line 13: Line 13:
 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. 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.
  
-In addition, the micro-controller may have other tasks to perform besides the WiFi interface. I prefer to have complete control over them.+In addition, the micro-controller may have other tasks to perform besides the WiFi interface. I prefer to have complete control over them instead of relying on the ESP8266 Arduino core libraries, especially when my project needs to control items, or to handle critical timing.
  
-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 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 below.+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>​ <​code>​
Line 23: Line 25:
     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 56: Line 60:
 WiFi.persistent(false);​ WiFi.persistent(false);​
 WiFi.setAutoConnect(false);​ WiFi.setAutoConnect(false);​
 +WiFi.setAutoReconnect(false);​
 </​code>​ </​code>​
  
Line 93: Line 98:
 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>​
  
Line 118: Line 123:
   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)) {
esp8266/esp8266-setting-static-ip.txt · Last modified: 2024/02/02 21:49 by Ilias Iliopoulos