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

Commit

Permalink
Introduce AzureIoTHubClass to reduce boilerplate in the sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed Apr 5, 2016
1 parent 2f2a227 commit 033bc25
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 126 deletions.
124 changes: 0 additions & 124 deletions examples/samd/simplesample_http/simplesample_http.ino

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ That does not mean that HTTP only works with the _LL APIs.
Simply changing the using the convenience layer (functions not having _LL)
and removing calls to _DoWork will yield the same results. */

#include "AzureIoT.h"
#include "AzureIoTHub.h"

static const char* connectionString = "HostName=[host].azure-devices.net;DeviceId=[device];SharedAccessKey=[key]";
static const char* connectionString = "[device connection string]";

// Define the Model
BEGIN_NAMESPACE(WeatherStation);
Expand Down
23 changes: 23 additions & 0 deletions examples/simplesample_http/simplesample_http.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Arduino. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <AzureIoTHub.h>

#include "simplesample_http.h"

char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

void setup() {
Serial.begin(9600);
while(!Serial) {
;
}

AzureIoTHub.begin(ssid, pass);
}

void loop() {
simplesample_http_run();
}

19 changes: 19 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#######################################
# Syntax Coloring Map For WiFi
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

AzureIoTHub KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

begin KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
117 changes: 117 additions & 0 deletions src/AzureIoTHub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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>

#if defined(ARDUINO_SAMD_FEATHER_M0)
#include <Adafruit_WINC1500.h>
#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000)
#include <WiFi101.h>
#elif defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#else
#error "Unsupported platform"
#endif

#include "util/NTPClient.h"

#include "AzureIoTHub.h"

AzureIoTHubClass AzureIoTHub;

#if defined(ARDUINO_SAMD_FEATHER_M0)

#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC

Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
#endif

void AzureIoTHubClass::begin(const char ssid[], const char password[])
{
#if defined(ARDUINO_ARCH_ESP8266)
Serial.setDebugOutput(true);
#endif

setupWiFi(ssid, password);
syncTime();
}

void AzureIoTHubClass::setupWiFi(const char ssid[], const char password[])
{
#if defined(ARDUINO_SAMD_FEATHER_M0) && defined(WINC_EN)
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
#endif

#if defined(ARDUINO_ARCH_SAMD)
// check for the presence of the shield :
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
#endif

Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("Connected to wifi");
}

void AzureIoTHubClass::syncTime()
{
#if defined(ARDUINO_ARCH_SAMD)
time_t epochTime = (time_t)-1;

NTPClient ntpClient;
ntpClient.begin();

while (true) {
epochTime = ntpClient.getEpochTime("0.pool.ntp.org");

if (epochTime == (time_t)-1) {
Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry.");
delay(5000);
} else {
Serial.print("Fetched NTP epoch time is: ");
Serial.println(epochTime);
break;
}
}

ntpClient.end();

struct timeval tv;
tv.tv_sec = epochTime;
tv.tv_usec = 0;

settimeofday(&tv, NULL);
#elif defined(ARDUINO_ARCH_ESP8266)
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;
}
}
#endif
}
18 changes: 18 additions & 0 deletions src/AzureIoT.h → src/AzureIoTHub.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Arduino. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef AZUREIOTHUB_H
#define AZUREIOTHUB_H

#include "sdk/lock.h"
#include "sdk/threadapi.h"
#include "sdk/serializer.h"
Expand All @@ -9,5 +12,20 @@
#include "sdk/iothub_message.h"
#include "sdk/iothubtransporthttp.h"

#ifdef __cplusplus

class AzureIoTHubClass
{
public:
void begin(const char ssid[], const char password[]);

private:
void setupWiFi(const char ssid[], const char password[]);
void syncTime();
};

extern AzureIoTHubClass AzureIoTHub;

#endif

#endif
File renamed without changes.
File renamed without changes.

0 comments on commit 033bc25

Please sign in to comment.