2018/05/14

ESP8266 - Assigning a fixed IP address

This post is about how to set fixed IP for ESP8266 and how to check the result.

Arduino Sketch

The sketch used is the "AutoConnectWithStaticIP" sketch that comes with the WiFiManager library. Be sure to select the right block (block1 or block2) according to the version of the ESP8266 core installed on your computer.

For my case, I have version 2.4.1 installed, so I used block1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <FS.h>                   //this needs to be first, or it all crashes and burns...

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

/**************************************************************************************
 * this example shows how to set a static IP configuration for the ESP
 * although the IP shows in the config portal, the changes will revert 
 * to the IP set in the source file.
 * if you want the ability to configure and persist the new IP configuration
 * look at the FS examples, which save the config to file
 *************************************************************************************/

//default custom static IP, for use with block1
char static_ip[16] = "192.168.2.20";
char static_gw[16] = "192.168.2.1";
char static_sn[16] = "255.255.255.0";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();

  //WiFiManager
  //Local intialization. Once its business is done, there is no need to keep it around
  WiFiManager wifiManager;

  //reset settings - for testing
  //wifiManager.resetSettings();

  //set static ip
  //block1 should be used for ESP8266 core 2.1.0 or newer, otherwise use block2

  //start-block1
  IPAddress _ip,_gw,_sn;
  _ip.fromString(static_ip);
  _gw.fromString(static_gw);
  _sn.fromString(static_sn);
  //end-block1

  //start-block2
  //IPAddress _ip = IPAddress(10, 0, 1, 78);
  //IPAddress _gw = IPAddress(10, 0, 1, 1);
  //IPAddress _sn = IPAddress(255, 255, 255, 0);
  //end-block2
  
  wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn);

  //tries to connect to last known settings
  //if it does not connect it starts an access point with the specified name
  //here  "AutoConnectAP" with password "password"
  //and goes into a blocking loop awaiting configuration
  
  if (!wifiManager.autoConnect("SSID", "Password")) {
    Serial.println("failed to connect, we should reset as see if it connects");
    delay(3000);
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println("connected...yeey :)");

  Serial.println("local ip");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:
}

After uploading the sketch to ESP8266 NodeMCU module, I opened Serial Monitor to check for the connection status.


The interesting thing is, even though the info. in the Serial Monitor shows the ESP8266 is connected to the router and has 192.168.2.20 as its IP address, I cannot find the device in my router's list of connected devices. Only those connected via DHCP are shown.


Luckily, I have Fling installed on my cellphone, and it's clear that an Espressif device is connected to the router with 192.168.2.20 as its IP address.


My laptop could ping the ESP8266 at 192.168.2.20.


No comments:

Post a Comment