NTP request no response
Posted: Wed Jun 24, 2015 2:42 am
Hi all,
I tried to get time by NTP. There is no response from any NTP server.
I have put the call in project hello world:
The code itself is from Richard A. Burton (modified!!)
Can any one give me an advice?
Ulko
I tried to get time by NTP. There is no response from any NTP server.
I have put the call in project hello world:
Code: Select all
void user_init(void)
{
// Configure the UART
uart_init(BIT_RATE_115200, BIT_RATE_115200);
ets_uart_printf("START\r\n");
for(;;)
{
char command[100];
ets_strcpy(command,"Welt antworte!!\n");
if(uart0_read_string(2000000,3,pReadBuffer,command)){
ets_uart_printf("Antwort --------------------: %s\n",pReadBuffer);
}
else
ets_uart_printf("Timeout!\n");
ntp_get_time();
os_delay_us(5000000);
}
}
The code itself is from Richard A. Burton (modified!!)
Code: Select all
//////////////////////////////////////////////////
// Simple NTP client for ESP8266.
// Copyright 2015 Richard A Burton
// richardaburton@gmail.com
// See license.txt for license terms.
//////////////////////////////////////////////////
#include "espmissingincludes.h"
#include <c_types.h>
#include <user_interface.h>
#include <espconn.h>
#include <os_type.h>
#include <osapi.h>
#include <mem.h>
#include <time.h>
#include "stdio.h"
#include "ntp.h"
// list of major public servers http://tf.nist.gov/tf-cgi/servers.cgi
uint8 ntp_server[] = {98,175,203,200 }; // RWTH Aachen
static os_timer_t ntp_timeout;
static struct espconn *pCon = 0;
extern int ets_uart_printf(const char *fmt, ...);
extern char *pReadBuffer;
static void ntp_udp_timeout(void *arg) {
os_timer_disarm(&ntp_timeout);
ets_uart_printf("ntp timout\r\n");
// clean up connection
if (pCon) {
espconn_delete(pCon);
os_free(pCon->proto.udp);
os_free(pCon);
pCon = 0;
}
}
/************************************************************************
Beschreibung: monthlen
Inputs: isleapyear = 0-1, month=0-11
Return: Number of days per month
*************************************************************************/
unsigned char monthlen(unsigned char isleapyear,unsigned char month)
{
if (month == 1)
{
return (28+isleapyear);
}
if (month > 6)
{
month--;
}
if (month %2 == 1)
{
return (30);
}
return (31);
}
/************************************************************************
Beschreibung: decode_time
decodes the time into the datetime_t struct
Return:
*************************************************************************/
void decode_time(unsigned long ntp_time)
{
unsigned long dayclock;
unsigned int dayno;
struct tm *dt;
dt->tm_year = EPOCH_YR; //=1970
dayclock = (ntp_time - GETTIMEOFDAY_TO_NTP_OFFSET) % SECS_DAY;
dayno = (ntp_time - GETTIMEOFDAY_TO_NTP_OFFSET) / SECS_DAY;
dt->tm_sec = dayclock % 60UL;
dt->tm_min = (dayclock % 3600UL) / 60;
dt->tm_hour = dayclock / 3600UL;
dt->tm_wday = (dayno + 4) % 7; // day 0 was a thursday
wday = dt->tm_wday;
while (dayno >= YEARSIZE(dt->tm_year))
{
dayno -= YEARSIZE(dt->tm_year);
dt->tm_year++;
}
dt->tm_mon = 0;
while (dayno >= monthlen(LEAPYEAR(dt->tm_year), dt->tm_mon))
{
dayno -= monthlen(LEAPYEAR(dt->tm_year), dt->tm_mon);
dt->tm_mon++;
}
dt->tm_mon ++;
dt->tm_mday = dayno+1;
// Summertime
summertime = 1;
if (dt->tm_mon < 3 || dt->tm_mon > 10) // month 1, 2, 11, 12
{
summertime = 0; // -> Winter
}
if ((dt->tm_mday - dt->tm_wday >= 25) && (dt->tm_wday || dt->tm_hour >= 2))
{ // after last Sunday 2:00
if (dt->tm_mon == 10) // October -> Winter
{
summertime = 0;
}
}
else
{ // before last Sunday 2:00
if (dt->tm_mon == 3) // March -> Winter
{
summertime = 0;
}
}
// strcpy_P(dt->datestr, &wday_str[dt->wday*3]);
ets_uart_printf(" %d.%02d.%04d", dt->tm_wday, dt->tm_mon, dt->tm_year);
ets_uart_printf("%2d:%02d:%02d", dt->tm_hour, dt->tm_min, dt->tm_sec);
}
static void ntp_udp_recv(void *arg, char *pdata, unsigned short len) {
ntp_t *ntp;
struct tm *dt;
time_t timestamp;
ets_uart_printf("HALLO NTP\r\n");
os_timer_disarm(&ntp_timeout);
// extract ntp time
ntp = (ntp_t*)pdata;
timestamp = ntp->trans_time[0] << 24 | ntp->trans_time[1] << 16 |ntp->trans_time[2] << 8 | ntp->trans_time[3];
// convert to unix time
timestamp -= 2208988800UL;
// create tm struct
//dt = gmtime(×tamp);
decode_time(timestamp);
ets_uart_printf("NTP-timestamp:%d\r\n",timestamp);
// do something with it, like setting an rtc
//ds1307_setTime(dt);
// or just print it out
// ets_uart_printf("%02d:%02d:%02d\r\n", dt->tm_hour, dt->tm_min, dt->tm_sec);
// clean up connection
if (pCon) {
espconn_delete(pCon);
os_free(pCon->proto.udp);
os_free(pCon);
pCon = 0;
}
}
void ntp_get_time() {
ntp_t ntp;
unsigned char ret1,ret2,ret3;
// set up the udp "connection"
pCon = (struct espconn *) os_zalloc(sizeof(struct espconn));
pCon->type = ESPCONN_UDP;
pCon->state = ESPCONN_NONE;
pCon->proto.udp = (esp_udp*)os_zalloc(sizeof(esp_udp));
pCon->proto.udp->local_port = (uint16)espconn_port();
pCon->proto.udp->remote_port = 123;
os_memcpy(pCon->proto.udp->remote_ip, ntp_server, 4);
// create a really simple ntp request packet
os_memset(&ntp, 0, sizeof(ntp_t));
ntp.options = 0b00100011; // leap = 0, version = 4, mode = 3 (client)
// set timeout timer
os_timer_disarm(&ntp_timeout);
os_timer_setfn(&ntp_timeout, (os_timer_func_t*)ntp_udp_timeout, pCon);
os_timer_arm(&ntp_timeout, NTP_TIMEOUT_MS, 0);
// send the ntp request
ret1=espconn_create(pCon);
ret2=espconn_regist_recvcb(pCon, ntp_udp_recv);
ret3=espconn_sent(pCon, (uint8*)&ntp, sizeof(ntp_t));
ets_uart_printf("NTP Requst senden....Port:%d create:%d regist:%d sent:%d\r\n",pCon->proto.udp->local_port,ret1,ret2,ret3);
}
Can any one give me an advice?
Ulko