diff --git a/examples/command_center/command_center.ino b/examples/command_center/command_center.ino index 2c18d56..b9f6e6d 100644 --- a/examples/command_center/command_center.ino +++ b/examples/command_center/command_center.ino @@ -20,7 +20,6 @@ //#include #include -#include #include #include "rem_ctrl_http.h" @@ -41,12 +40,19 @@ static const char pass[] = "[Your WiFi network WPA password or WEP key]"; // and SharedAccessKey for this particular Thing on the Internet. static const char* connectionString = "[Device Connection String]"; -// change the next line to use on non-Adafruit WINC1500 based boards/shields -Adafruit_WINC1500SSLClient sslClient; // for Adafruit WINC150 -//WiFiSSLClient sslClient; // for WiFi101 -//WiFiClientSecure sslClient; // for ESP8266 +// change the next two lines to use on non-Adafruit WINC1500 based boards/shields +Adafruit_WINC1500SSLClient sslClient; +Adafruit_WINC1500UDP udp; -AzureIoTHubClient iotHubClient(sslClient); +// for WiFi101 +//WiFiSSLClient sslClient; +//WiFiUDP udp; + +// for ESP8266 +//WiFiClientSecure sslClient; +//WiFiUDP udp; + +AzureIoTHubClient iotHubClient(sslClient, udp); #define SEALEVELPRESSURE_HPA (1013.25) const int Bme280_cs_pin__i = 5; @@ -59,7 +65,6 @@ void setup() { Serial.println("Azure_remote_monitoring Sketch."); initWifi(); - initTime(); initBME(); if (rem_ctrl_set_connection_string(connectionString)) { @@ -153,50 +158,6 @@ void initWifi() { Serial.println("Connected to wifi"); } -void initTime() { - // change the next line to use on non-WINC1500 based boards/shields - Adafruit_WINC1500UDP ntpUdp; // for Adafruit WINC1500 - // WiFiUDP ntpUdp; // for WiFi101 - // for ESP8266 boards see comment below - NTPClient ntpClient(ntpUdp); - - ntpClient.begin(); - - while (!ntpClient.update()) { - Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry."); - delay(5000); - } - - ntpClient.end(); - - unsigned long epochTime = ntpClient.getEpochTime(); - - Serial.print("Fetched NTP epoch time is: "); - Serial.println(epochTime); - - iotHubClient.setEpochTime(epochTime); - - // For ESP8266 boards comment out the above portion of the function and un-comment - // the remainder below. - - // time_t epochTime; - - // configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - // while (true) { - // epochTime = time(NULL); - - // if (epochTime == 0) { - // Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry."); - // delay(2000); - // } else { - // Serial.print("Fetched NTP epoch time is: "); - // Serial.println(epochTime); - // break; - // } - // } -} - void initBME() { Serial.println("Checking for the presence of the BME280 temp/humid/press module."); Bme_init_result = bme.begin(); diff --git a/examples/command_center/rem_ctrl_http.c b/examples/command_center/rem_ctrl_http.c index 6f2052b..d43a2c4 100644 --- a/examples/command_center/rem_ctrl_http.c +++ b/examples/command_center/rem_ctrl_http.c @@ -230,8 +230,8 @@ void rem_ctrl_http_send_data(float Temp_c__f, float Pres_hPa__f, float Humi_pct_ // Don't run this function unless the initialization succeeded. if (Init_level__i < 4) return; - timeNow = (int)time(NULL); - sLogInfo(buff, "%d", timeNow); + timeNow = (int)get_time(NULL); + sprintf(buff, "%d", timeNow); myWeather->DeviceId = "FeatherM0_w_BME280"; myWeather->MTemperature = Temp_c__f; diff --git a/examples/remote_monitoring/remote_monitoring.c b/examples/remote_monitoring/remote_monitoring.c index 3878714..e6ec720 100644 --- a/examples/remote_monitoring/remote_monitoring.c +++ b/examples/remote_monitoring/remote_monitoring.c @@ -125,7 +125,7 @@ void remote_monitoring_run(void) { initBme(); - srand((unsigned int)time(NULL)); + srand((unsigned int)get_time(NULL)); if (serializer_init(NULL) != SERIALIZER_OK) { LogInfo("Failed on serializer_init\r\n"); diff --git a/examples/remote_monitoring/remote_monitoring.ino b/examples/remote_monitoring/remote_monitoring.ino index faa7b66..9de3596 100644 --- a/examples/remote_monitoring/remote_monitoring.ino +++ b/examples/remote_monitoring/remote_monitoring.ino @@ -25,7 +25,6 @@ // // Setup the WINC1500 connection with the pins above and the default hardware SPI. // Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST); -#include #include #include "remote_monitoring.h" @@ -33,17 +32,23 @@ char ssid[] = "[Your WiFi network SSID or name]"; char pass[] = "[Your WiFi network WPA password or WEP key]"; -// change the next line to use on non-WiFi101 based boards/shields +// change the next two lines to use on non-WiFi101 based boards/shields WiFiSSLClient sslClient; -//WiFiClientSecure sslClient; // for ESP8266 -//Adafruit_WINC1500SSLClient sslClient; // for Adafruit WINC1500 +WiFiUDP udp; -AzureIoTHubClient iotHubClient(sslClient); +// for ESP8266 +//WiFiClientSecure sslClient; +//WiFiUDP udp; + +// for Adafruit WINC1500 +//Adafruit_WINC1500SSLClient sslClient; +//Adafruit_WINC1500UDP udp; + +AzureIoTHubClient iotHubClient(sslClient, udp); void setup() { initSerial(); initWifi(); - initTime(); iotHubClient.begin(); } @@ -95,47 +100,3 @@ void initWifi() { Serial.println("Connected to wifi"); } - -void initTime() { - // change the next line to use on non-WiFi101, for ESP8266 boards see comment below - WiFiUDP ntpUdp; - //Adafruit_WINC1500UDP ntpUdp; // for Adafruit WINC1500 - NTPClient ntpClient(ntpUdp); - - ntpClient.begin(); - - while (!ntpClient.update()) { - Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry."); - delay(5000); - } - - ntpClient.end(); - - unsigned long epochTime = ntpClient.getEpochTime(); - - Serial.print("Fetched NTP epoch time is: "); - Serial.println(epochTime); - - iotHubClient.setEpochTime(epochTime); - - // For ESP8266 boards comment out the above portion of the function and un-comment - // the remainder below. - - // time_t epochTime; - - // configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - // while (true) { - // epochTime = time(NULL); - - // if (epochTime == 0) { - // Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry."); - // delay(2000); - // } else { - // Serial.print("Fetched NTP epoch time is: "); - // Serial.println(epochTime); - // break; - // } - // } -} - diff --git a/examples/simplesample_http/simplesample_http.c b/examples/simplesample_http/simplesample_http.c index 6b70a96..eed9f7d 100644 --- a/examples/simplesample_http/simplesample_http.c +++ b/examples/simplesample_http/simplesample_http.c @@ -129,7 +129,7 @@ void simplesample_http_run(void) else { IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol); - srand((unsigned int)time(NULL)); + srand((unsigned int)get_time(NULL)); int avgWindSpeed = 10; if (iotHubClientHandle == NULL) diff --git a/examples/simplesample_http/simplesample_http.ino b/examples/simplesample_http/simplesample_http.ino index e98c4c9..e06b348 100644 --- a/examples/simplesample_http/simplesample_http.ino +++ b/examples/simplesample_http/simplesample_http.ino @@ -25,7 +25,6 @@ // // Setup the WINC1500 connection with the pins above and the default hardware SPI. // Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST); -#include #include #include "simplesample_http.h" @@ -33,17 +32,23 @@ char ssid[] = "yourNetwork"; // your network SSID (name) char pass[] = "yourPassword"; // your network password (use for WPA, or use as key for WEP) -// change the next line to use on non-WiFi101 based boards/shields +// change the next two lines to use on non-WiFi101 based boards/shields WiFiSSLClient sslClient; -//WiFiClientSecure sslClient; // for ESP8266 -//Adafruit_WINC1500SSLClient sslClient; // for Adafruit WINC1500 +WiFiUDP udp; -AzureIoTHubClient iotHubClient(sslClient); +// for ESP8266 +//WiFiClientSecure sslClient; +//WiFiUDP udp; + +// for Adafruit WINC1500 +//Adafruit_WINC1500SSLClient sslClient; +//Adafruit_WINC1500UDP udp; + +AzureIoTHubClient iotHubClient(sslClient, udp); void setup() { initSerial(); initWifi(); - initTime(); iotHubClient.begin(); } @@ -92,46 +97,3 @@ void initWifi() { Serial.println("Connected to wifi"); } - -void initTime() { - // change the next line to use on non-WiFi101, for ESP8266 boards see comment below - WiFiUDP ntpUdp; - //Adafruit_WINC1500UDP ntpUdp; // for Adafruit WINC1500 - NTPClient ntpClient(ntpUdp); - - ntpClient.begin(); - - while (!ntpClient.update()) { - Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry."); - delay(5000); - } - - ntpClient.end(); - - unsigned long epochTime = ntpClient.getEpochTime(); - - Serial.print("Fetched NTP epoch time is: "); - Serial.println(epochTime); - - iotHubClient.setEpochTime(epochTime); - - // For ESP8266 boards comment out the above portion of the function and un-comment - // the remainder below. - - // time_t epochTime; - - // configTime(0, 0, "pool.ntp.org", "time.nist.gov"); - - // while (true) { - // epochTime = time(NULL); - - // if (epochTime == 0) { - // Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry."); - // delay(2000); - // } else { - // Serial.print("Fetched NTP epoch time is: "); - // Serial.println(epochTime); - // break; - // } - // } -} diff --git a/keywords.txt b/keywords.txt index 64f59fc..8b03690 100644 --- a/keywords.txt +++ b/keywords.txt @@ -13,7 +13,6 @@ AzureIoTHubClient KEYWORD1 ####################################### begin KEYWORD2 -setEpochTime KEYWORD2 ####################################### # Constants (LITERAL1) diff --git a/src/AzureIoTHubClient.cpp b/src/AzureIoTHubClient.cpp index 0d6a4ec..b71c028 100644 --- a/src/AzureIoTHubClient.cpp +++ b/src/AzureIoTHubClient.cpp @@ -1,29 +1,30 @@ // Copyright (c) Arduino. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -#include -#include - #include -Client* AzureIoTHubClient::sslClient = NULL; +extern HTTPSClient* httpsClient; +extern NTPClient* ntpClient; -AzureIoTHubClient::AzureIoTHubClient(Client& sslClient) +AzureIoTHubClient::AzureIoTHubClient(Client& sslClient, UDP& ntpUdp): + _httpsClient(sslClient), + _ntpClient(ntpUdp) { - AzureIoTHubClient::sslClient = &sslClient; + httpsClient = &_httpsClient; + ntpClient = &_ntpClient; } void AzureIoTHubClient::begin() { -} + ntpClient->begin(); -void AzureIoTHubClient::setEpochTime(unsigned long epochTime) -{ -#ifndef ARDUINO_ARCH_ESP8266 - struct timeval tv; + while (!ntpClient->update()) { + Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry."); + delay(5000); + } + + unsigned long epochTime = ntpClient->getEpochTime(); - tv.tv_sec = epochTime; - tv.tv_usec = 0; - settimeofday(&tv, NULL); -#endif + Serial.print("Fetched NTP epoch time is: "); + Serial.println(epochTime); } diff --git a/src/AzureIoTHubClient.h b/src/AzureIoTHubClient.h index 874d5aa..ff0cd98 100644 --- a/src/AzureIoTHubClient.h +++ b/src/AzureIoTHubClient.h @@ -4,16 +4,22 @@ #ifdef __cplusplus #include +#include + +#include + +#include "util/HTTPSClient.h" class AzureIoTHubClient { public: - AzureIoTHubClient(Client& sslClient); + AzureIoTHubClient(Client& sslClient, UDP& ntpUdp); void begin(); - void setEpochTime(unsigned long epochTime); - static Client* sslClient; +private: + HTTPSClient _httpsClient; + NTPClient _ntpClient; }; #endif diff --git a/src/agenttime.cpp b/src/agenttime.cpp new file mode 100644 index 0000000..d04c9c4 --- /dev/null +++ b/src/agenttime.cpp @@ -0,0 +1,26 @@ +// Copyright (c) Arduino. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +#include "time.h" + +#include "sdk/agenttime.h" + +#include + +NTPClient* ntpClient = NULL; + +time_t get_time(time_t* p) +{ + time_t t = ntpClient->getEpochTime(); + + if (p) { + *p = t; + } + + return t; +} + +double get_difftime(time_t stopTime, time_t startTime) +{ + return difftime(stopTime, startTime); +} diff --git a/src/httpapi.cpp b/src/httpapi.cpp index 35d0552..0a6442b 100644 --- a/src/httpapi.cpp +++ b/src/httpapi.cpp @@ -15,7 +15,6 @@ HTTPSClient* httpsClient = NULL; HTTPAPI_RESULT HTTPAPI_Init(void) { - httpsClient = new HTTPSClient(AzureIoTHubClient::sslClient); httpsClient->setTimeout(10000); return HTTPAPI_OK; diff --git a/src/samd/time.cpp b/src/samd/time.cpp deleted file mode 100644 index 62738b8..0000000 --- a/src/samd/time.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Arduino. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -#if defined(ARDUINO_ARCH_SAMD) -#include -#include - -#include - -RTCZero rtc; - -extern "C" { - int _gettimeofday(struct timeval* tp, void* /*tzvp*/) - { - tp->tv_sec = rtc.getEpoch(); - tp->tv_usec = 0; - - return 0; - } - - int settimeofday(const struct timeval* tp, const struct timezone* /*tzp*/) - { - rtc.begin(); - rtc.setEpoch(tp->tv_sec); - - return 0; - } -} -#endif \ No newline at end of file diff --git a/src/sdk/agenttime.c b/src/sdk/agenttime.c deleted file mode 100644 index d1e72d9..0000000 --- a/src/sdk/agenttime.c +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include -#ifdef _CRTDBG_MAP_ALLOC -#include -#endif -#include "gballoc.h" - -#include "time.h" -#include "agenttime.h" - -time_t get_time(time_t* p) -{ - return time(p); -} - -struct tm* get_gmtime(time_t* currentTime) -{ - return gmtime(currentTime); -} - -char* get_ctime(time_t* timeToGet) -{ - return ctime(timeToGet); -} - -double get_difftime(time_t stopTime, time_t startTime) -{ - return difftime(stopTime, startTime); -} \ No newline at end of file diff --git a/src/update_sdk.cmd b/src/update_sdk.cmd index d488b68..fc164a2 100644 --- a/src/update_sdk.cmd +++ b/src/update_sdk.cmd @@ -26,7 +26,6 @@ copy "%~dp0\temp\azure-iot-sdks\c\serializer\src\" "%~dp0\sdk" copy "%~dp0\temp\azure-iot-sdks\c\serializer\inc\" "%~dp0\sdk" copy "%~dp0\temp\azure-iot-sdks\c\azure-c-shared-utility\c\src\" "%~dp0\sdk" copy "%~dp0\temp\azure-iot-sdks\c\azure-c-shared-utility\c\inc\" "%~dp0\sdk" -copy "%~dp0\temp\azure-iot-sdks\c\azure-c-shared-utility\c\adapters\agenttime.c" "%~dp0\sdk" del "%~dp0\sdk\iot_logging.h" del "%~dp0\sdk\iothubtransportamqp.*" diff --git a/src/util/HTTPSClient.cpp b/src/util/HTTPSClient.cpp index 160a4cf..8c2a78a 100644 --- a/src/util/HTTPSClient.cpp +++ b/src/util/HTTPSClient.cpp @@ -10,8 +10,8 @@ // #define DEBUG_STREAM Serial -HTTPSClient::HTTPSClient(Client* sslClient) : - _sslClient(sslClient), +HTTPSClient::HTTPSClient(Client& sslClient) : + _sslClient(&sslClient), _contentLength(0) { } diff --git a/src/util/HTTPSClient.h b/src/util/HTTPSClient.h index d153166..5564700 100644 --- a/src/util/HTTPSClient.h +++ b/src/util/HTTPSClient.h @@ -13,7 +13,7 @@ class HTTPSClient : Print { public: - HTTPSClient(Client* sslClient); + HTTPSClient(Client& sslClient); int begin(const char* host, int port = HTTPS_PORT); uint8_t connected(); void setTimeout(unsigned long timeout);