Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By jimbob321
#25801 yep thats what it was :)
i'll let it run to see if it updates

Code: Select all..WiFi connected
IP address:
192.168.1.111
Received NTP Response
Unix time = 1439395623
User avatar
By martinayotte
#25827 I don't have much of the context to see or figure out.
You got an NTP response once, and then after 1 hour, you got a "No NTP reponse" ?
This probably means that your module lost his Wifi connection during that hour ...
User avatar
By rajbadri
#26181 hi i am in india and i am using GMT +5:30 in your code but somehow i get time which is 30 min ahead of the actual time.
is it because of daylight saving ? how can i correct this error.
this is my code
Code: Select all  #define tm1637_clk_pin 14
  #define tm1637_data_pin 13


#include <ntp.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <TM1637.h>
#include <Ticker.h>
#include <Time.h>

bool startWiFi(void);
time_t getNTPtime(void);
void readLine(char buffer[], uint8_t size);
void digitalClockDisplay();
void toggleColon(void);

NTP NTPclient;
tm1637 display(tm1637_clk_pin, tm1637_data_pin);
Ticker clock;
bool colon = true;
bool updateTime = true;

#define GMT +5.5 //india standard time

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println();
  while (!startWiFi()){delay(1500);}
 
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  display.setBrightness(4);

  NTPclient.begin("time.nist.gov", GMT);
  setSyncInterval(SECS_PER_HOUR);
  setSyncProvider(getNTPtime);
 
  clock.attach(0.5, toggleColon);

}

void loop() {
   if (updateTime){
      updateTime = false;
      if (timeStatus() != timeNotSet)
        digitalClockDisplay(); 
   }
}

time_t getNTPtime(void)
{
  return NTPclient.getNtpTime();
}

void digitalClockDisplay()
{
  // digital clock display of the time
    display.writeTime(hourFormat12(), minute(), colon);
    colon = !colon;
}


bool startWiFi(void)
{
  uint8_t i;
  //check for persistent wifi connection
 for (i=0;i<10;i++){
   if (WiFi.status() == WL_CONNECTED) return true;
   delay(500);
   Serial.print(".");
 }

  /*didn't work: so ask user to enter credentials over Serial Port */
  #define maxSSIDlength 20
  #define maxPASSlength 40
  char ssid[maxSSIDlength];
  char pass[maxPASSlength];
 
 //prompt the user for his ssid

  Serial.print("Enter ssid name: ");
  readLine(ssid, maxSSIDlength);
  Serial.println();
  Serial.print("Enter pass phrase: ");
  readLine(pass, maxPASSlength);
  Serial.println();

  Serial.print("Attempting to Connect");
  if (WiFi.begin(ssid, pass) != WL_CONNECTED) {
     for (i=0;i<10;i++){
       if (WiFi.status() == WL_CONNECTED) return true;
       delay(500);
       Serial.print(".");
     }
  }
  Serial.print("Failed to connect to: ");
  Serial.println(ssid);
 
  Serial.print("using pass phrase: ");
  Serial.println(pass);

  return false;
 }

void readLine(char buffer[], uint8_t size)
{
    uint8_t i = 0;
    char c;

    do {
      while (!Serial.available()){yield();} //wait for input
      c = Serial.read();
      Serial.write(c); //echo characters back to user.
      if (c == '\r') break;
      if (c != '\n')
        buffer[i++] = c;     
    } while (i < size-1);
    buffer[i] = '\0';
}

void toggleColon(void)
{
   updateTime = true; 
}