esp8266:esp8266-setting-static-ip
Differences
This shows you the differences between two versions of the page.
Previous revision | |||
— | esp8266:esp8266-setting-static-ip [2024/11/22 12:02] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Setting and maintaining a static IP address to ESP8266 ====== | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | The main reason for writing this article is the frustration that I experienced when I tried to configure an ESP8266 with a static IP address, as required by one of my projects. Although the first time after flashing the program, the IP address was set properly, then after a restart, the system presented an erratic behaviour. Sometimes the DHCP client was activated and a dynamic IP address was acquired by the DHCP server, while other times, the static IP address was set properly. This unstable behaviour made my head spin and caused me to search deeply under the hood. | ||
+ | |||
+ | Unfortunately, | ||
+ | |||
+ | I still have my doubts regarding the " | ||
+ | |||
+ | The article presents issues that need to be considered when setting a static IP address to your ESP8266 project which connects to a Wireless Access Point in STA mode . | ||
+ | |||
+ | ===== 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 '' | ||
+ | |||
+ | ESP8266 is equipped with several functions that take place in the background, assisting 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, | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | My connection code is in a function called '' | ||
+ | |||
+ | The example below is a bit simplistic but it conveys the idea. | ||
+ | |||
+ | <code c++> | ||
+ | void loop() { | ||
+ | |||
+ | if (WiFi.status() == WL_CONNECTED) { | ||
+ | ethernetReceive(); | ||
+ | } else { | ||
+ | if (i_want_to_do_so && nothing_more_important_to_do) { | ||
+ | connectWiFi(); | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | //... other program code | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Arduino IDE settings ===== | ||
+ | |||
+ | ESP8266 firmware is designed to store the configuration data of the last achieved connection in its flash memory. Libraries may use this information to re-connect after a temporary failure. I have not been able to understand exactly how this is performed and under which conditions, so I decided to take things in my own hands. | ||
+ | |||
+ | In the '' | ||
+ | |||
+ | * Only Sketch | ||
+ | * Sketch and WiFi Settings | ||
+ | * All flash Contents | ||
+ | |||
+ | When programming a new program and in order to erase any data from previous programs, I select '' | ||
+ | |||
+ | ===== WiFi persistence and auto-connection ===== | ||
+ | |||
+ | See [[https:// | ||
+ | |||
+ | Quote: | ||
+ | " | ||
+ | |||
+ | |||
+ | As a result, I decide to set: | ||
+ | |||
+ | <code c++> | ||
+ | WiFi.persistent(false); | ||
+ | WiFi.setAutoConnect(false); | ||
+ | WiFi.setAutoReconnect(false); | ||
+ | </ | ||
+ | |||
+ | ===== DHCP client disable ===== | ||
+ | |||
+ | Library function '' | ||
+ | |||
+ | <code c++> | ||
+ | WiFi.config(local_ip, | ||
+ | </ | ||
+ | |||
+ | Please note the order of the arguments which differs from the syntax of the [[https:// | ||
+ | |||
+ | <code c++> | ||
+ | WiFi.config(ip, | ||
+ | </ | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | |||
+ | Function will return true if configuration change is applied successfully. | ||
+ | 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. | ||
+ | |||
+ | |||
+ | 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, | ||
+ | |||
+ | <code c++> | ||
+ | while (!WiFi.config(localIPaddress, | ||
+ | delay(500); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Perhaps, other actions need to be taken after several attempts, such as a restart. It was not necessary in any of my projects. If you find any additional information, | ||
+ | |||
+ | ===== Configuration data ===== | ||
+ | Make sure that the IP addresses provided to '' | ||
+ | |||
+ | ===== Example connectWiFi() function ===== | ||
+ | <code c++> | ||
+ | |||
+ | #define WIFI_RECONNECT_TIMER 500 | ||
+ | |||
+ | const char* wifi_ssid = " | ||
+ | const char* wifi_password = " | ||
+ | |||
+ | IPAddress staticIP(192, | ||
+ | IPAddress gateway(192, | ||
+ | IPAddress subnetMask(255, | ||
+ | |||
+ | |||
+ | void connectWiFi(void) { | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(configuration.wifi_ssid); | ||
+ | Serial.print("'" | ||
+ | Serial.println(); | ||
+ | |||
+ | // The following did not appear in the Station Class documentation. Perhaps it is the default mode | ||
+ | // | ||
+ | |||
+ | WiFi.persistent(false); | ||
+ | WiFi.setAutoConnect(false); | ||
+ | WiFi.setAutoReconnect(false); | ||
+ | |||
+ | while (!WiFi.config(staticIP, | ||
+ | delay(500); | ||
+ | } | ||
+ | |||
+ | WiFi.begin(wifi_ssid, | ||
+ | |||
+ | int i = 0; | ||
+ | while (WiFi.status() != WL_CONNECTED) { | ||
+ | Serial.print(" | ||
+ | // avoid long lines | ||
+ | if (++i > 80) { | ||
+ | Serial.println(); | ||
+ | i = 0; | ||
+ | } | ||
+ | delay(WIFI_RECONNECT_TIMER); | ||
+ | } | ||
+ | Serial.println(); | ||
+ | Serial.print(" | ||
+ | Serial.print(wifi_ssid); | ||
+ | Serial.println(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Conclusion ===== | ||
+ | There is a huge amount of information in the ESP8266 documentation. Unfortunately, | ||
+ | |||
+ | Perhaps the information in this article is not complete, or contains errors or does not apply in all conditions and with all ESP8266 modules. If you would like to share your experiences, | ||
+ | |||
+ | |||
+ | ~~DISQUS~~ | ||
+ | | ||
+ | |||