Setting StaticIP address on ESP8266

Tried your code and work: output was…GOOD (for both local server and Blynk cloud server) :slight_smile:

[21071] Blynk v0.3.8 on Arduino
[21071] Blynk v0.3.8 on Arduino
WiFi connected with IP address:
[21072] Connecting to
[21911] Ready (ping: 0ms).
Connected to Server
[23395] Time sync: OK
Date is: 04/09/2016 and the time is now 22:10:47
[24394] Time sync: OK
1 Like

@mars why is it taking 22 seconds to connect and be sure to read the edit in my last post.

Just tweaking your sketch.

sorry Costas - not sure how you deduced the 22 seconds ?

I have read you comments but I guess I’m failing to understand how the loop states are working i.e. I was thinking rtc.begin was a synchronous function but sounds like it is asynchronous i.e. does not wait for the sync to complete or fail

[21071] Blynk v0.3.8 on Arduino the {21071] is just over 21 seconds, no?

I thought [21071] to [21911] is < 1 sec ?

@mars try this sketch and understand the changes marked with Costas (Serial Monitor confirms time is sync’d within about 200ms by having 200L timer):

Dallas Temperature IC Control Library Demo
Locating devices...Found 0 devices.
Resolution set to:9
[767] Blynk v0.3.8 on ESP8266
[5001] Connecting to
[5138] Ready (ping: 12ms).
Blynk First connected EXECUTED
BLYNK is Connected!
Year in gettime = 1970
still trying to sync :-(
[5360] Time sync: OK
Year in gettime = 2016
Requesting temperatures...

// marsmodified.ino Costas 4 Sept 2016

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <OneWire.h> // make sure used ESP OneWire.h file in ESP folder
#include <DallasTemperature.h>
#include <TimeLib.h>
#include <SimpleTimer.h>

char Date[16];
char Time[16];
char ssid[] = "MTN WIFI 19996";               // Costas
char pass[] = "xxxxxxxxx";                 // Costas

#define ONE_WIRE_BUS 13   // GPIO13 = onewire to use PIN 7 on WeMos D1 R2 ESP8266 board

#define RELAY1  13  // D7
#define RELAY2  12  // D6                       
#define RELAY3  14  // D5
#define RELAY4  4   // D4
DeviceAddress Therm_1 = { 0x28, 0xff, 0x61, 0xd0, 0x64, 0x15, 0x03, 0x5f };
DeviceAddress Therm_2 = { 0x28, 0xFF, 0x2D, 0xD4, 0x64, 0x15, 0x01, 0x25 };
DeviceAddress Therm_3 = { 0x28, 0xFF, 0x18, 0xE7, 0x64, 0x15, 0x01, 0xB3 };
DeviceAddress Therm_4 = { 0x28, 0xFF, 0x70, 0xF4, 0x64, 0x15, 0x02, 0xCE };
DeviceAddress Therm_5 = { 0x28, 0xFF, 0xB0, 0xAB, 0x64, 0x15, 0x01, 0x4C };

int rainSensePin= 0;    // analog pin 0 - sensor  i/p
long BaudRate = 9600, sysTick = 0;
char GotChar;

SimpleTimer timer;

int clockcounter = 3;
bool clock_sync = 0;

WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V8);   // Costas

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int numberOfDevices; // Number of temperature devices found

DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

// Time to sleep (in seconds):
const int sleepTimeS = 10;
bool V1_written = 0;

//char auth[] = "xxxxxxxxxxxx"; // Costas Blynk Server
char auth[] = "xxxxxxxxxxxxx"; // Costas Pi Zero Server

           // ESP  token for Wemos D1 Project TEMP on LOCAL SERVER 
           // Blynk token for ESP8266 Project on LOCAL SERVER

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

void setup()
  pinMode (13, INPUT_PULLUP); // set GPIO13 Onewire with Internal PULL_UP resistor on for it to work
  //Serial.begin(9600);    // Costas
  Serial.begin(115200);    // Costas
  if(oneWire.reset()==1) { Serial.println ("OneWire has been reset correctly");}
  else { Serial.println ("One wire reset was UNSUCCESSFUL!");}

  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();

  // locate devices on the bus
  Serial.print("Locating devices...");

  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
  Serial.print("Resolution set to:");
  // sensors.setResolution(TEMPERATURE_PRECISION);

   WiFi.config(arduino_ip, gateway_ip, subnet_mask);
   //WiFi.begin("xxxx", "xxxx");                     // Costas
   WiFi.begin(ssid, pass);                           // Costas
   //Blynk.config(auth,IPAddress(xxx,xxx,xxx,xxx));  // Costas
   while (WiFi.status() != WL_CONNECTED) {           // Costas
      delay(500);                                    // Costas
      Serial.print(".");                             // Costas
   }                                                 // Costas
   Serial.println();                                 // Costas
   Blynk.config(auth, "");                               // Costas to Pi Zero local server
   //Blynk.config(auth, "");                               // Costas to Blynk cloud server                      
  // Or like this:
  //Blynk.begin(auth, "", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  while (Blynk.connect() == false) {    // wait for Blynk to be connected
  //rtc.begin();  // Costas
  Serial.println ("BLYNK is Connected!");

   //  Blynk.syncAll();   // Real all virtualpins   
   //   clockcounter = timer.setInterval (300L, gettime); // check if clock has updated every second
   clockcounter = timer.setInterval (200L, gettime); // check if clock has updated every 0.2 seconds  Costas
   //Serial.print ("Clockcounter = ");
   // Serial.println (clockcounter);
   timer.setInterval(2000L,tempclock);  // get temp & show clock every 2s, can be much less than 2s Costas

void tempclock(){   // Costas
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.println("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
 // printTemperature(Therm_1, 1);
 // printTemperature(Therm_2, 2);
 // printTemperature(Therm_3, 3);
 // printTemperature(Therm_4, 4);
 printTemperature(Therm_5, 5);
    // deepSleep time is defined in microseconds. Multiply
   // seconds by 1e6 
   // Serial.println ("I'm going to sleep");
     //if (clock_sync == 0) { gettime();}  // Costas
  //  ESP.deepSleep(sleepTimeS * 1000000); not using this for now
    Serial.println ("I'm awake");
    //delay (1000);  // Costas  

void loop()
{;;  // Costas

 // BLYNK_LOG("Got a value: %s", param.asStr());
  // You can also use:
 int i = param.asInt();
 int state;

 float tempC = sensors.getTempC(Therm_5);
 Serial.print ("Temp C = ");

 V1_written = 1;  // my little flag

bool isFirstConnect = true;

  if (isFirstConnect) {
  //  Blynk.syncAll();   // not doing much for now
    isFirstConnect = false;
    Serial.println ("Blynk First connected EXECUTED");   

// function to print the temperature for a device to Blynk app
void printTemperature(DeviceAddress deviceAddress, int thermostat_label)
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit

  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  Serial.print("Thermostat : ");
  Serial.print("Temp C: ");
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  Serial.println("...temperature read ok..... pls wait"); // Costas
  Serial.println(" ");


// Digital clock display of the time in Blynk App

void clockDisplay()
  // You can call hour(), minute(), ... at any time
  // Please see Time library examples for details

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

  // Send time to the App
  Blynk.virtualWrite(V2, Time);
  // Send date to the App
  Blynk.virtualWrite(V3, Date);

void gettime(){
    Serial.print ("Year in gettime = ");
    Serial.println (year());
    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  Costas
      sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
      sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
      clock_sync = 1;         // set clock_sync flag to TRUE so we don't call gettime function again from void loop ()
    else {
      Serial.println ("still trying to sync :-(");
      rtc.begin();  // keep trying to sync the time until year is not 1970

Yes but why the initial 21071? As you will see I have an initial 767 for Blynk to be called (less than 1s) and connected by 5138 (5s).

good question - its instant from my serial monitor, certainly not waiting 21 seconds.

OK I think I know what it is, this will include your compiling time.
A normal ESP reset should show the correct details.

Edit: Maybe not, doesn’t give any longer times on my system when compiling.

Is there a hybrid system whereby you can still use AT commands from Espressif SDK whilst using ESP’s in standalone mode via the Arduino IDE?

That’s not possible. It’s assigned via the Sketch if it’s running standalone. It has nothing to do with the AT set because as soon as you upload a Sketch the AT firmware is deleted.

@Lichtsignaal I think some SDK stuff is available by including Espressif header files.

Hi Costas - just tried your sketch and it worked perfectly :slight_smile:
Clock syncs very quickly and temp updated properly.
thank you so much for your help - learnt quite a lot from this exercise.

1 Like

@mars my tests show there is negligible difference in access times whether you use static IP or not and whether you use Blynk cloud or local server.

same on my end. But I’m thinking if I setup static IP using AT commands and assuming that IP address remains in ESP even if powered off then it maybe faster to setup ?

All my esp8266 retain their ip addresses, is this because of my router settings?

if you using DHCP then most probably yes as you router may be configure to hold IP for MAC addresses for xx period before forcing renewal.


thank you so so so… much :slight_smile:

A post was split to a new topic: How can i set for server IP", 80