Post topics, source code that relate to the Arduino Platform

User avatar
By scargill
#4770 If this doesn't materialise - I have another way. I've been using NTP client on my home control for a while - and it's ok but not perfect, sometimes they are slow or busy - and you need a bit of code... if you have access to a PHP server - ie you might have server space or similar... a very simple PHP web page can be written to generate the time in a variety of formats - and also things like dusk and dawn.

My own returns this...

So no worries about summer/winter time and you get lighting up times back as well.

{time=1418518341;timestr=00:52:21 14-12-2014;dawn=07:40;dusk=16:24;}

Usually within milliseconds... the format is my chosing - but you could do anything - so I have a routine that waits for stuff coming back from the ESP8266 and then looks for start and end blocks... so something like waitforserial(mystring,sizeof(mystring),2000,500,"time=|;");

So that will wait up to 2 seconds for something coming in - will look for "time=" in that timeslot and once it has that will look for the next ";". After that it will hang around discarding anything else that comes in for 500ms. The only buffering needed in terms of the string... is in this case enough to store "1418518341" which is the time in a format suitable for pushing to the Arduino time library.

Works a treat. The only issue I'm having is doing that while the ESP8266 is acting as socket listener - it seems it can't do both at once which is a pain.. so right now that's the first thing I do on power up - go get the time - the plan would be to reset the ESP8266 board maybe 2am every morning and go get the time.

Pete.
User avatar
By Slartibartfast
#4774 I too have been trying to get this working (on an arduino pro mini). I get the connection an have taken some of the code from an aruino example to create the NTP request. Unfortunately the return data is garbage and I don't know what step to take next (apart from asking a question here) :D

Code: Select all// // ESP8266 connection

/* Connect ESP8266 as follows
  I used an Arduino 3.3v Pro Mini with a 3.3V USB-FTDI converter
  The ESP8266 was communicating at 9600 when delivered, ive upped this to 19200
  Vcc - 3.3v
  gnd - gnd
  rx - pin 11
  tx - pin 10
  ch_PD - pin 6
*/

// Target Access Point
#define ssid         "xxxxxxxxxxxx"
#define pass         "xxxxxxxxxxxx"

int EnablePin = 6;
unsigned long time = 0;

const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
char buffer[48];
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX


void setup() 
{
 
  pinMode(EnablePin, OUTPUT);
  digitalWrite(EnablePin, LOW);
  delay (500);
  digitalWrite(EnablePin, HIGH);
  delay (500);
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(19200);
  delay (1000);

  connectWiFi();  // Start the WiFi module
 
  setPacket();

}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

void connectWiFi()
{
 
//Rest the module.
Serial.println("AT+RST");
mySerial.println("AT+RST");
time = millis();
  while ((time + 2000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }

delay(20);

//Set the wireless mode
Serial.println("AT+CWMODE=1");
mySerial.println("AT+CWMODE=1");
time = millis();
  while ((time + 500) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }
 delay(20);

//disconnect  - it shouldn't be but just to make sure
Serial.println("AT+CWQAP");
mySerial.println("AT+CWQAP");
time = millis();
  while ((time + 1000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }
 
// connect to your wireless router 
String cmd="AT+CWJAP=\"";
cmd+=ssid;
cmd+="\",\"";
cmd+=pass;
cmd+="\"";
Serial.println(cmd);
mySerial.println(cmd);
//delay(2000);
time = millis();
  while ((time + 15000) >= millis()){
   if (mySerial.available() > 0) {
  Serial.write(mySerial.read());
  }
  }

//print the ip addr
  mySerial.println("AT+CIFSR");
  Serial.println("ip address:");
  time = millis();
  while ((time + 5000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }
 
//set the single connection mode
Serial.println("AT+CIPMUX=0");
mySerial.println("AT+CIPMUX=0");
time = millis();
  while ((time + 1000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }

//Connect to the NTP server

String cmd1 = "AT+CIPSTART=\"UDP\",\"";
cmd1 += "129.6.15.28";
cmd1 += "\",123";
mySerial.println(cmd1);
Serial.println(cmd1);
time = millis();
  while ((time + 5000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }
 
mySerial.println("AT+CIPSEND=48");
Serial.println("AT+CIPSEND=48");
time = millis();
  while ((time + 5000) >= millis()){
   if (mySerial.available() > 0) {
   Serial.write(mySerial.read());
   }
  }
}

void setPacket(){
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
 
  //mySerial.write("192.168.0.13", 123);
  mySerial.write(packetBuffer,NTP_PACKET_SIZE);
 
 // delay (2000);
 
  //mySerial.readBytes(buffer, 48);
  //for (int i=1;i<48;i++){
  //Serial.print(buffer[i]);
  //}
 // Serial.println(packetBuffer,NTP_PACKET_SIZE);
  //Udp.endPacket();
}


The last responses I get are:
==================================================================
OK
AT+CIPSTART="UDP","129.6.15.28",123
AT+CIPSTART="UDP","129.6.15.28",123


OK
AT+CIPSEND=48
AT+CIPSEND=48

> ã i 1N14

SEND OK

+IPD,48:$ã ACTSØ7žK0Ә

OK

===============================================================================================

.....am I close? How do I send and receive the data correctly? - I seem to have a good connection to the time server which is a start!
User avatar
By xiscoj
#7426 hi,
anyone has a working code to get time from ntp server with esp2866??
I'm interested on it.
Thanks.
User avatar
By tgibbs99
#13041 Alos interested in a sketch with esp8266 obtaining NPT time. I would also like to see alternative ways to create the request packet.

I believe this is the packet in binary:

111000110000000000000110111011000000000000000000000000000000000000000000000000000000000000000000010010010100111001001001010100100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Anyone interested in posting more basic instructions would be greatly appreciated.

Tim