void GetTime::begin() { config = new Configuration("Time", new ConfigurationItem("maxcount", 10), new ConfigurationItem("ntp1", "ntp.scarlet.be"), new ConfigurationItem("ntp2", "ntp.belnet.be"), new ConfigurationItem("timezone", 1), NULL); max_count = config->GetValue("maxcount"); ntp1 = (char *)config->GetStringValue("ntp1"); ntp2 = (char *)config->GetStringValue("ntp2"); timezone = config->GetValue("timezone"); #if 0 //UPnPService::begin(config); //alarmpin = config->GetValue("pin"); #endif sntp_init(); sntp_setservername(0, ntp1); sntp_setservername(1, ntp2); (void)sntp_set_timezone(timezone); // Wire.begin(); }
/* This function initializes applications */ static void apps_init() { #if LWIP_DNS_APP && LWIP_DNS /* wait until the netif is up (for dhcp, autoip or ppp) */ sys_timeout(5000, dns_dorequest, NULL); #endif /* LWIP_DNS_APP && LWIP_DNS */ #if LWIP_CHARGEN_APP && LWIP_SOCKET chargen_init(); #endif /* LWIP_CHARGEN_APP && LWIP_SOCKET */ #if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP ping_init(); #endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */ #if LWIP_NETBIOS_APP && LWIP_UDP netbios_init(); #endif /* LWIP_NETBIOS_APP && LWIP_UDP */ #if LWIP_HTTPD_APP && LWIP_TCP #ifdef LWIP_HTTPD_APP_NETCONN http_server_netconn_init(); #else /* LWIP_HTTPD_APP_NETCONN */ httpd_init(); #endif /* LWIP_HTTPD_APP_NETCONN */ #endif /* LWIP_HTTPD_APP && LWIP_TCP */ #if LWIP_NETIO_APP && LWIP_TCP netio_init(); #endif /* LWIP_NETIO_APP && LWIP_TCP */ #if LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP rtp_init(); #endif /* LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP */ #if LWIP_SNTP_APP && LWIP_SOCKET sntp_init(); #endif /* LWIP_SNTP_APP && LWIP_SOCKET */ #if LWIP_SHELL_APP && LWIP_NETCONN shell_init(); #endif /* LWIP_SHELL_APP && LWIP_NETCONN */ #if LWIP_TCPECHO_APP #if LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) tcpecho_init(); #else /* LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) */ echo_init(); #endif #endif /* LWIP_TCPECHO_APP && LWIP_NETCONN */ #if LWIP_UDPECHO_APP && LWIP_NETCONN udpecho_init(); #endif /* LWIP_UDPECHO_APP && LWIP_NETCONN */ #if LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET socket_examples_init(); #endif /* LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET */ #ifdef LWIP_APP_INIT LWIP_APP_INIT(); #endif }
uint32_t ICACHE_FLASH_ATTR NTP_SetTimeZone(PACKET_CMD *cmd) { REQUEST req; int timezone; CMD_Request(&req, cmd); if (CMD_GetArgc(&req) != 1) { return 0; } CMD_PopArgs(&req, (uint8_t*)&timezone); if ((timezone > 13) || (timezone < -11)) { return -1; } if (timezone != sntp_get_timezone()) { sntp_stop(); if (true == sntp_set_timezone(timezone)) { sntp_init(); } } return 0; }
static void get_time() { struct timeval now; int sntp_retry_cnt = 0; int sntp_retry_time = 0; sntp_setoperatingmode(0); sntp_setservername(0, "pool.ntp.org"); sntp_init(); while (1) { for (int32_t i = 0; (i < (SNTP_RECV_TIMEOUT / 100)) && now.tv_sec < 1525952900; i++) { vTaskDelay(100 / portTICK_RATE_MS); gettimeofday(&now, NULL); } if (now.tv_sec < 1525952900) { sntp_retry_time = SNTP_RECV_TIMEOUT << sntp_retry_cnt; if (SNTP_RECV_TIMEOUT << (sntp_retry_cnt + 1) < SNTP_RETRY_TIMEOUT_MAX) { sntp_retry_cnt ++; } printf("SNTP get time failed, retry after %d ms\n", sntp_retry_time); vTaskDelay(sntp_retry_time / portTICK_RATE_MS); } else { printf("SNTP get time success\n"); break; } } }
void ICACHE_FLASH_ATTR rtc_timezone(uint8_t timezone) { sntp_stop(); // if (true == sntp_set_timezone(sysCfg.timezone)) sntp_set_timezone(timezone); sntp_init(); myrtc = sntp_get_current_timestamp(); }
void sntp_task(void *pvParameters) { while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) { vTaskDelay(10); }; /* * Set one of SNTP_OPMODE_LISTENONLY or SNTP_OPMODE_POLL */ /* * SNTP_OPMODE_LISTENONLY * just needs the mode set, no server names required * (requires broadcast NTP on your network) */ sntp_setoperatingmode(SNTP_OPMODE_LISTENONLY); /* * SNTP_OPMODE_POLL * Needs one or more server names set * additional servers are "fail over" * Can use a DNS name or an address literal * LWIP can also be configured with SNTP_GET_SERVERS_FROM_DHCP * (DHCP-specified SNTP servers untested at this time) * * NOTE: Early testing with polling shows higher deviations * than seen with broadcast, even with RTT compensation * Cause unknown at this time, but believed to be within SNTP * amd not part of timekeeping itself. */ /* sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, "ntp_a.example.com"); sntp_setservername(1, "ntp_b.example.com"); sntp_setservername(2, "ntp_c.example.com"); */ /* Once set up, this is all it takes */ sntp_init(); /* * Have high-priority thread "parked", might as well use it * Show calling gettimeofday() once an hour to check for timer wrap * (the SNTP process itself, if connected, should be sufficient) */ while (1) { vTaskDelay(60 * 60 * (1000 / portTICK_PERIOD_MS)); printf("gettimeofday(NULL, NULL)\n"); gettimeofday(NULL, NULL); } }
int main(void) { halInit(); chSysInit(); get_device_id(); /* start stdout port */ sdStart(SD_STDIO, NULL); xflash_init(); cmdline_init(); rngStart(&RNGD); app_cfg_init(); check_for_faults(); gfx_init(); touch_init(); sensor_init(SENSOR_1, SD_OW1); sensor_init(SENSOR_2, SD_OW2); temp_control_init(CONTROLLER_1); temp_control_init(CONTROLLER_2); ota_update_init(); net_init(); web_api_init(); sntp_init(); gui_init(); thread_watchdog_init(); create_home_screen(); recovery_screen_create(); screen_saver_create(); if (palReadPad(PORT_SELF_TEST_EN, PAD_SELF_TEST_EN) == 0) { widget_t* self_test_screen = self_test_screen_create(); gui_push_screen(self_test_screen); } recovery_img_init(); while (TRUE) { cmdline_restart(); toggle_LED1(); } }
void ICACHE_FLASH_ATTR sntp_test() { sntp_init(); sntp_setservername(0, (char*)"cn.pool.ntp.org"); sntp_setservername(1, (char*)"1.cn.pool.ntp.org"); sntp_setservername(2, (char*)"2.cn.pool.ntp.org"); os_timer_disarm(&test_timer); os_timer_setfn(&test_timer, test_timer_cb, NULL); os_timer_arm(&test_timer, 0, 0); // Start immediately. }
void ninit(void) { sntp_init(); ip_addr_t ip; int r = dns_gethostbyname("pool.ntp.org", &ip, ntp_dns_found, NULL); if (r == ESPCONN_OK) { sntp_setserver(0, &ip); } }
LOCAL void ICACHE_FLASH_ATTR sntp_starts_here(){ ipaddr_aton("192.168.254.30", &NTP1); // sntp_setservername(0, ”bg.pool.ntp.org”); // set server 0 by domain name // sntp_setservername(1, ”us.pool.ntp.org”); // set server 0 by domain name sntp_setserver(0,NTP1); sntp_set_timezone(+2); sntp_init(); os_timer_disarm(&ds18b20_timer); os_timer_setfn(&ds18b20_timer, (os_timer_func_t *)sntp_cb, (void *)0); os_timer_arm(&ds18b20_timer, DELAY, 1); }
void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ sntp_setservername(0, "pool.ntp.org"); // set sntp server after got ip address sntp_init(); os_timer_disarm(&sntp_timer); os_timer_setfn(&sntp_timer, (os_timer_func_t *)sntpfn, NULL); os_timer_arm(&sntp_timer, 1000, 1);//1s } else { MQTT_Disconnect(&mqttClient); } }
void ICACHE_FLASH_ATTR rtc_init(uint8_t timezone) { sntp_setservername(0, NTP_SERVER1); sntp_setservername(1, NTP_SERVER2); // sntp_setservername(2, NTP_SERVER3); sntp_stop(); // if (true == sntp_set_timezone(sysCfg.timezone)) sntp_set_timezone(timezone); sntp_init(); myrtc = 0; }
void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) { sntp_stop(); setServer(0, server1); setServer(1, server2); setServer(2, server3); sntp_set_timezone(timezone/3600); sntp_set_daylight(daylightOffset_sec); sntp_init(); }
void ntp_setup(void) { tv_t tv; tz_t tz; time_t sec; if(!network_init) return; if(ntp_init == 0) { ip_addr_t *addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t)); ipaddr_aton("192.168.200.1", addr); sntp_setserver(1,addr); ipaddr_aton("192.168.200.240", addr); sntp_setserver(2,addr); sntp_init(); os_free(addr); ntp_init = 1; DEBUG_PRINTF("NTP:1\n"); } if(ntp_init == 1) { // they hard coded it to +8 hours from GMT sec = sntp_get_current_timestamp(); if(sec > 10) { ntp_init = 2; } } if(ntp_init == 2) { sntp_stop(); DEBUG_PRINTF("NTP:2\n"); // they return GMT + 8 sec = sec - (8UL * 3600UL); // we are GMT - 4 sec = sec - (4UL * 3600UL); tv.tv_sec = sec; tv.tv_usec = 0; tz.tz_minuteswest = 0; settimeofday(&tv, &tz); DEBUG_PRINTF("SEC:%ld\n",sec); DEBUG_PRINTF("TIME:%s\n", ctime(&sec)); ntp_init = 3; } }
void WEAK_ATTR configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) { sntp_stop(); setServer(0, server1); setServer(1, server2); setServer(2, server3); s_timezone_sec = timezone; s_daylightOffset_sec = daylightOffset_sec; sntp_set_timezone(timezone/3600); sntp_init(); }
void start_sntp_service(void) { char *ntp_server = NULL; /*os_printf("+start_sntp_service\n");*/ // check if need to set ntp server ntp_server = sntp_getservername(0); /*os_printf("ntp_server:(%p)%s\n", ntp_server, ntp_server);*/ if ((ntp_server == NULL) || (os_strcmp(ntp_server, NTP_SERVER1) != 0)) { // no ntp server. set it sntp_setservername(0, NTP_SERVER1); sntp_setservername(1, NTP_SERVER2); sntp_setservername(2, NTP_SERVER3); } sntp_init(); }
void ICACHE_FLASH_ATTR user_sntp_init(void) { static os_timer_t user_sntp_timer; // 配置sntp服务器 sntp_setservername(0,"0.cn.pool.ntp.org"); sntp_setservername(1,"1.cn.pool.ntp.org"); sntp_setservername(2,"2.cn.pool.ntp.org"); // sntp初始化 sntp_init(); // 定时器初始化 os_timer_disarm(&user_sntp_timer); os_timer_setfn(&user_sntp_timer, sntp_timer_cb , NULL); os_timer_arm(&user_sntp_timer, SNTP_READ_INTERVAL, 1); }
void ICACHE_FLASH_ATTR at_exeCmdNTP(uint8_t id) { uint8 buffer[255] = {0}; sntp_stop(); int i; uint32 tm = 0; int ntp_servers_len = sizeof(ntp_servers) / sizeof(ntp_servers[0]); for (i = 0; i < 3; i++) { if (i >= ntp_servers_len) break; sntp_setservername(i, ntp_servers[i]); } sntp_set_timezone(NTP_OFFSET); sntp_init(); os_sprintf(buffer, "NTP init\r\n"); at_port_print(buffer); at_response_ok(); }
static void tcpip_init_done(void *arg) { sys_sem_t *sem; sem = (sys_sem_t *)arg; init_netifs(); #if LWIP_IPV4 netbiosns_set_name("simhost"); netbiosns_init(); #endif /* LWIP_IPV4 */ sntp_setoperatingmode(SNTP_OPMODE_POLL); #if LWIP_DHCP sntp_servermode_dhcp(1); /* get SNTP server via DHCP */ #else /* LWIP_DHCP */ #if LWIP_IPV4 sntp_setserver(0, netif_ip_gw4(&netif)); #endif /* LWIP_IPV4 */ #endif /* LWIP_DHCP */ sntp_init(); #if LWIP_SNMP lwip_privmib_init(); #if SNMP_LWIP_MIB2 #if SNMP_USE_NETCONN snmp_threadsync_init(&snmp_mib2_lwip_locks, snmp_mib2_lwip_synchronizer); #endif /* SNMP_USE_NETCONN */ snmp_mib2_set_syscontact_readonly((const u8_t*)"root", NULL); snmp_mib2_set_syslocation_readonly((const u8_t*)"lwIP development PC", NULL); snmp_mib2_set_sysdescr((const u8_t*)"simhost", NULL); #endif /* SNMP_LWIP_MIB2 */ snmp_set_mibs(mibs, LWIP_ARRAYSIZE(mibs)); snmp_init(); #endif /* LWIP_SNMP */ sys_sem_signal(sem); }
void ICACHE_FLASH_ATTR user_init() { // init UART for debugging baud rate comes from user_config.h uart_div_modify(0, UART_CLK_FREQ / BAUD_RATE); os_printf("\r\nESP8266 OOK decoding\r\n"); // speed boost (hopefully) //system_update_cpu_freq(160); system_update_cpu_freq(80); // setup loop callback in system task queue system_os_task(loop, user_procTaskPrio, user_procTaskQueue, user_procTaskQueueLen); system_os_post(user_procTaskPrio, 0, 0); // init wifi using creds in wifi_config.h. Put your own creds in the file connect_wifi(WIFI_SSID, WIFI_PSK); // apparently an accurate system time is needed for SSL, so start up // SNTP stuff. // SNTP is also used for timestamping emails and alarm triggeringings sntp_setservername(0, "time.nist.gov"); sntp_setservername(1, "time-c.nist.gov"); sntp_set_timezone(TIMEZONE); sntp_init(); // init stuff for the ook decoder gpio_init(); gpio_intr_handler_register(ook_intr_handler, (void*) &unprocessedPackets); init_ook_decoder(); // webserver-related initialisation init_web_server(); attach_btn_clear(clearTriggeredSensors); attach_btn_arm_alarm(arm_alarm); attach_btn_disarm_alarm(disarm_alarm); // update so we don't just have a blank page on startup updateWebpage(); }
/* thread phase init */ void rt_init_thread_entry(void *parameter) { /* LwIP Initialization */ #ifdef RT_USING_LWIP { extern void lwip_sys_init(void); extern void rt_hw_dm9000_init(void); eth_system_device_init(); /* register ethernetif device */ rt_hw_dm9000_init(); /* init all device */ rt_device_init_all(); /* init lwip system */ lwip_sys_init(); rt_kprintf("TCP/IP initialized!\n"); sntp_init(); } #endif }
void ICACHE_FLASH_ATTR network_check_ip(void) { struct ip_info ipconfig; os_timer_disarm(&network_timer); wifi_get_ip_info(STATION_IF, &ipconfig); if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0) { char page_buffer[20]; os_sprintf(page_buffer,"IP: %d.%d.%d.%d",IP2STR(&ipconfig.ip)); os_sprintf(ipstation, "%d.%d.%d.%d",IP2STR(&ipconfig.ip)); os_printf(page_buffer); ServerInit(flashData->ServerPort); //GetNetworkTime(); os_printf("SNTP-----%d", flashData->SNTP); sntp_init(flashData->SNTP); } else { counter++; os_printf("No ip found\n\r"); if (counter < 12) { os_timer_setfn(&network_timer, (os_timer_func_t *)network_check_ip, NULL); os_printf("try %d\n", counter); os_timer_arm(&network_timer, 1000, 0); } else { os_printf("could not connect to server\n", counter); wifi_station_disconnect(); ServerInit(flashData->ServerPort); counter = 0; } } }
//Called from the wifi callback, Set the clock from NTP void ICACHE_FLASH_ATTR NIXE_Set_Clock_Wifi() { sntp_setservername(0, "se.pool.ntp.org"); // set server 0 by domain name sntp_setservername(1, "0.europe.pool.ntp.org"); // set server 1 by domain name sntp_set_timezone(+1); sntp_init(); uint32 current_stamp; current_stamp = sntp_get_current_timestamp(); os_printf("sntp: %d, %s \n",current_stamp, sntp_get_real_time(current_stamp)); //Update the External RTC via i2c // init tm Struct /* time->tm_sec time->tm_min time->tm_hour time->tm_wday time->tm_mday time->tm_mon time->tm_year */ time_t unix = sntp_get_current_timestamp(); //nixietime = gmtime(&unix); //gmtime not in sdk.. tm_sec + tm_min*60 tm_hour*3600 tm_yday*86400 + (tm_year-70)*31536000 + ((tm_year-69)/4)*86400 - ((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400 ds1307_setTime(nixietime); }
static void initialize_sntp(void) { sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, "pool.ntp.org"); sntp_init(); }
//***************************************************************************** // //! Initializes the lwIP TCP/IP stack. //! //! \param pucMAC is a pointer to a six byte array containing the MAC //! address to be used for the interface. //! \param ulIPAddr is the IP address to be used (static). //! \param ulNetMask is the network mask to be used (static). //! \param ulGWAddr is the Gateway address to be used (static). //! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC will force //! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with //! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force //! Link Local only. //! //! This function performs initialization of the lwIP TCP/IP stack for the //! Stellaris Ethernet MAC, including DHCP and/or AutoIP, as configured. //! //! \return None. // //***************************************************************************** void LWIPServiceTaskInit(void *pvParameters) { struct ip_addr *ip_addr; struct ip_addr *net_mask; struct ip_addr *gw_addr; //struct ip_addr ip_addr_local; //struct ip_addr net_mask_local; char IPState = 0; char* configLoad; IP_CONFIG * ipCfg; printf("Initialisiere IP "); ipCfg = pvPortMalloc(sizeof(IP_CONFIG)); configLoad = loadFromConfig(IP_CONFIG_FILE, "USE_DHCP"); #ifdef ENABLE_GRAPHIC vShowBootText("load ipconfig ..."); #endif printf("LWIPSTACK: LOAD CONFIG: (%s)\n", configLoad); if (configLoad == 0) { IPState = IPADDR_USE_AUTOIP; } else if (strcmp(configLoad, "true") == 0) { IPState = IPADDR_USE_DHCP; } else { IPState = IPADDR_USE_STATIC; } vPortFree(configLoad); // Start the TCP/IP thread & init stuff tcpip_init(NULL, NULL); vTaskDelay(100 / portTICK_RATE_MS); // Setup the network address values. if (IPState == IPADDR_USE_STATIC) { ip_addr = getAddresFromConfig("IP_ADDRESS"); net_mask = getAddresFromConfig("IP_SUBNETMASK"); gw_addr = getAddresFromConfig("IP_GATEWAY"); } #if LWIP_DHCP || LWIP_AUTOIP else { ip_addr = pvPortMalloc(sizeof(struct ip_addr)); net_mask = pvPortMalloc(sizeof(struct ip_addr)); gw_addr = pvPortMalloc(sizeof(struct ip_addr)); } #endif // Create, configure and add the Ethernet controller interface with // default settings. // WARNING: This must only be run after the OS has been started. // Typically this is the case, however, if not, you must place this // in a post-OS initialization // @SEE http://lwip.wikia.com/wiki/Initialization_using_tcpip.c printf("Starting NETIF ... \n"); #ifdef ENABLE_GRAPHIC vShowBootText("starting Network ..."); #endif netif_add(&lwip_netif, ip_addr, net_mask, gw_addr, NULL, ethernetif_init, tcpip_input); netif_set_default(&lwip_netif); printf("NETIF UP\n"); // Start DHCP, if enabled. #if LWIP_DHCP if (IPState == IPADDR_USE_DHCP) { #ifdef ENABLE_GRAPHIC vShowBootText("waiting for DHCP ..."); #endif printf("Starte DHCP Client ... "); if (dhcp_start(&lwip_netif) == ERR_OK) { printf("[ok]\n"); } else { printf("[fail]\n"); } } #endif // Start AutoIP, if enabled and DHCP is not. #if LWIP_AUTOIP if (IPState == IPADDR_USE_AUTOIP) { printf ("Setzte Auto IP (NICHT DHCP) ...\n"); autoip_start(&lwip_netif); } #endif if (IPState == IPADDR_USE_STATIC) { // Bring the interface up. netif_set_up(&lwip_netif); } vTaskDelay(1000 / portTICK_RATE_MS); while (0 == netif_is_up(&lwip_netif)) { vTaskDelay(5000 / portTICK_RATE_MS); if (0 == netif_is_up(&lwip_netif)) { dhcp_renew(&lwip_netif); } } printnetif(&lwip_netif); configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_SERVER"); if (strcmp(configLoad, "true") == 0) { /* Initialize HTTP, DNS, SNTP */ printf("HTTPD Starten ...\n"); httpd_init(); } vPortFree(configLoad); #if ENABLE_SNTP printf("SNTP Starten ...\n"); sntp_init(); #endif #if ENABLE_DNS printf("DNS Starten ...\n"); dns_init(); #endif #if ENABLE_NET_BIOS printf("NETBIOS Starten ...\n"); netbios_init(); #endif printf("Dienste gestartet ...\n"); #ifdef ENABLE_GRAPHIC configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_CLIENT"); if (strcmp(configLoad, "true") == 0) { vShowBootText("loading menu ..."); vLoadMenu(); } else { vShowBootText("ready for requests ..."); } vPortFree(configLoad); #endif // Nothing else to do. No point hanging around. while (1) { vTaskDelay(500 / portTICK_RATE_MS); if (EthernetPHYRead(ETH_BASE, PHY_MR1) & ETH_PHY_LINK_UP) { if (!(netif_is_up(&lwip_netif))) { // set link up flag netif_set_up(&lwip_netif); #ifdef ENABLE_GRAPHIC vShowBootText("activate networkinterface ..."); configLoad = loadFromConfig(IP_CONFIG_FILE, "IS_CLIENT"); if (strcmp(configLoad, "true") == 0) { vShowBootText("loading menu ..."); vLoadMenu(); } else { vShowBootText("ready for requests ..."); } vPortFree(configLoad); #endif if (IPState == IPADDR_USE_DHCP) { printf("DHCP Adresse anfordern ... "); if (dhcp_renew(&lwip_netif) == ERR_OK) { printf("[ok]\n"); printnetif(&lwip_netif); } else { printf("[fail]\n"); } } } } else { if (netif_is_up(&lwip_netif)) { #ifdef ENABLE_GRAPHIC vShowBootText("no networkconnection!!"); #endif printf("Deaktiviere Netzwerkinterface ... "); netif_set_down(&lwip_netif); } } } }
//Main routine void ICACHE_FLASH_ATTR user_init(void) { stdoutInit(); os_delay_us(100000); CFG_Load(); ioInit(); WIFI_Connect(wifiConnectCb); httpdInit(builtInUrls, sysCfg.httpd_port); if(sysCfg.ntp_enable==1) { sntp_init(sysCfg.ntp_tz); //timezone } if(sysCfg.mqtt_enable==1) { MQTT_InitConnection(&mqttClient, (uint8_t *)sysCfg.mqtt_host, sysCfg.mqtt_port, sysCfg.mqtt_use_ssl ); MQTT_InitClient(&mqttClient, (uint8_t *)sysCfg.mqtt_devid, (uint8_t *)sysCfg.mqtt_user, (uint8_t *)sysCfg.mqtt_pass, sysCfg.mqtt_keepalive,1); MQTT_OnConnected(&mqttClient, mqttConnectedCb); MQTT_OnDisconnected(&mqttClient, mqttDisconnectedCb); MQTT_OnPublished(&mqttClient, mqttPublishedCb); MQTT_OnData(&mqttClient, mqttDataCb); } if(sysCfg.sensor_dht22_enable) DHTInit(SENSOR_DHT22, 30000); if(sysCfg.sensor_ds18b20_enable) ds_init(30000); broadcastd_init(); thermostat_init(30000); /* //Netbios to set the name struct softap_config wiconfig; os_memset(netbios_name, ' ', sizeof(netbios_name)-1); if(wifi_softap_get_config(&wiconfig)) { int i; for(i = 0; i < sizeof(netbios_name)-1; i++) { if(wiconfig.ssid[i] < ' ') break; netbios_name[i] = wiconfig.ssid[i]; }; } else os_sprintf(netbios_name, "ESP8266"); netbios_name[sizeof(netbios_name)-1]='\0'; netbios_init(); */ os_printf("\nRelay Board Ready\n"); os_printf("Free heap size:%d\n",system_get_free_heap_size()); #ifdef CGIPWM_H //Mind the PWM pin!! defined in pwm.h duty=0; pwm_init( 50, &duty); pwm_set_duty(duty, 0); pwm_start(); #endif //OLEDInit(); }
/* This function initializes applications */ static void apps_init(void) { #if LWIP_DNS_APP && LWIP_DNS /* wait until the netif is up (for dhcp, autoip or ppp) */ sys_timeout(5000, dns_dorequest, NULL); #endif /* LWIP_DNS_APP && LWIP_DNS */ #if LWIP_CHARGEN_APP && LWIP_SOCKET chargen_init(); #endif /* LWIP_CHARGEN_APP && LWIP_SOCKET */ #if LWIP_PING_APP && LWIP_RAW && LWIP_ICMP ping_init(); #endif /* LWIP_PING_APP && LWIP_RAW && LWIP_ICMP */ #if LWIP_NETBIOS_APP && LWIP_UDP netbiosns_init(); #ifndef NETBIOS_LWIP_NAME #if LWIP_NETIF_HOSTNAME netbiosns_set_name(netif_default->hostname); #else netbiosns_set_name("NETBIOSLWIPDEV"); #endif #endif #endif /* LWIP_NETBIOS_APP && LWIP_UDP */ #if LWIP_HTTPD_APP && LWIP_TCP #ifdef LWIP_HTTPD_APP_NETCONN http_server_netconn_init(); #else /* LWIP_HTTPD_APP_NETCONN */ httpd_init(); #endif /* LWIP_HTTPD_APP_NETCONN */ #endif /* LWIP_HTTPD_APP && LWIP_TCP */ #if LWIP_MDNS_RESPONDER mdns_resp_init(); #if LWIP_NETIF_HOSTNAME mdns_resp_add_netif(netif_default, netif_default->hostname, 3600); #else mdns_resp_add_netif(netif_default, "lwip", 3600); #endif mdns_resp_add_service(netif_default, "lwipweb", "_http", DNSSD_PROTO_TCP, HTTPD_SERVER_PORT, 3600, srv_txt, NULL); #endif #if LWIP_NETIO_APP && LWIP_TCP netio_init(); #endif /* LWIP_NETIO_APP && LWIP_TCP */ #if LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP rtp_init(); #endif /* LWIP_RTP_APP && LWIP_SOCKET && LWIP_IGMP */ #if LWIP_SNTP_APP && LWIP_SOCKET sntp_init(); #endif /* LWIP_SNTP_APP && LWIP_SOCKET */ #if LWIP_SHELL_APP && LWIP_NETCONN shell_init(); #endif /* LWIP_SHELL_APP && LWIP_NETCONN */ #if LWIP_TCPECHO_APP #if LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) tcpecho_init(); #else /* LWIP_NETCONN && defined(LWIP_TCPECHO_APP_NETCONN) */ tcpecho_raw_init(); #endif #endif /* LWIP_TCPECHO_APP && LWIP_NETCONN */ #if LWIP_UDPECHO_APP && LWIP_NETCONN udpecho_init(); #endif /* LWIP_UDPECHO_APP && LWIP_NETCONN */ #if LWIP_LWIPERF_APP lwiperf_start_tcp_server_default(lwiperf_report, NULL); #endif #if LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET socket_examples_init(); #endif /* LWIP_SOCKET_EXAMPLES_APP && LWIP_SOCKET */ #ifdef LWIP_APP_INIT LWIP_APP_INIT(); #endif }
/** Application entry point. */ int main(void) { static thread_t *shelltp = NULL; /* Initializes a serial-over-USB CDC driver. */ sduObjectInit(&SDU1); sduStart(&SDU1, &serusbcfg); sdStart(&SD3, NULL); chprintf((BaseSequentialStream *)&SD3 , "\n> boot\n"); /* * Activates the USB driver and then the USB bus pull-up on D+. * Note, a delay is inserted in order to not have to disconnect the cable * after a reset. */ usbDisconnectBus(serusbcfg.usbp); chThdSleepMilliseconds(1500); usbStart(serusbcfg.usbp, &usbcfg); usbConnectBus(serusbcfg.usbp); /* Shell manager initialization. */ shellInit(); /* Initialize global objects. */ config_init(); chMtxObjectInit(&robot_pose_lock); /* Initialise timestamp module */ timestamp_stm32_init(); /* bus enumerator init */ static __attribute__((section(".ccm"))) struct bus_enumerator_entry_allocator bus_enum_entries_alloc[MAX_NB_BUS_ENUMERATOR_ENTRIES]; bus_enumerator_init(&bus_enumerator, bus_enum_entries_alloc, MAX_NB_BUS_ENUMERATOR_ENTRIES); /* allocate and init motor manager */ static __attribute__((section(".ccm"))) trajectory_t trajectory_buffer[MAX_NB_TRAJECTORY_BUFFERS]; static __attribute__((section(".ccm"))) float trajectory_points_buffer[ACTUATOR_TRAJECTORY_NB_POINTS * ACTUATOR_TRAJECTORY_POINT_DIMENSION * MAX_NB_TRAJECTORY_BUFFERS]; static __attribute__((section(".ccm"))) motor_driver_t motor_driver_buffer[MAX_NB_MOTOR_DRIVERS]; motor_manager_init(&motor_manager, trajectory_buffer, MAX_NB_TRAJECTORY_BUFFERS, trajectory_points_buffer, MAX_NB_TRAJECTORY_BUFFERS, motor_driver_buffer, MAX_NB_MOTOR_DRIVERS, &bus_enumerator); differential_base_init(); /* Checks if there is any log message from a previous boot */ if (panic_log_read() != NULL) { /* Turns on the user LED if yes */ palClearPad(GPIOC, GPIOC_LED); /* Turn on all LEDs on the front panel. */ palSetPad(GPIOF, GPIOF_LED_READY); palSetPad(GPIOF, GPIOF_LED_DEBUG); palSetPad(GPIOF, GPIOF_LED_ERROR); palSetPad(GPIOF, GPIOF_LED_POWER_ERROR); palSetPad(GPIOF, GPIOF_LED_PC_ERROR); palSetPad(GPIOF, GPIOF_LED_BUS_ERROR); palSetPad(GPIOF, GPIOF_LED_YELLOW_1); palSetPad(GPIOF, GPIOF_LED_YELLOW_2); palSetPad(GPIOF, GPIOF_LED_GREEN_1); palSetPad(GPIOF, GPIOF_LED_GREEN_2); } else { struct netif *ethernet_if; differential_base_tracking_start(); // tracy ip_thread_init(); chThdSleepMilliseconds(1000); ethernet_if = netif_find("ms0"); if (ethernet_if) { dhcp_start(ethernet_if); } sntp_init(); can_bridge_init(); uavcan_node_start(10); rpc_server_init(); message_server_init(); interface_panel_init(); odometry_publisher_init(); #ifdef ENABLE_STREAM #warning "Enabling robot stream can lead to lwip crash. Do not use in match until fixed." stream_init(); #endif } /* main thread, spawns a shell on USB connection. */ while (1) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) { shelltp = shellCreate(&shell_cfg1, SHELL_WA_SIZE, USB_SHELL_PRIO); } else if (chThdTerminatedX(shelltp)) { chThdRelease(shelltp); /* Recovers memory of the previous shell. */ shelltp = NULL; /* Triggers spawning of a new shell. */ } chThdSleepMilliseconds(500); } }
void ntp_setup(void) { tv_t tv; tz_t tz; time_t sec; struct ip_info getinfo; // Wait until we have an IP address before we set the time if(!network_init) return; if(ntp_init == 0) { ip_addr_t *addr = (ip_addr_t *)safecalloc(sizeof(ip_addr_t),1); // form pool.ntp.org ipaddr_aton("206.108.0.131", addr); sntp_setserver(1,addr); ipaddr_aton("167.114.204.238", addr); sntp_setserver(2,addr); #if 0 // Alternate time setting if the local router does NTP if(wifi_get_ip_info(0, &getinfo)) { printf("NTP:0 GW: %s\n", ipv4_2str(getinfo.gw.addr)); printf("NTP:0 IP: %s\n", ipv4_2str(getinfo.ip.addr)); sntp_setserver(1, & getinfo.gw); sntp_setserver(2, & getinfo.ip); } else { printf("NTP:0 failed to get GW address\n"); return; } #endif if( sntp_set_timezone(0) ) { printf("NTP: set_timeone OK\n"); sntp_init(); safefree(addr); ntp_init = 1; printf("NTP:1\n"); } else { printf("NTP: set_timeone Failed\n"); } } if(ntp_init == 1) { // they hard coded it to +8 hours from GMT if( (sec = sntp_get_current_timestamp()) > 10 ) { sntp_stop(); ntp_init = 2; } } if(ntp_init == 2) { time_t s; tm_t *p; printf("NTP:2\n"); // they return GMT + 8 // sec = sec - (8UL * 3600UL); tv.tv_sec = sec; printf("ntp_init: %s\n", asctime(gmtime(&sec))); printf("ntp_init: %s\n", ctime_gm(&sec)); tv.tv_usec = 0; tz.tz_minuteswest = 300; tz.tz_dsttime = 0; settimeofday(&tv, &tz); printf("SEC:%ld\n",sec); printf("TIME:%s\n", ctime(&sec)); printf("Zone: %d\n", (int) sntp_get_timezone()); ntp_init = 3; set_dst(tv.tv_sec); print_dst_gmt(); print_dst(); p = gmtime(&tv.tv_sec); mktime(p); printf("Localtime: %s\n", asctime(p)); } }