Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit abf84b2

Browse files
Sync Arduino libraries with latest Azure IoT SDK 1.0.41
1 parent 71df21c commit abf84b2

File tree

16 files changed

+664
-187
lines changed

16 files changed

+664
-187
lines changed

‎README.md‎

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
This project has adopted the
2+
[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
3+
For more information see the
4+
[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
5+
[opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
6+
17
# AzureIoTProtocol_MQTT - Azure IoT MQTT protocol library for Arduino
28

3-
[![Build Status](https://travis-ci.org/arduino-libraries/AzureIoTProtocol_MQTT.svg?branch=master)](https://travis-ci.org/arduino-libraries/AzureIoTProtocol_MQTT)
9+
This library is a port of the MQTT protocol from
10+
[Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md)
11+
to Arduino. Together with the AzureIoTHub, it allows you to use several Arduino compatible
12+
boards with Azure IoT MQTT protocol.
13+
14+
## Contributors
15+
16+
### **Pull request caution!**
17+
All of the files in these Arduino library directories are auto-generated
18+
by the Azure IoT C SDK release process, so pull requests cannot be
19+
accepted in this repo.
20+
Your Arduino-specific contributions will be welcomed with open arms in these locations:
21+
* [AzureIoTHub Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTHub)
22+
* [AzureIoTProtocol_HTTP sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTProtocol_HTTP)
23+
* [AzureIoTProtocol_MQTT Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTProtocol_MQTT)
24+
* [AzureIoTUtility Arduino-specific sources](https://github.com/Azure/azure-iot-pal-arduino/tree/master/build_all/base-libraries/AzureIoTUtility)
425

5-
This library is a port of the compact implementation of the MQTT protocol from [Microsoft Azure IoT device SDK for C](https://github.com/Azure/azure-iot-sdks/blob/master/c/readme.md) to Arduino. Together with the AzureIoTHub, 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).
26+
Contributions for code that is not Arduino-specific can be made to the
27+
[Azuure IoT C SDK](https://github.com/azure/azure-iot-sdk-c)
628

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

1838
You should have the following ready before beginning with any board:
19-
- [Setup your IoT hub](https://github.com/Azure/azure-iot-sdks/blob/master/doc/setup_iothub.md)
20-
- [Provision your device and get its credentials](https://github.com/Azure/azure-iot-sdks/blob/master/doc/manage_iot_hub.md)
39+
- [Setup your IoT hub](https://github.com/Azure/azure-iot-device-ecosystem/blob/master/setup_iothub.md)
40+
- [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)
2141
- [Arduino IDE 1.6.12](https://www.arduino.cc/en/Main/Software)
22-
- Install the `AzureIoTHub` library via the Arduino IDE Library Manager
23-
- Install the `AzureIoTUtility` library via the Arduino IDE Library Manager
24-
- Install the `AzureIoTProtocol_MQTT` library via the Arduino IDE Library Manager
42+
- Install the [`AzureIoTHub`](https://github.com/Azure/azure-iot-arduino) library via the Arduino IDE Library Manager
43+
- Install the [`AzureIoTUtility`](https://github.com/Azure/azure-iot-arduino-utility) library via the Arduino IDE Library Manager
44+
- Install the [`AzureIoTProtocol_MQTT`](https://github.com/Azure/azure-iot-arduino-protocol-mqtt) library via the Arduino IDE Library Manager
2545

26-
# Simple Sample Instructions
2746

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

37-
## ESP8266
49+
### ESP8266
3850
##### Sparkfun Thing, Adafruit Feather Huzzah, or generic ESP8266 board
3951

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

46-
2. Open the `simplesample_mqtt` example from the Arduino IDE File->Examples->AzureIoTHub menu.
47-
3. Update the sketch as directed by comments in the sample to support the ESP8266 board.
48-
4. Update Wifi SSID/Password in simplesample_mqtt.ino
58+
2. Open the `simplesample_mqtt` example from the Arduino IDE File->AzureIoTHub->ESP8266->Examples menu.
59+
4. Update Wifi SSID/Password and IoT Hub Connection string in iot_configs.h
4960
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
50-
5. Update IoT Hub Connection string in simplesample_mqtt.c
5161
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.
5262
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.
5363

5464

55-
## Adafruit Feather M0
65+
### Adafruit Feather M0
5666
1. Install Feather M0 board support into your Arduino IDE.
5767
* Start Arduino and open Preferences window.
5868
* 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.
5969
* Open Boards Manager from Tools > Board menu and install `Arduino SAMD Boards` and `Adafruit SAMD Boards` 1.0.7 or later.
6070
* Select your `Adafruit Feather M0` from Tools > Board menu after installation
6171
2. Install the `WiFi101` library from the Arduino IDE Library Manager.
6272
3. Install the `RTCZero` library from the Arduino IDE Library Manager.
63-
4. Install the `NTPClient` library from the Arduino IDE Library Manager.
64-
5. Open the `simplesample_mqtt` example from the Arduino IDE File->Examples->AzureIoTHub menu.
65-
6. Update the sketch as directed by comments in the sample to support the ESP8266 board.
66-
7. Update Wifi SSID/Password in simplesample_mqtt.ino
73+
5. Open the `simplesample_mqtt` example from the Arduino IDE File->AzureIoTHub->M0->Examples menu.
74+
4. Update Wifi SSID/Password and IoT Hub Connection string in iot_configs.h
6775
* Ensure you are using a wifi network that does not require additional manual steps after connection, such as opening a web browser.
68-
8. Update IoT Hub Connection string in simplesample_mqtt.c
6976
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.
7077

7178
## License

‎examples/README.md‎

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### simplesample_mqtt
2+
3+
Instructions for this sample are
4+
[here in the Azure IoT MQTT protocol library for Arduino.](https://github.com/Azure/azure-iot-arduino-protocol-mqtt)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
#ifndef IOT_CONFIGS_H
5+
#define IOT_CONFIGS_H
6+
7+
/**
8+
* WiFi setup
9+
*/
10+
#define IOT_CONFIG_WIFI_SSID "<Your WiFi network SSID or name>"
11+
#define IOT_CONFIG_WIFI_PASSWORD "<Your WiFi network WPA password or WEP key>"
12+
13+
/**
14+
* Find under Microsoft Azure IoT Suite -> DEVICES -> <your device> -> Device Details and Authentication Keys
15+
* String containing Hostname, Device Id & Device Key in the format:
16+
* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
17+
*/
18+
#define IOT_CONFIG_CONNECTION_STRING "HostName=<host_name>.azure-devices.net;DeviceId=<device_id>;SharedAccessKey=<device_key>"
19+
20+
#endif /* IOT_CONFIGS_H */

‎examples/simplesample_mqtt/simplesample_mqtt.c‎ renamed to ‎examples/esp8266/simplesample_mqtt/simplesample_mqtt.c‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,13 @@ That does not mean that MQTT only works with the _LL APIs.
1111
Simply changing the using the convenience layer (functions not having _LL)
1212
and removing calls to _DoWork will yield the same results. */
1313

14-
#ifdef ARDUINO
1514
#include "AzureIoTHub.h"
16-
#else
17-
#include "azure_c_shared_utility/threadapi.h"
18-
#include "azure_c_shared_utility/platform.h"
19-
#include "serializer.h"
20-
#include "iothub_client_ll.h"
21-
#include "iothubtransportmqtt.h"
22-
#endif
23-
24-
#ifdef MBED_BUILD_TIMESTAMP
25-
#define SET_TRUSTED_CERT_IN_SAMPLES
26-
#endif // MBED_BUILD_TIMESTAMP
27-
28-
#ifdef SET_TRUSTED_CERT_IN_SAMPLES
29-
#include "certs.h"
30-
#endif // SET_TRUSTED_CERT_IN_SAMPLES
15+
#include "iot_configs.h"
3116

3217

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

3722
// Define the Model
3823
BEGIN_NAMESPACE(WeatherStation);
File renamed without changes.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
// Please Use Arduino IDE 1.6.8 or later.
5+
6+
#include "iot_configs.h"
7+
8+
#include <stdlib.h>
9+
#include <stdio.h>
10+
#include <stdint.h>
11+
#include <time.h>
12+
#include <sys/time.h>
13+
14+
// for ESP8266
15+
#include <ESP8266WiFi.h>
16+
#include <WiFiClientSecure.h>
17+
#include <WiFiUdp.h>
18+
19+
20+
#include "simplesample_mqtt.h"
21+
#include "iot_configs.h"
22+
23+
#include <AzureIoTHub.h>
24+
#include <AzureIoTProtocol_MQTT.h>
25+
26+
27+
28+
static char ssid[] = IOT_CONFIG_WIFI_SSID;
29+
static char pass[] = IOT_CONFIG_WIFI_PASSWORD;
30+
31+
int status = WL_IDLE_STATUS;
32+
33+
void setup() {
34+
// The Feather M0 loses it's COMn connection with every reset.
35+
// This 10 s delay allows you to reselect the COM port and open the serial monitor window.
36+
delay(10000);
37+
38+
initSerial();
39+
40+
initWifi();
41+
42+
initTime();
43+
}
44+
45+
void loop() {
46+
simplesample_mqtt_run();
47+
}
48+
49+
void initSerial() {
50+
// Start serial and initialize stdout
51+
Serial.begin(115200);
52+
//Serial.setDebugOutput(true);
53+
}
54+
55+
void initWifi() {
56+
// Attempt to connect to Wifi network:
57+
Serial.print("\r\n\r\nAttempting to connect to SSID: ");
58+
Serial.println(ssid);
59+
60+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
61+
status = WiFi.begin(ssid, pass);
62+
63+
while (WiFi.status() != WL_CONNECTED) {
64+
delay(500);
65+
Serial.print(".");
66+
}
67+
68+
Serial.println("\r\nConnected to wifi");
69+
}
70+
71+
///////////////////////////////////////////////////////////////////////////////////////////////////
72+
void initTime() {
73+
time_t epochTime;
74+
75+
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
76+
77+
while (true) {
78+
epochTime = time(NULL);
79+
80+
if (epochTime == 0) {
81+
Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry.");
82+
delay(2000);
83+
} else {
84+
Serial.print("Fetched NTP epoch time is: ");
85+
Serial.println(epochTime);
86+
break;
87+
}
88+
}
89+
}
90+
91+
92+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
#if defined(ARDUINO_ARCH_SAMD)
5+
#include "NTPClient.h"
6+
7+
#define LOCAL_UDP_PORT 2390
8+
9+
NTPClient::NTPClient() :
10+
_udp()
11+
{
12+
}
13+
14+
int NTPClient::begin()
15+
{
16+
return _udp.begin(LOCAL_UDP_PORT);
17+
}
18+
19+
uint32_t NTPClient::getEpochTime(const char* host, int port, unsigned long timeout)
20+
{
21+
if (host == NULL || port < 1) {
22+
return (uint32_t)-1;
23+
}
24+
25+
prepareRequest();
26+
sendRequest(host, port);
27+
28+
if (!receiveResponse(timeout)) {
29+
return (uint32_t)-1;
30+
}
31+
32+
return parseResponse();
33+
}
34+
35+
void NTPClient::end()
36+
{
37+
_udp.stop();
38+
}
39+
40+
void NTPClient::prepareRequest()
41+
{
42+
memset(_buffer, 0, NTP_PACKET_SIZE);
43+
44+
// Initialize values needed to form NTP request
45+
_buffer[0] = 0b11100011; // LI, Version, Mode
46+
_buffer[1] = 0; // Stratum, or type of clock
47+
_buffer[2] = 6; // Polling Interval
48+
_buffer[3] = 0xEC; // Peer Clock Precision
49+
50+
// 8 bytes of zero for Root Delay & Root Dispersion
51+
52+
_buffer[12] = 49;
53+
_buffer[13] = 0x4E;
54+
_buffer[14] = 49;
55+
_buffer[15] = 52;
56+
}
57+
58+
void NTPClient::sendRequest(const char* host, int port)
59+
{
60+
_udp.beginPacket(host, port);
61+
_udp.write(_buffer, NTP_PACKET_SIZE);
62+
_udp.endPacket();
63+
}
64+
65+
int NTPClient::receiveResponse(unsigned long timeout)
66+
{
67+
long start = millis();
68+
int size = 0;
69+
70+
while(size == 0 && (millis() - start) < timeout) {
71+
size = _udp.parsePacket();
72+
}
73+
74+
if (size != NTP_PACKET_SIZE) {
75+
return 0;
76+
}
77+
78+
_udp.read(_buffer, NTP_PACKET_SIZE);
79+
80+
return 1;
81+
}
82+
83+
uint32_t NTPClient::parseResponse()
84+
{
85+
uint16_t high = word(_buffer[40], _buffer[41]);
86+
uint16_t low = word(_buffer[42], _buffer[43]);
87+
uint32_t ntpTime = high << 16 | low; // since 1900
88+
uint32_t epoch = ntpTime - 2208988800UL; // since 1970
89+
90+
return epoch;
91+
}
92+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 <WiFi101.h>
5+
#include <WiFiUdp.h>
6+
7+
#ifndef NTPCLIENT_H
8+
#define NTPCLIENT_H
9+
10+
#define NTP_PACKET_SIZE 48
11+
#define NTP_PORT 123
12+
13+
#define DEFAULT_NTP_TIMEOUT 10000
14+
15+
class NTPClient
16+
{
17+
public:
18+
NTPClient();
19+
int begin();
20+
uint32_t getEpochTime(const char* host, int port = NTP_PORT, unsigned long timeout = DEFAULT_NTP_TIMEOUT);
21+
void end();
22+
23+
private:
24+
void prepareRequest();
25+
void sendRequest(const char* host, int port);
26+
int receiveResponse(unsigned long timeout);
27+
uint32_t parseResponse();
28+
29+
char _buffer[NTP_PACKET_SIZE];
30+
WiFiUDP _udp;
31+
};
32+
33+
#endif // NTPCLIENT_H

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /