diff --git a/examples/samd/simplesample_http/simplesample_http.ino b/examples/samd/simplesample_http/simplesample_http.ino deleted file mode 100644 index 23f813e..0000000 --- a/examples/samd/simplesample_http/simplesample_http.ino +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright (c) Arduino. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -#include - -#ifdef ARDUINO_SAMD_FEATHER_M0 -#include -#include -#include -#include -#include -#include -#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) -#include -#endif - -#include "simplesample_http.h" -#include "NTPClient.h" -#include -#include - -#ifdef ARDUINO_SAMD_FEATHER_M0 -// Define the WINC1500 board connections below. -// If you're following the Adafruit WINC1500 board -// guide you don't need to modify these: -#define WINC_CS 8 -#define WINC_IRQ 7 -#define WINC_RST 4 -#define WINC_EN 2 // or, tie EN to VCC -// The SPI pins of the WINC1500 (SCK, MOSI, MISO) should be -// connected to the hardware SPI port of the Arduino. -// On an Uno or compatible these are SCK = #13, MISO = #12, MOSI = #11. -// On an Arduino Zero use the 6-pin ICSP header, see: -// https://www.arduino.cc/en/Reference/SPI - -// Setup the WINC1500 connection with the pins above and the default hardware SPI. -Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST); -#endif - -char ssid[] = ""; // your network SSID (name) -char pass[] = ""; // your network password (use for WPA, or use as key for WEP) -int status = WL_IDLE_STATUS; - -void setup() { - initSerial(); - initWifi(); - initTime(); -} - -void initSerial() { - Serial.begin(9600); - - while(!Serial) { - ; - } -} - -void initWifi() { -#ifdef WINC_EN - pinMode(WINC_EN, OUTPUT); - digitalWrite(WINC_EN, HIGH); -#endif - - // check for the presence of the shield : - if (WiFi.status() == WL_NO_SHIELD) { - Serial.println("WiFi shield not present"); - // don't continue: - while (true); - } - - // attempt to connect to Wifi network: - while (status != WL_CONNECTED) { - Serial.print("Attempting to connect to SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network. Change this line if using open or WEP network: - status = WiFi.begin(ssid, pass); - - if (status != WL_CONNECTED) { - // wait 10 seconds for connection: - delay(10000); - } - } - - Serial.println("Connected to wifi"); -} - -void initTime() { -#ifdef ARDUINO_SAMD_FEATHER_M0 - Adafruit_WINC1500UDP _udp; -#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) - WiFiUDP _udp; -#endif - - 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); - -} - -void loop() { - simplesample_http_run(); -} - diff --git a/examples/samd/simplesample_http/simplesample_http.c b/examples/simplesample_http/simplesample_http.c similarity index 98% rename from examples/samd/simplesample_http/simplesample_http.c rename to examples/simplesample_http/simplesample_http.c index 2e03948..47737a2 100644 --- a/examples/samd/simplesample_http/simplesample_http.c +++ b/examples/simplesample_http/simplesample_http.c @@ -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); diff --git a/examples/samd/simplesample_http/simplesample_http.h b/examples/simplesample_http/simplesample_http.h similarity index 100% rename from examples/samd/simplesample_http/simplesample_http.h rename to examples/simplesample_http/simplesample_http.h diff --git a/examples/simplesample_http/simplesample_http.ino b/examples/simplesample_http/simplesample_http.ino new file mode 100644 index 0000000..a2c73c6 --- /dev/null +++ b/examples/simplesample_http/simplesample_http.ino @@ -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 + +#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(); +} + diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..b79f705 --- /dev/null +++ b/keywords.txt @@ -0,0 +1,19 @@ +####################################### +# Syntax Coloring Map For WiFi +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +AzureIoTHub KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/src/AzureIoTHub.cpp b/src/AzureIoTHub.cpp new file mode 100644 index 0000000..7c09821 --- /dev/null +++ b/src/AzureIoTHub.cpp @@ -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 +#include + +#if defined(ARDUINO_SAMD_FEATHER_M0) +#include +#elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) +#include +#elif defined(ARDUINO_ARCH_ESP8266) +#include +#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 +} diff --git a/src/AzureIoT.h b/src/AzureIoTHub.h similarity index 52% rename from src/AzureIoT.h rename to src/AzureIoTHub.h index 950cf65..66edf09 100644 --- a/src/AzureIoT.h +++ b/src/AzureIoTHub.h @@ -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" @@ -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 diff --git a/examples/samd/simplesample_http/NTPClient.cpp b/src/util/NTPClient.cpp similarity index 100% rename from examples/samd/simplesample_http/NTPClient.cpp rename to src/util/NTPClient.cpp diff --git a/examples/samd/simplesample_http/NTPClient.h b/src/util/NTPClient.h similarity index 100% rename from examples/samd/simplesample_http/NTPClient.h rename to src/util/NTPClient.h