Setting StaticIP address on ESP8266

Hi. Using the Blynk.begin command can you set your Wemos or ESP8266 to a static IP address (instead of using DHCP every time) so that when it powers up it connects faster ? I can’t seem to find any info on this.

Try the Manual Connection example. It’s written for Ethernet, but same applies to Wifi too :slight_smile:

@mars

In order to set static IP Address into ESP8266 you have to send it the following AT Command:

AT+CIPSTA=“ip module”,“ip gateway”,“subnet mask”

e.g.: AT+CIPSTA=“192.168.1.160”,“192.168.1.1”,“255.255.255.0”

where: .160 is the address of the ESP8266 and .1 is the address of the router.

You can send this command to the ESP 8266 just once.

The program the I use to send commands to ESP8266 is:

#include <SoftwareSerial.h>

#define RX 2  // Arduino RX line on pin D2
#define TX 3  // Arduino TX line on pin D3

SoftwareSerial esp_8266(RX, TX);

void setup()
{
  Serial.begin(9600);
  esp_8266.begin(9600);
}

void loop()
{
  while (Serial.available())
  {
    esp_8266.write(Serial.read());
  }

  while (esp_8266.available())
  {
    Serial.write(esp_8266.read());
  }
}

The command are placed on the top bar of the Serial Monitor of the IDE of Arduino. The Serial Monitor MUST be placed in Both (NL & CR) and the baud rate of the program must be the same of the baud rate of ESP. Try with a simple AT command to verify if all is OK. The ESP must respond with an OK.
The module is connected to a soft serial (pins 2 and 3).
Hope this can be useful.
Ciao, Giancarlo

1 Like

Subtle difference, the AT commands are for when the it’s being used as shield, if you use it with the IDE as standalone, you need to adjust your sketch :slight_smile:

1 Like

Correct !!
I forgot to put in evidence that I use the ESP as a shield !!

thanks guys - so you can setup static IP address and attributes as per your program ONCE then ESP8266 remembers them even if powered off ? With respect to code example,if I have a Wemos then I change serial to RX 1, and 2 respectively ?

if you only using a Wemos i think you should check the manual Ethernet sketch @Lichtsignaal suggested…

got it thanks all :slight_smile:

@mars Yes, the ESP stores the last received setting (e. g. static IP) in its non volatile memory so it remembers even if powerd off. Regarding the serial lines, you can use the most suitable lines for your hardware.
Ciao,
Giancarlo

Hi - I tried to modify the blynk Ethernet sketch for setting static ip for esp8266 but can’t seem to get it to work. Anyone done it successfully without using AT commands ie doing it with blynk.begin api ? If so can you pls share

@mars

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h> 

char auth[] = "xxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char password[] = "xxxxxxxxxxxx";

IPAddress server_ip (192, 168, 10, 229);

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress arduino_ip ( 192,  168,   10,  20);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,  168,   10,   90);
IPAddress subnet_mask(255, 255, 255,   0);

void setup()
{
  Serial.begin(115200);
  Serial.println();
  WiFi.config(arduino_ip, gateway_ip, subnet_mask);
  WiFi.begin(ssid, password);
  Blynk.config(auth);
  while (Blynk.connect(1000) == false) { 
  }  
}

void loop()
{
  Blynk.run();
}

thanks Costas - this works, appreciate that.

I assume doing it this way does NOT keep the static IP address within the ESP8266 even when switched off, i.e. I need to setup staticIP eachtime Blynk runs ?
Unlike in the case with using AT commands the ESP8266 remembers the StaticIP settings even when switched off.

Is this correct ?

To be honest I’m not really sure if the IP is saved in none volatile memory like the ssid and pwd. If you are running the sketch then it will always have the same IP when power is restored.

Question on a totally different topic - but I know you understand a lot about given historical posts. I am using the RTC widget in my code and its all compiles fine etc and displays formatted time and date in LCD widget but it always start with time 0.0.0 and date 1.1.1970 - even though I select correct timezone in widget. I am using a local Blynk server and wonder if there is anything special or different I need to do because I am using a local Blynk server ?

It is because the ESP8266 clock hasn’t been sync’d with the server. The default is for the sync to be at 5 minute intervals.

The RTC widget uses this library https://github.com/PaulStoffregen/Time and you can change the sync interval with setSyncInterval( interval), where interval is some seconds.

I have found it doesn’t always sync though if you have a lot of virtualWrite’s and other pin syncing when the ESP boots up.

Extract of a fix below that uses a SimpleTimer every 1 second to try and sync the time, once the time is sync’d the SimpleTimer is deleted.

int clockcounter = 3; // just happens to be our 3rd timer but must be set as an integer to be able to disable the timer

void gettime(){
    if(year() != 1970){  // we have got the right time now so kill the SimpleTimer
      setSyncInterval(300); // normal 5 minute sync interval but not sure this actually works
      timer.deleteTimer(clockcounter);  // don't need this timer anymore
      sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
      Serial.println(Date);
      sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
      Serial.println(Time);
    }
    rtc.begin();  // keep trying to sync the time until year is not 1970
}

void setup(){

clockcounter = timer.setInterval(1000L, gettime); // check if clock has updated every second

}

Edit:
I should have added that you must include the following line in loop()

timer.run(); // plus include required libraries at the top of your sketch

Top of your sketch:

#include <TimeLib.h>
#include <SimpleTimer.h>
SimpleTimer timer;

And a “perfect” loop is:

void loop()
{
  Blynk.run();
  timer.run();  // Costas
}

giving it a try now - been going for 5 mins and still date is 1.1.1970 :frowning:
can it take a long time usually ?

It takes just a few seconds for the sync to take place on our system.

Maybe I should give you a full sketch to try.

Just a thought, is your local server something like a Pi that has been restricted to LAN routing?

hi - i’m running local server on windows 10 pc.
I realized for some reason the interrupt function was not working so I created a little flag checking function which then worked - but then i realized only GMT+11 is available from RTC widget on Blyk App and it should be GMT+10 for Australia/Sydney — I’ve raised another post issue on it earlier.

If you can share the entire script that would be great.

See below: the time in LCD widget is 19:26 which is GMT+11, but the time in the history graph is GMT+10 ! really weird – I don’t know why.

@mars Hi Mars, I report you a note that I wrote in a former post of some days ago:

… the RTC shows 1/1/1970 for the first 5 minutes if you miss this line of code:

while(Blynk.connect() == false);

equivalent to:

while(Blynk.connect() == false)
{
}

This line has to be inserted after Blynk.begin and before rtc.begin:

Blynk.begin(auth, wifi, ssid, pass);

while(Blynk.connect() == false);

rtc.begin();

In such a way the RTC hows immediately the correcet date and time.

Regarding the code to format date and time, in my opinion, the most elegant lines of code are:

sprintf(Date, "%04d/%02d/%02d", year(), month(), day());
sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());

Blynk.virtualWrite(V1, Date);
Blynk.virtualWrite(V2, Time);

If you don’t want leading zero, exclude the 0 from the formatting section of sprintf.

Date and Time are array of char:

char Date[16];
char Time[16];

Regarding the static IP Address of ESP8266, I have verified that if you assign it with AT commands it is stored in the non volatile memory of ESP.

I hope it could be useful.
Ciao,
Giancarlo