The use of the ESP8266 in the world of IoT

User avatar
By xuanduc611
#70097 Hi guys,

I'm making an application which needs to assign new static IP any time user wants. So I need to put that task in loop() function aka I don't config IP address in setup(). Normally, examples on the Internet put the WiFi.config() in setup().

I'm planning to store assigned static IP in EEPROM and read it in setup() function to config static IP. So is there another way to make the module boot with assigned static IP without storing it into EEPROM?

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "Sales.SMATEC";
const char* password = "0904838283";

IPAddress g_ip_addr(192, 168, 10, 95);
IPAddress g_gateway(192, 168, 10, 1);
IPAddress g_subnet(255, 255, 255, 0);
//IPAddress g_dns(192.168.1.1);

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
    WiFi.config(g_ip_addr, g_gateway, g_subnet);
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 1;

void loop()
{
    if (value) {
        value = 0;
        WiFi.config(g_ip_addr, g_gateway, g_subnet);
    }
    delay(100);
    Serial.println(WiFi.localIP());
}



Output:
Code: Select allConnecting to Sales.SMATEC
..
WiFi connected
IP address:
192.168.10.51 // Module's IP addr at startup and I need it boot with 192.168.10.95 without accessing EEPROM.
192.168.10.95 // Module's IP addr after execute WiFi.config().
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95
192.168.10.95