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

Commit

Permalink
Sync Arduino libraries with latest Azure IoT SDK 1.0.41
Browse files Browse the repository at this point in the history
  • Loading branch information
JetstreamRoySprowl committed Nov 3, 2017
1 parent 581bcdf commit 5e2d772
Show file tree
Hide file tree
Showing 16 changed files with 612 additions and 103 deletions.
66 changes: 36 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
This project has adopted the
[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the
[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
[opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

# AzureIoTHub - Azure IoT Hub library for Arduino

This library is a port of the [Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md) to Arduino. It allows you to use several Arduino compatible boards with Azure IoT Hub. Please submit any contribution directly to [azure-iot-sdks](https://github.com/Azure/azure-iot-sdks).
This library is a port of the
[Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md)
to Arduino. It allows you to use several Arduino compatible boards with Azure IoT Hub.

Currently supported hardware:
## Contributors

### **Pull request caution!**
With the exception of the `.travis.yml` and `keywords.txt` files, all of
the files in these Arduino library directories are auto-generated by the SDK release process, so pulls cannot be
accepted in this repo for files other than `.travis.yml` and `keywords.txt`.
Your Arduino-specific contributions will be welcomed with open arms in these locations:
* [AzureIoTHub Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTHub)
* [AzureIoTProtocol_HTTP sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTProtocol_HTTP)
* [AzureIoTProtocol_MQTT Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTProtocol_MQTT)
* [AzureIoTUtility Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTUtility)

Contributions for code that is not Arduino-specific can be made to the
[Azuure IoT C SDK](https://github.com/azure/azure-iot-sdk-c)

## Currently supported hardware
- Atmel SAMD Based boards
- Arduino/Genuino [MKR1000](https://www.arduino.cc/en/Main/ArduinoMKR1000)
- Arduino/Genuino [Zero](https://www.arduino.cc/en/Main/ArduinoBoardZero) and [WiFi Shield 101](https://www.arduino.cc/en/Main/ArduinoWiFiShield101)
- Adafruit [Feather M0](https://www.adafruit.com/products/3010)
- ESP8266 based boards with [esp8266/arduino](https://github.com/esp8266/arduino)
- SparkFun [Thing](https://www.sparkfun.com/products/13711)
Expand All @@ -16,25 +35,17 @@ Currently supported hardware:
## Prerequisites

You should have the following ready before beginning with any board:
- [Setup your IoT hub](https://github.com/Azure/azure-iot-sdks/blob/master/doc/setup_iothub.md)
- [Provision your device and get its credentials](https://github.com/Azure/azure-iot-sdks/blob/master/doc/manage_iot_hub.md)
- [Setup your IoT hub](https://github.com/Azure/azure-iot-device-ecosystem/blob/master/setup_iothub.md)
- [Provision your device and get its credentials](https://github.com/Azure/azure-iot-device-ecosystem/blob/master/setup_iothub.md#create-new-device-in-the-iot-hub-device-identity-registry)
- [Arduino IDE 1.6.12](https://www.arduino.cc/en/Main/Software)
- Install the `AzureIoTHub` library via the Arduino IDE Library Manager
- Install the `AzureIoTUtility` library via the Arduino IDE Library Manager
- Install the `AzureIoTProtocol_MQTT` library via the Arduino IDE Library Manager
- Install the [`AzureIoTHub`](https://github.com/Azure/azure-iot-arduino) library via the Arduino IDE Library Manager
- Install the [`AzureIoTUtility`](https://github.com/Azure/azure-iot-arduino-utility) library via the Arduino IDE Library Manager
- Install the [`AzureIoTProtocol_HTTP`](https://github.com/Azure/azure-iot-arduino-protocol-http) library via the Arduino IDE Library Manager

# Simple Sample Instructions

## MKR1000 or Zero + Wifi101
1. Open the `simplesample_http` example from the Arduino IDE File->Examples->AzureIoTHub menu.
2. Install the `WiFi101` library from the Arduino IDE Library Manager.
3. Install the `NTPClient` library from the Arduino IDE Library Manager.
4. Install the `RTCZero` library from the Arduino IDE Library Manager.
5. Update Wifi SSID/Password in simplesample_http.ino
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
6. Update IoT Hub Connection string in simplesample_http.c
## Simple Sample Instructions

## ESP8266
### ESP8266
##### Sparkfun Thing, Adafruit Feather Huzzah, or generic ESP8266 board

1. Install esp8266 board support into your Arduino IDE.
Expand All @@ -43,29 +54,24 @@ You should have the following ready before beginning with any board:
* Open Boards Manager from Tools > Board menu and install esp8266 platform 2.2.0 or later
* Select your ESP8266 board from Tools > Board menu after installation

2. Open the `simplesample_http` example from the Arduino IDE File->Examples->AzureIoTHub menu.
3. Update the sketch as directed by comments in the sample to support the ESP8266 board.
4. Update Wifi SSID/Password in simplesample_http.ino
2. Open the `simplesample_http` example from the Arduino IDE File->AzureIoTHub->ESP8266->Examples menu.
4. Update Wifi SSID/Password and IoT Hub Connection string in iot_configs.h
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
5. Update IoT Hub Connection string in simplesample_http.c
6. Access the [SparkFun Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-thingdev-getstartedkit/) tutorial to learn more about Microsoft Sparkfun Dev Kit.
7. Access the [Huzzah Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-huzzah-getstartedkit/) tutorial to learn more about Microsoft Huzzah Dev Kit.


## Adafruit Feather M0
### Adafruit Feather M0
1. Install Feather M0 board support into your Arduino IDE.
* Start Arduino and open Preferences window.
* Enter `https://adafruit.github.io/arduino-board-index/package_adafruit_index.json` into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
* Open Boards Manager from Tools > Board menu and install `Arduino SAMD Boards` and `Adafruit SAMD Boards` 1.0.7 or later.
* Select your `Adafruit Feather M0` from Tools > Board menu after installation
2. Install the `WiFi101` library from the Arduino IDE Library Manager.
3. Install the `RTCZero` library from the Arduino IDE Library Manager.
4. Install the `NTPClient` library from the Arduino IDE Library Manager.
5. Open the `simplesample_http` example from the Arduino IDE File->Examples->AzureIoTHub menu.
6. Update the sketch as directed by comments in the sample to support the ESP8266 board.
7. Update Wifi SSID/Password in simplesample_http.ino
5. Open the `simplesample_http` example from the Arduino IDE File->AzureIoTHub->M0->Examples menu.
4. Update Wifi SSID/Password and IoT Hub Connection string in iot_configs.h
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
8. Update IoT Hub Connection string in simplesample_http.c
9. Access the [Feather M0 WiFi Get Started](https://azure.microsoft.com/en-us/documentation/samples/iot-hub-c-m0wifi-getstartedkit/) tutorial to learn more about Microsoft Feather M0 WiFi Dev Kit.

## License
Expand Down
3 changes: 0 additions & 3 deletions examples/README.md

This file was deleted.

4 changes: 4 additions & 0 deletions examples/esp8266/simplesample_http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### simplesample_http

Instructions for this sample are
[here in the Azure IoT HTTP protocol library for Arduino.](https://github.com/Azure/azure-iot-arduino-protocol-http)
20 changes: 20 additions & 0 deletions examples/esp8266/simplesample_http/iot_configs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef IOT_CONFIGS_H
#define IOT_CONFIGS_H

/**
* WiFi setup
*/
#define IOT_CONFIG_WIFI_SSID "<Your WiFi network SSID or name>"
#define IOT_CONFIG_WIFI_PASSWORD "<Your WiFi network WPA password or WEP key>"

/**
* Find under Microsoft Azure IoT Suite -> DEVICES -> <your device> -> Device Details and Authentication Keys
* String containing Hostname, Device Id & Device Key in the format:
* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
*/
#define IOT_CONFIG_CONNECTION_STRING "HostName=<host_name>.azure-devices.net;DeviceId=<device_id>;SharedAccessKey=<device_key>"

#endif /* IOT_CONFIGS_H */
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,19 @@

#include <stdio.h>
#include <stdint.h>
#include "iot_configs.h"

/* This sample uses the _LL APIs of iothub_client for example purposes.
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. */

#ifdef ARDUINO
#include "AzureIoTHub.h"
#else
#include "azure_c_shared_utility/threadapi.h"
#include "azure_c_shared_utility/platform.h"
#include "serializer.h"
#include "iothub_client_ll.h"
#include "iothubtransporthttp.h"
#endif

#ifdef MBED_BUILD_TIMESTAMP
#define SET_TRUSTED_CERT_IN_SAMPLES
#endif // MBED_BUILD_TIMESTAMP

#ifdef SET_TRUSTED_CERT_IN_SAMPLES
#include "certs.h"
#endif // SET_TRUSTED_CERT_IN_SAMPLES

/*String containing Hostname, Device Id & Device Key in the format: */
/* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
static const char* connectionString = "[device connection string]";
static const char* connectionString = IOT_CONFIG_CONNECTION_STRING;

// Define the Model
BEGIN_NAMESPACE(WeatherStation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,21 @@
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include <SPI.h>
#ifdef ARDUINO_ARCH_ESP8266

// for ESP8266
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#else
#include <WiFi101.h>
#include <WiFiSSLClient.h>
#include <WiFiUdp.h>
#include <NTPClient.h>
#endif

#ifdef ARDUINO_SAMD_FEATHER_M0
// For the Adafruit WINC1500 we need to create our own WiFi instance
// Define the WINC1500 board connections below.
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC
#endif

#include <AzureIoTHub.h>
#include <AzureIoTUtility.h>
#include <AzureIoTProtocol_HTTP.h>

#include "simplesample_http.h"
#include "iot_configs.h"

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

void setup() {
initSerial();
Expand All @@ -53,22 +38,10 @@ void loop() {
void initSerial() {
// Start serial and initialize stdout
Serial.begin(115200);
#ifdef ARDUINO_ARCH_ESP8266
Serial.setDebugOutput(true);
#endif
}

void initWifi() {
#ifdef ARDUINO_SAMD_FEATHER_M0
//Configure pins for Adafruit ATWINC1500 Feather
Serial.println(F("WINC1500 on FeatherM0 detected."));
Serial.println(F("Setting pins for WiFi101 library (WINC1500 on FeatherM0)"));
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
// for the Adafruit WINC1500 we need to enable the chip
pinMode(WINC_EN, OUTPUT);
digitalWrite(WINC_EN, HIGH);
Serial.println(F("Enabled WINC1500 interface for FeatherM0"));
#endif

// check for the presence of the shield :
if (WiFi.status() == WL_NO_SHIELD) {
Expand All @@ -94,26 +67,6 @@ void initWifi() {
}

void initTime() {
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_FEATHER_M0)
WiFiUDP ntpUdp;

NTPClient ntpClient(ntpUdp);

ntpClient.begin();

while (!ntpClient.update()) {
Serial.println("Fetching NTP epoch time failed! Waiting 5 seconds to retry.");
delay(5000);
}

ntpClient.end();

unsigned long epochTime = ntpClient.getEpochTime();

Serial.print("Fetched NTP epoch time is: ");
Serial.println(epochTime);

#elif ARDUINO_ARCH_ESP8266
time_t epochTime;

configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Expand All @@ -130,5 +83,4 @@ void initTime() {
break;
}
}
#endif
}
92 changes: 92 additions & 0 deletions examples/samd/simplesample_http/NTPClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) Arduino. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if defined(ARDUINO_ARCH_SAMD)
#include "NTPClient.h"

#define LOCAL_UDP_PORT 2390

NTPClient::NTPClient() :
_udp()
{
}

int NTPClient::begin()
{
return _udp.begin(LOCAL_UDP_PORT);
}

uint32_t NTPClient::getEpochTime(const char* host, int port, unsigned long timeout)
{
if (host == NULL || port < 1) {
return (uint32_t)-1;
}

prepareRequest();
sendRequest(host, port);

if (!receiveResponse(timeout)) {
return (uint32_t)-1;
}

return parseResponse();
}

void NTPClient::end()
{
_udp.stop();
}

void NTPClient::prepareRequest()
{
memset(_buffer, 0, NTP_PACKET_SIZE);

// Initialize values needed to form NTP request
_buffer[0] = 0b11100011; // LI, Version, Mode
_buffer[1] = 0; // Stratum, or type of clock
_buffer[2] = 6; // Polling Interval
_buffer[3] = 0xEC; // Peer Clock Precision

// 8 bytes of zero for Root Delay & Root Dispersion

_buffer[12] = 49;
_buffer[13] = 0x4E;
_buffer[14] = 49;
_buffer[15] = 52;
}

void NTPClient::sendRequest(const char* host, int port)
{
_udp.beginPacket(host, port);
_udp.write(_buffer, NTP_PACKET_SIZE);
_udp.endPacket();
}

int NTPClient::receiveResponse(unsigned long timeout)
{
long start = millis();
int size = 0;

while(size == 0 && (millis() - start) < timeout) {
size = _udp.parsePacket();
}

if (size != NTP_PACKET_SIZE) {
return 0;
}

_udp.read(_buffer, NTP_PACKET_SIZE);

return 1;
}

uint32_t NTPClient::parseResponse()
{
uint16_t high = word(_buffer[40], _buffer[41]);
uint16_t low = word(_buffer[42], _buffer[43]);
uint32_t ntpTime = high << 16 | low; // since 1900
uint32_t epoch = ntpTime - 2208988800UL; // since 1970

return epoch;
}
#endif
33 changes: 33 additions & 0 deletions examples/samd/simplesample_http/NTPClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Arduino. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <WiFi101.h>
#include <WiFiUdp.h>

#ifndef NTPCLIENT_H
#define NTPCLIENT_H

#define NTP_PACKET_SIZE 48
#define NTP_PORT 123

#define DEFAULT_NTP_TIMEOUT 10000

class NTPClient
{
public:
NTPClient();
int begin();
uint32_t getEpochTime(const char* host, int port = NTP_PORT, unsigned long timeout = DEFAULT_NTP_TIMEOUT);
void end();

private:
void prepareRequest();
void sendRequest(const char* host, int port);
int receiveResponse(unsigned long timeout);
uint32_t parseResponse();

char _buffer[NTP_PACKET_SIZE];
WiFiUDP _udp;
};

#endif // NTPCLIENT_H
Loading

0 comments on commit 5e2d772

Please sign in to comment.