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

Commit 19ae72f

Browse files
committed
Introduce AzureIoTHubClass to reduce boilerplate in the sketch
1 parent 7d980b4 commit 19ae72f

File tree

9 files changed

+178
-125
lines changed

9 files changed

+178
-125
lines changed

examples/samd/simplesample_http/simplesample_http.ino

-124
This file was deleted.

examples/samd/simplesample_http/simplesample_http.c renamed to examples/simplesample_http/simplesample_http.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ and removing calls to _DoWork will yield the same results. */
1313

1414
#include "AzureIoTHub.h"
1515

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

1818
// Define the Model
1919
BEGIN_NAMESPACE(WeatherStation);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Arduino. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#include <AzureIoTHub.h>
5+
6+
#include "simplesample_http.h"
7+
8+
char ssid[] = "yourNetwork"; // your network SSID (name)
9+
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
10+
11+
void setup() {
12+
Serial.begin(9600);
13+
while(!Serial) {
14+
;
15+
}
16+
17+
AzureIoTHub.begin(ssid, pass);
18+
}
19+
20+
void loop() {
21+
simplesample_http_run();
22+
}
23+

keywords.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#######################################
2+
# Syntax Coloring Map For WiFi
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
AzureIoTHub KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
17+
#######################################
18+
# Constants (LITERAL1)
19+
#######################################

src/AzureIoTHub.cpp

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright (c) Arduino. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#include <time.h>
5+
#include <sys/time.h>
6+
7+
#if defined(ARDUINO_SAMD_FEATHER_M0)
8+
#include <Adafruit_WINC1500.h>
9+
#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000)
10+
#include <WiFi101.h>
11+
#elif defined(ARDUINO_ARCH_ESP8266)
12+
#include <ESP8266WiFi.h>
13+
#else
14+
#error "Unsupported platform"
15+
#endif
16+
17+
#include "util/NTPClient.h"
18+
19+
#include "AzureIoTHub.h"
20+
21+
AzureIoTHubClass AzureIoTHub;
22+
23+
#if defined(ARDUINO_SAMD_FEATHER_M0)
24+
25+
#define WINC_CS 8
26+
#define WINC_IRQ 7
27+
#define WINC_RST 4
28+
#define WINC_EN 2 // or, tie EN to VCC
29+
30+
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
31+
#endif
32+
33+
void AzureIoTHubClass::begin(const char ssid[], const char password[])
34+
{
35+
#if defined(ARDUINO_ARCH_ESP8266)
36+
Serial.setDebugOutput(true);
37+
#endif
38+
39+
setupWiFi(ssid, password);
40+
syncTime();
41+
}
42+
43+
void AzureIoTHubClass::setupWiFi(const char ssid[], const char password[])
44+
{
45+
#if defined(ARDUINO_SAMD_FEATHER_M0) && defined(WINC_EN)
46+
pinMode(WINC_EN, OUTPUT);
47+
digitalWrite(WINC_EN, HIGH);
48+
#endif
49+
50+
#if defined(ARDUINO_ARCH_SAMD)
51+
// check for the presence of the shield :
52+
if (WiFi.status() == WL_NO_SHIELD) {
53+
Serial.println("WiFi shield not present");
54+
// don't continue:
55+
while (true);
56+
}
57+
#endif
58+
59+
Serial.print("Attempting to connect to SSID: ");
60+
Serial.println(ssid);
61+
62+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
63+
while (WiFi.begin(ssid, password) != WL_CONNECTED) {
64+
delay(500);
65+
Serial.print(".");
66+
}
67+
68+
Serial.println("Connected to wifi");
69+
}
70+
71+
void AzureIoTHubClass::syncTime()
72+
{
73+
#if defined(ARDUINO_ARCH_SAMD)
74+
time_t epochTime = (time_t)-1;
75+
76+
NTPClient ntpClient;
77+
ntpClient.begin();
78+
79+
while (true) {
80+
epochTime = ntpClient.getEpochTime("0.pool.ntp.org");
81+
82+
if (epochTime == (time_t)-1) {
83+
Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry.");
84+
delay(5000);
85+
} else {
86+
Serial.print("Fetched NTP epoch time is: ");
87+
Serial.println(epochTime);
88+
break;
89+
}
90+
}
91+
92+
ntpClient.end();
93+
94+
struct timeval tv;
95+
tv.tv_sec = epochTime;
96+
tv.tv_usec = 0;
97+
98+
settimeofday(&tv, NULL);
99+
#elif defined(ARDUINO_ARCH_ESP8266)
100+
time_t epochTime;
101+
102+
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
103+
104+
while (true) {
105+
epochTime = time(NULL);
106+
107+
if (epochTime == 0) {
108+
Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry.");
109+
delay(2000);
110+
} else {
111+
Serial.print("Fetched NTP epoch time is: ");
112+
Serial.println(epochTime);
113+
break;
114+
}
115+
}
116+
#endif
117+
}

src/AzureIoTHub.h

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Copyright (c) Arduino. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4+
#ifndef AZUREIOTHUB_H
5+
#define AZUREIOTHUB_H
6+
47
#include "sdk/lock.h"
58
#include "sdk/threadapi.h"
69
#include "sdk/serializer.h"
@@ -9,5 +12,20 @@
912
#include "sdk/iothub_message.h"
1013
#include "sdk/iothubtransporthttp.h"
1114

15+
#ifdef __cplusplus
16+
17+
class AzureIoTHubClass
18+
{
19+
public:
20+
void begin(const char ssid[], const char password[]);
21+
22+
private:
23+
void setupWiFi(const char ssid[], const char password[]);
24+
void syncTime();
25+
};
26+
27+
extern AzureIoTHubClass AzureIoTHub;
1228

29+
#endif
1330

31+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)