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

Reduce boilerplate in the sketch #9

Closed
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
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 @@ -13,7 +13,7 @@ and removing calls to _DoWork will yield the same results. */

#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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ladyada there is potential to remove use of Adafruit_WINC1500 library in this library and just use WiFi101 if the variant for the "Adafruit Feather M0 WiFi" board defines following WINC1500 pin values like the MKR1000: https://github.com/arduino/ArduinoCore-samd/blob/master/variants/mkr1000/variant.h#L137-L143


#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/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