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

Commit

Permalink
Update samples to use internal ESP NTP mechanism (configTime)
Browse files Browse the repository at this point in the history
    - Also update logging in samples to use LogInfo as some printf
    invocations are not good on ESP8266 right now
  • Loading branch information
obsoleted authored and sandeepmistry committed Apr 18, 2016
1 parent 43c3183 commit b52ef5a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 55 deletions.
33 changes: 28 additions & 5 deletions examples/command_center/command_center.ino
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ void loop() {
///////////////////////////////////////////////////////////////////////////////////////////////////
void initSerial() {
//Initialize serial and wait for port to open:
// For SAMD boards (e.g. MKR1000, Adafruit WINC1500 based)
Serial.begin(9600);


// Uncomment the next two lines For ESP8266 boards (and comment out the line above)
// Serial.begin(115200);
// Serial.setDebugOutput(true);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// uncomment the next line to enable debug output on ESP8266
//Serial.setDebugOutput(true);
}

void initWifi() {
Expand Down Expand Up @@ -154,7 +156,8 @@ void initWifi() {
void initTime() {
// change the next line to use on non-WINC1500 based boards/shields
Adafruit_WINC1500UDP ntpUdp; // for Adafruit WINC1500
// WiFiUDP ntpUdp; // for WiFi101 or ESP8266
// WiFiUDP ntpUdp; // for WiFi101
// for ESP8266 boards see comment below
NTPClient ntpClient(ntpUdp);

ntpClient.begin();
Expand All @@ -172,6 +175,26 @@ void initTime() {
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() {
Expand Down
39 changes: 20 additions & 19 deletions examples/command_center/rem_ctrl_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#ifdef ARDUINO
#include "AzureIoTHub.h"
#include "iot_logging.h"
#else
#include "serializer.h"
#include "iothub_client_ll.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATIO
EXECUTE_COMMAND_RESULT TurnFanOn(BME280_data* device)
{
(void)device;
(void)printf("Turning Green LED on.\r\n");
LogInfo("Turning Green LED on.\r\n");
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
return EXECUTE_COMMAND_SUCCESS;
Expand All @@ -57,7 +58,7 @@ EXECUTE_COMMAND_RESULT TurnFanOn(BME280_data* device)
EXECUTE_COMMAND_RESULT TurnFanOff(BME280_data* device)
{
(void)device;
(void)printf("Turning red LED on.\r\n");
LogInfo("Turning red LED on.\r\n");
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
return EXECUTE_COMMAND_SUCCESS;
Expand All @@ -79,9 +80,9 @@ void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCal
{
int messageTrackingId = (intptr_t)userContextCallback;

(void)printf("Message Id: %d Received.\r\n", messageTrackingId);
LogInfo("Message Id: %d Received.\r\n", messageTrackingId);

(void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
LogInfo("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -91,17 +92,17 @@ static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsign
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
LogInfo("unable to create a new IoTHubMessage\r\n");
}
else
{
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
LogInfo("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
LogInfo("IoTHubClient accepted the message for delivery\r\n");
}
IoTHubMessage_Destroy(messageHandle);
}
Expand All @@ -118,7 +119,7 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
LogInfo("unable to IoTHubMessage_GetByteArray\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
Expand All @@ -127,7 +128,7 @@ static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE mess
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
LogInfo("failed to malloc\r\n");
result = EXECUTE_COMMAND_ERROR;
}
else
Expand Down Expand Up @@ -157,7 +158,7 @@ int rem_ctrl_http_init(void)

if (serializer_init(NULL) != SERIALIZER_OK)
{
(void)printf("Failed on serializer_init\r\n");
LogInfo("Failed on serializer_init\r\n");
}
else
{
Expand All @@ -166,7 +167,7 @@ int rem_ctrl_http_init(void)
iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
if (iotHubClientHandle == NULL)
{
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
LogInfo("Failed on IoTHubClient_LL_Create\r\n");
}
else
{
Expand All @@ -175,21 +176,21 @@ int rem_ctrl_http_init(void)
unsigned int minimumPollingTime = 9; // Because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds.
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
{
printf("failure to set option \"MinimumPollingTime\"\r\n");
LogInfo("failure to set option \"MinimumPollingTime\"\r\n");
}

myWeather = CREATE_MODEL_INSTANCE(RemoteMonitorExample, BME280_data);
if (myWeather == NULL)
{
(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
LogInfo("Failed on CREATE_MODEL_INSTANCE\r\n");
}
else
{
Init_level__i = 3;

if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
{
printf("unable to IoTHubClient_SetMessageCallback\r\n");
LogInfo("unable to IoTHubClient_SetMessageCallback\r\n");
}
else
{
Expand Down Expand Up @@ -230,7 +231,7 @@ void rem_ctrl_http_send_data(float Temp_c__f, float Pres_hPa__f, float Humi_pct_
if (Init_level__i < 4) return;

timeNow = (int)time(NULL);
sprintf(buff, "%d", timeNow);
sLogInfo(buff, "%d", timeNow);

myWeather->DeviceId = "FeatherM0_w_BME280";
myWeather->MTemperature = Temp_c__f;
Expand All @@ -242,24 +243,24 @@ void rem_ctrl_http_send_data(float Temp_c__f, float Pres_hPa__f, float Humi_pct_
size_t destinationSize;
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->EventTime, myWeather->MTemperature, myWeather->Pressure, myWeather->Humidity) != IOT_AGENT_OK)
{
(void)printf("Failed to serialize\r\n");
LogInfo("Failed to serialize\r\n");
}
else
{
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
LogInfo("unable to create a new IoTHubMessage\r\n");
}
else
{
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
LogInfo("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
LogInfo("IoTHubClient accepted the message for delivery\r\n");
}

IoTHubMessage_Destroy(messageHandle);
Expand Down
3 changes: 2 additions & 1 deletion examples/remote_monitoring/bme280.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "bme280.h"
#include "iot_logging.h"

const int Bme280_cs_pin__i = 5;
bool Bme_init_result = false;
Expand Down Expand Up @@ -33,6 +34,6 @@ void getNextSample(float* Temperature, float* Humidity)
//float Pres_hPa__f = bme.readPressure() / 100;
*Humidity = bme.readHumidity();
//printf("Temp=%.2f, Pres=%.2f, Humi=%.2f\n", Temp_c__f, Pres_hPa__f, Humi_pct__f);
printf("Temp=%.2f, Humi=%.2f\n", *Temperature, *Humidity);
LogInfo("Temp=%.2f, Humi=%.2f\n", *Temperature, *Humidity);
}

1 change: 1 addition & 0 deletions examples/remote_monitoring/remote_monitoring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "AzureIoTHub.h"
#include "sdk/schemaserializer.h"
#include "bme280.h"
#include "iot_logging.h"


static const char* deviceId = "[device-id]";
Expand Down
30 changes: 26 additions & 4 deletions examples/remote_monitoring/remote_monitoring.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ void loop() {

void initSerial() {
//Initialize serial and wait for port to open:
// For SAMD boards (e.g. MKR1000, Adafruit WINC1500 based)
Serial.begin(9600);

// Uncomment the next two lines For ESP8266 boards (and comment out the line above)
// Serial.begin(115200);
// Serial.setDebugOutput(true);

while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// uncomment the next line to enable debug output on ESP8266
//Serial.setDebugOutput(true);
}

void initWifi() {
Expand Down Expand Up @@ -95,7 +97,7 @@ void initWifi() {
}

void initTime() {
// change the next line to use on non-WiFi101 or ESP8266 based boards/shields
// 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);
Expand All @@ -115,5 +117,25 @@ void initTime() {
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;
// }
// }
}

Loading

0 comments on commit b52ef5a

Please sign in to comment.