Setting StaticIP address on ESP8266

@Costas @Gianca @Dmitriy thanks for your help but I’m finding this rtc not working very well at all wth local blynk server. Is this a know fault / issue. You’ll see when it does connect the time if +1 off but ok in the history graph :frowning:

here is my code (its a work in progress but you can hopefully follow what I’m trying to do) - it works well but clock hardly ever syncs !
perhaps I’m doing something obviously wrong.

#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];

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

#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);


// 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[] = "xxxxxxxxx"; // 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[] = { xxx, xxx, xxx, xxx ...}
IPAddress arduino_ip ( 192,   168,   0,  253);
IPAddress dns_ip     (  192,   168,   0,   1);
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255,   0);

void setup()
{
 
  pinMode (13, INPUT_PULLUP); // set GPIO13 Onewire with Internal PULL_UP resistor on for it to work
  
  Serial.begin(9600);
  
  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
  

  sensors.begin();
  
  
  sensors.setResolution(Therm_1,12);
  sensors.setResolution(Therm_2,12); 
  sensors.setResolution(Therm_3,12); 
  sensors.setResolution(Therm_4,12); 
  sensors.setResolution(Therm_5,12);
   
  // 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);
  Serial.println(sensors.getResolution());

 
   WiFi.config(arduino_ip, gateway_ip, subnet_mask);
   WiFi.begin("xxxx", "xxxx");
   Blynk.config(auth,IPAddress(xxx,xxx,xxx,xxx));
 
  // Or like this:
  //Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  
  while (Blynk.connect() == false) {    // wait for Blynk to be connected
  }  
  rtc.begin();
  Serial.println ("BLYNK is Connected!");

   //  Blynk.syncAll();   // Real all virtualpins   
   //   clockcounter = timer.setInterval (300L, gettime); // check if clock has updated every second
   //Serial.print ("Clockcounter = ");
   // Serial.println (clockcounter);
}

void loop()
{

  // 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
  Serial.println("DONE");


 // printTemperature(Therm_1, 1);
 // printTemperature(Therm_2, 2);
 // printTemperature(Therm_3, 3);
 // printTemperature(Therm_4, 4);
 printTemperature(Therm_5, 5);
  
  Blynk.run();
   // deepSleep time is defined in microseconds. Multiply
   // seconds by 1e6 
 
   // Serial.println ("I'm going to sleep");
   
     clockDisplay();
     
     if (clock_sync == 0) { gettime();}
          
  //  ESP.deepSleep(sleepTimeS * 1000000); not using this for now
    Serial.println ("I'm awake");
    delay (1000);
  
}


BLYNK_WRITE(V1)
{
 // 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 = ");
 Serial.println(tempC);
 Blynk.virtualWrite(V1,tempC);

 V1_written = 1;  // my little flag
}


 
bool isFirstConnect = true;

BLYNK_CONNECTED() {
  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(sensors.getTempC(deviceAddress));
  //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.println(thermostat_label);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  Serial.println("...temperatere read ok..... pls wait");
  Serial.println(" ");

  Blynk.virtualWrite(V1,tempC);
}


// 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(Time);
  Serial.print(" ");
  Serial.print(Date);
  Serial.println();

  // 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
     sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
      Serial.println(Date);
     sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
     Serial.println(Time);
     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
    }         
 }

@mars the sketch by @Gianca will not work if you have a project where you have a lot of sync and virtualWrite entries in setup(). Either it will sync the time and skip the other syncs and virtualWrite’s or it will skip the time sync and do the other stuff.

Below are a couple of extracts from Serial Monitor. The first is with line 11 of the following sketch commented out (so without a fixed IP for the ESP) and the second is with a fixed IP for the ESP (line 11 not commented out).

Attempting to connect to SSID: MTN WIFI 19996
[260] Connecting to MTN WIFI 19996
[2261] Connected to WiFi
[2261] IP: 192.168.10.156
[2261] Blynk v0.3.8 on ESP8266
WiFi connected with IP address: 192.168.10.156
[5001] Connecting to blynk-cloud.com:8442
[5137] Ready (ping: 0ms).
Connected to Server
[6275] Time sync: OK
Date is: 04/09/2016 and the time is now 12:45:09
[7277] Time sync: OK




Attempting to connect to SSID: MTN WIFI 19996
.
[761] Blynk v0.3.8 on ESP8266
WiFi connected with IP address: 192.168.10.20
[5001] Connecting to blynk-cloud.com:8442
[5152] Ready (ping: 1ms).
Connected to Server
[6291] Time sync: OK
Date is: 04/09/2016 and the time is now 12:48:46
[7292] Time sync: OK

Look through this sketch, change the settings but nothing else and let me know what you get.

// ESP_RTC_FixedIP.ino by Costas 4th Sept 2016
//http://community.blynk.cc/t/setting-staticip-address-on-esp8266/7866

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

#define FIXEDIP // comment this out if you don't want to assign a fixed IP to the ESP

#ifdef FIXEDIP
  // Configure your own IP details in this section *********************
  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);
  // end of static IP configuration for ESP ***************************
#endif

SimpleTimer timer;
WidgetRTC rtc;
BLYNK_ATTACH_WIDGET(rtc, V24);  // change to you chosen virtual pin
char Date[16];
char Time[16];
char ssid[] = "MTN WIFI 19996";
char pass[] = "xxxxxxxxxx";
char auth[] = "xxxxxxxxxxxxxxxxxxx";
int clockcounter = 3;

char connectionStatus[28] = "Connected to Server"; // default is that a connection will be made to the server

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, kill it
      sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
      sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
      Serial.print("Date is: ");
      Serial.print(Date);
      Serial.print(" and the time is now ");
      Serial.println(Time);
    }
    rtc.begin();  // keep trying to sync the time until year is not 1970
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  
  #ifdef FIXEDIP
    //**************** this section is to allocate fixed IP to ESP ***************************
      WiFi.config(arduino_ip, gateway_ip, subnet_mask);
      WiFi.begin(ssid, pass);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      } 
      Serial.println(); 
      Blynk.config(auth);
    //*************** end of fixed IP allocation section *************************************/
  #else
    Blynk.begin(auth, ssid, pass);  // Fixed IP is not required for the ESP
  #endif
  
  Serial.print("WiFi connected with IP address: ");
  Serial.println(WiFi.localIP());
    
  int mytimeout = millis() / 1000;
  while (Blynk.connect(7000) == false) { // try to connect to server for 7 seconds
    if((millis() / 1000) > mytimeout + 4){ // continue if not connected within 5 seconds
      strcpy(connectionStatus, "Failed to connect to server");
      break;
    }
  }  
  Serial.println(connectionStatus);
  clockcounter = timer.setInterval(1000L, gettime); // check every second if clock has updated then delete this timer
}

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

Meanwhile I will take a quick look at your sketch.

@mars I have done a few mods to your sketch and below is the Serial Monitor output when connecting to a Pi Zero.

Dallas Temperature IC Control Library Demo
Locating devices...Found 0 devices.
Resolution set to:9
.
[775] Blynk v0.3.8 on ESP8266
[5001] Connecting to 192.168.10.229:8442
[5105] Ready (ping: 7ms).
Blynk First connected EXECUTED
BLYNK is Connected!
Requesting temperatures...
DONE
Thermostat : 5
Temp C: -127.00 Temp F: -196.60
...temperature read ok..... pls wait
 
Current time: 00:00:05 01/01/1970
Year in gettime = 1970
still trying to sync :-(
I'm awake
Requesting temperatures...
DONE
Thermostat : 5
Temp C: -127.00 Temp F: -196.60
...temperature read ok..... pls wait
 
[6442] Time sync: OK
Year in gettime = 2016
04/09/2016
13:55:42
Current time: 13:55:42 04/09/2016
I'm awake
Requesting temperatures...
DONE
Thermostat : 5
Temp C: -127.00 Temp F: -196.60
...temperature read ok..... pls wait

I haven’t hooked up a temperature sensor so it defaults to -127.00 but as you will see it takes 5 seconds to connect and within a further 1.5 seconds it has the correct time.

You loop() is a real mess. You shouldn’t be trying to read the temperature and time a thousand times a second. That is what a loop() does. I can see your deepsleep entries are commented out at the moment and you could argue when enabled the bad loop will not be important. This is not true because it is simply bad practise not to use SimpleTimer for your functions.

That is one reason you have had problems with RTC, the loop is so busy that RTC function can’t be called. I’ll post your amended sketch shortly.

thanks for initial feedback - I’m trying your sketch now.

BTW: given i have a delay (1000) in my main loop does that not reduce the amount of time the loop is run to ONCE per second ?

Yes it does but that is not the way to do it.
What time is it with you?

Edit:

What you were doing with delay(1000) in the loop and rtc.begin() in setup() was:

Asking for the time
Going off to the loop() working hard then waiting, followed by working hard then waiting etc and it never gets the chance to actually recheck the time on the server. Must be 1 second timer as then there is no waiting involved.

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: 192.168.0.253
[21072] Connecting to blynk-cloud.com:8442
[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 192.168.10.229:8442
[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
04/09/2016
14:40:17
Requesting temperatures...
DONE


// marsmodified.ino Costas 4 Sept 2016 http://community.blynk.cc/t/setting-staticip-address-on-esp8266/7866

#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 TEMPERATURE_PRECISION 12

#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
  
  sensors.begin();
  
  sensors.setResolution(Therm_1,12);
  sensors.setResolution(Therm_2,12); 
  sensors.setResolution(Therm_3,12); 
  sensors.setResolution(Therm_4,12); 
  sensors.setResolution(Therm_5,12);
   
  // 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);
  Serial.println(sensors.getResolution());

   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, "192.168.10.229");                               // Costas to Pi Zero local server
   //Blynk.config(auth, "192.168.10.229");                               // Costas to Blynk cloud server                      
 
  // Or like this:
  //Blynk.begin(auth, "blynk-cloud.com", 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
  Serial.println("DONE");
 // 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");
   
     clockDisplay();
     
     //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()
{
  Blynk.run();
  timer.run();  // Costas
}


BLYNK_WRITE(V1) 
{
 // 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 = ");
 Serial.println(tempC);
 Blynk.virtualWrite(V1,tempC);  

 V1_written = 1;  // my little flag
}


 
bool isFirstConnect = true;

BLYNK_CONNECTED() {
  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(sensors.getTempC(deviceAddress));
  //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.println(thermostat_label);
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
  Serial.println("...temperature read ok..... pls wait"); // Costas
  Serial.println(" ");

  Blynk.virtualWrite(V1,tempC);  
}


// 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(Time);
  Serial.print(" ");
  Serial.print(Date);
  Serial.println();

  // 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());
      Serial.println(Date);
      sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
      Serial.println(Time);
      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 ?