Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

Replace sdk/agentime.c with NTPClient back version #18

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 12 additions & 51 deletions examples/command_center/command_center.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//#include <WiFiUdp.h>

#include <Adafruit_BME280.h>
#include <NTPClient.h>
#include <AzureIoTHub.h>

#include "rem_ctrl_http.h"
Expand All @@ -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;
Expand All @@ -59,7 +65,6 @@ void setup() {
Serial.println("Azure_remote_monitoring Sketch.");

initWifi();
initTime();
initBME();

if (rem_ctrl_set_connection_string(connectionString)) {
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions examples/command_center/rem_ctrl_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/remote_monitoring/remote_monitoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
61 changes: 11 additions & 50 deletions examples/remote_monitoring/remote_monitoring.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@
// // Setup the WINC1500 connection with the pins above and the default hardware SPI.
// Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);

#include <NTPClient.h>
#include <AzureIoTHub.h>

#include "remote_monitoring.h"

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();
}
Expand Down Expand Up @@ -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;
// }
// }
}

2 changes: 1 addition & 1 deletion examples/simplesample_http/simplesample_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
60 changes: 11 additions & 49 deletions examples/simplesample_http/simplesample_http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@
// // Setup the WINC1500 connection with the pins above and the default hardware SPI.
// Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);

#include <NTPClient.h>
#include <AzureIoTHub.h>

#include "simplesample_http.h"

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();
}
Expand Down Expand Up @@ -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;
// }
// }
}
1 change: 0 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ AzureIoTHubClient KEYWORD1
#######################################

begin KEYWORD2
setEpochTime KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
31 changes: 16 additions & 15 deletions src/AzureIoTHubClient.cpp
Original file line number Diff line number Diff line change
@@ -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 <time.h>
#include <sys/time.h>

#include <AzureIoTHubClient.h>

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);
}
12 changes: 9 additions & 3 deletions src/AzureIoTHubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
#ifdef __cplusplus

#include <Client.h>
#include <Udp.h>

#include <NTPClient.h>

#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
Expand Down
26 changes: 26 additions & 0 deletions src/agenttime.cpp
Original file line number Diff line number Diff line change
@@ -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.h>

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);
}
Loading