int main() { EthernetInterface eth; NTPClient ntp; eth.init(); //Use DHCP eth.connect(); // NTP set time { bool result = true; const char *url_ntp_server = "0.pool.ntp.org"; printf("NTP_SETTIME: Trying to update time... \r\n"); const int ret = ntp.setTime(url_ntp_server); if (ret == 0) { time_t ctTime = time(NULL); printf("NTP_SETTIME: UTC Time read successfully ... [OK]\r\n"); printf("NTP_SETTIME: %s\r\n", ctime(&ctTime)); } else { printf("NTP_SETTIME: Error(%d) ... [FAIL]\r\n", ret); result = false; } if (result == false) { notify_completion(false); exit(ret); } } eth.disconnect(); notify_completion(true); return 0; }
int setupRealTime(void) { int result; (void)printf("setupRealTime begin\r\n"); if (EthernetInterface::connect()) { (void)printf("Error initializing EthernetInterface.\r\n"); result = __LINE__; } else { (void)printf("setupRealTime NTP begin\r\n"); NTPClient ntp; if (ntp.setTime("0.pool.ntp.org") != 0) { (void)printf("Failed setting time.\r\n"); result = __LINE__; } else { (void)printf("set time correctly!\r\n"); result = 0; } (void)printf("setupRealTime NTP end\r\n"); EthernetInterface::disconnect(); } (void)printf("setupRealTime end\r\n"); return result; }
void setup() { Serial.begin(115200); delay(2000); // We start by connecting to a WiFi network WiFi.mode (WIFI_STA); WiFi.begin (SETTINGS_NETWORK_SSID, SETTINGS_NETWORK_PASS); Serial.println(); Serial.println(); Serial.print("Wait for WiFi... "); while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } timeClient.begin(); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); Serial.println(__DATE__); Serial.println(__TIME__); // load time from network if rtc has lost power... if (rtc.lostPower()) { Serial.println("RTC lost power- set time from NTP"); timeClient.update(); setTime(timeClient.getEpochTime()); rtc.adjust(DateTime(year(), month(), day(), hour(), minute(), second())); } DateTime now = rtc.now(); setTime(now.unixtime()); delay(500); }
int setupRealTime(void) { int result; if (EthernetInterface::connect()) { result = __LINE__; } else { NTPClient ntp; if (ntp.setTime("0.pool.ntp.org") != 0) { result = __LINE__; } else { result = 0; } EthernetInterface::disconnect(); } return result; }
HTTPAPI_RESULT HTTPAPI_Init(void) { LogInfo("HTTPAPI_Init::Start\r\n"); time_t ctTime; ctTime = time(NULL); HTTPAPI_RESULT result; LogInfo("HTTAPI_Init::Time is now (UTC) %s\r\n", ctime(&ctTime)); if (eth.init()) { LogInfo("HTTPAPI_Init::Error with initing the Ethernet Interface\r\n"); result = HTTPAPI_INIT_FAILED; } else { LogInfo("HTTPAPI_Init::Ethernet interface was inited!\r\n"); if (eth.connect(30000)) { LogError("HTTPAPI_Init::Error with connecting.\r\n"); result = HTTPAPI_INIT_FAILED; } else { LogInfo("HTTAPI_Init::Ethernet interface was connected (brought up)!\r\n"); LogInfo("HTTAPI_Init::MAC address %s\r\n", eth.getMACAddress()); LogInfo("HTTAPI_Init::IP address %s\r\n", eth.getIPAddress()); if (ntp.setTime("0.pool.ntp.org") == 0) { LogInfo("HTTAPI_Init:: Successfully set time\r\n"); LogInfo("HTTAPI_Init::Time is now (UTC) %s\r\n", ctime(&ctTime)); result = HTTPAPI_OK; } else { LogError("HTTPAPI_INIT::Unable to set time. Init failed"); result = HTTPAPI_INIT_FAILED; } } } LogInfo("HTTPAPI_Init::End\r\n"); return result; }
int main() { setbuf(stdout, NULL); // no buffering for this filehandle char json[1024]; int content_length; int response_code; char buf[1024]; float amb_temp; setupEthernet(); // set time lcd.printf("Reading Time... "); ntp.setTime("time-c.nist.gov"); lcd.printf("Time set"); while(1) { // Loop char streamName[] = "amb-temp"; // Set value for stream you are using // GET Stream value response_code = getStream(streamName, json); content_length = readContentLength(json); pc.printf("GET Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); ///// // PUT value to Strem amb_temp = tmp.read(); sprintf(json, "{\"value\":\"%0.2f\"}", amb_temp); response_code = putStream(streamName, json); content_length = readContentLength(json); pc.printf("PUT Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); ////// // POST value(s) to Stream time_t seconds; seconds = time(NULL); // get current time from mbed RTC strftime(buf,40, "%Y-%m-%dT%H:%M:%S%z", localtime(&seconds)); amb_temp = tmp.read(); sprintf(json, "{ \"values\": [ {\"at\": \"%s\", \"value\":\"%0.2f\"} ] }", buf, amb_temp); response_code = postStream(streamName, json); content_length = readContentLength(json); pc.printf("POST Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); /////// // PUT Location to Feed sprintf(json, "{ \"name\": \"%s\", \"latitude\": \"%0.8f\", \"longitude\": \"%0.8f\", \"elevation\": \"%0.2f\" }", LOC_NAME, LOC_LAT, LOC_LONG, LOC_ELEV); response_code = putLocation(json); content_length = readContentLength(json); pc.printf("PUT Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); /////// // GET Location of Feed response_code = getLocation(json); content_length = readContentLength(json); pc.printf("GET Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json); Thread::wait(5000); } eth.disconnect(); while(1) {} }