Advertisement
JustinCase2020

ESP8266 Basic SNTP (non-os sdk)

Apr 19th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include "osapi.h"
  2. #include "user_interface.h"
  3.  
  4. #define INFO( format, ... ) os_printf( format, ## __VA_ARGS__ )
  5.  
  6. LOCAL os_timer_t ntp_timer;
  7.  
  8. LOCAL char ssid[32] = "ssidthingy";
  9. LOCAL char password[64] = "passwordthingy";
  10.  
  11. uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void)
  12. {
  13.     return 1019;
  14. }
  15.  
  16. void NTPTimer(void* arg)
  17. {
  18.     static uint8_t cnt = 50;
  19.  
  20.     uint32 current_stamp;
  21.     current_stamp = sntp_get_current_timestamp();
  22.     if (current_stamp == 0 && cnt > 0)
  23.     {
  24.         cnt--;
  25.         os_timer_arm(&ntp_timer, 100, 0);
  26.     }
  27.     else
  28.     {
  29.         INFO("Current timestamp (%d): %d - %s", cnt, current_stamp, sntp_get_real_time(current_stamp));
  30.     }
  31. }
  32.  
  33. void ICACHE_FLASH_ATTR mywifi_handle_events(System_Event_t *evt)
  34. {
  35.     switch (evt->event)
  36.     {
  37.         case EVENT_STAMODE_CONNECTED:
  38.         {
  39.             break;
  40.         }
  41.         case EVENT_STAMODE_DISCONNECTED:
  42.         {
  43.             break;
  44.         }
  45.         case EVENT_STAMODE_GOT_IP:
  46.         {
  47.             sntp_init();
  48.             os_timer_arm(&ntp_timer, 100, 0);
  49.         }
  50.     }
  51. }
  52.  
  53. void ICACHE_FLASH_ATTR user_init(void)
  54. {
  55.     gpio_init();
  56.  
  57.     uart_init(74880, 74880);
  58.  
  59.     struct station_config stConf;
  60.     os_memcpy(&stConf.ssid, ssid, 32);
  61.     os_memcpy(&stConf.password, password, 64);
  62.  
  63.     wifi_set_event_handler_cb(mywifi_handle_events);
  64.     wifi_set_opmode_current(STATION_MODE);
  65.     wifi_station_set_config(&stConf);
  66.     wifi_set_sleep_type(NONE_SLEEP_T);
  67.  
  68.     os_timer_disarm(&ntp_timer);
  69.     os_timer_setfn(&ntp_timer, (os_timer_func_t *)NTPTimer, NULL);
  70.  
  71.     sntp_setservername(0, "0.ca.pool.ntp.org"); // set server 0 by domain name
  72.     sntp_setservername(1, "1.ca.pool.ntp.org"); // set server 1 by domain name
  73.     sntp_setservername(2, "2.ca.pool.ntp.org"); // set server 2 by domain name
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement