-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzure-IoT-Hub-ESP8266-Easy-Connect.ino
129 lines (104 loc) · 3.4 KB
/
Azure-IoT-Hub-ESP8266-Easy-Connect.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Please use an Arduino IDE 1.6.8 or greater
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include <AzureIoTHub.h>
#include <AzureIoTProtocol_MQTT.h>
#include <AzureIoTUtility.h>
#include "config.h"
static bool messagePending = false;
static bool messageSending = true;
static char *connectionString = CONNECTION_STRING;
static char *ssid = WIFI_SSID;
static char *pass = WIFI_PASS;
static int interval = INTERVAL;
void blinkLED()
{
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
}
void initWifi()
{
// Attempt to connect to Wifi network:
Serial.printf("Attempting to connect to SSID: %s.\r\n", ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
// Get Mac Address and show it.
// WiFi.macAddress(mac) save the mac address into a six length array, but the endian may be different. The huzzah board should
// start from mac[0] to mac[5], but some other kinds of board run in the oppsite direction.
uint8_t mac[6];
WiFi.macAddress(mac);
Serial.printf("You device with MAC address %02x:%02x:%02x:%02x:%02x:%02x connects to %s failed! Waiting 10 seconds to retry.\r\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], ssid);
WiFi.begin(ssid, pass);
delay(10000);
}
Serial.printf("Connected to wifi %s.\r\n", ssid);
}
void initTime()
{
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.printf("Fetched NTP epoch time is: %lu.\r\n", epochTime);
break;
}
}
}
void initSerial()
{
// Start serial and initialize stdout
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("Serial successfully inited.");
}
static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
void setup()
{
pinMode(LED_PIN, OUTPUT);
initSerial();
delay(2000);
initWifi();
initTime();
/*
* AzureIotHub library remove AzureIoTHubClient class in 1.0.34, so we remove the code below to avoid
* compile error
*/
// initIoThubClient();
iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol);
if (iotHubClientHandle == NULL)
{
Serial.println("Failed on IoTHubClient_CreateFromConnectionString.");
while (1);
}
IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, receiveMessageCallback, NULL);
IoTHubClient_LL_SetDeviceMethodCallback(iotHubClientHandle, deviceMethodCallback, NULL);
IoTHubClient_LL_SetDeviceTwinCallback(iotHubClientHandle, twinCallback, NULL);
}
static int messageCount = 1;
void loop()
{
if (!messagePending && messageSending)
{
char messagePayload[MESSAGE_MAX_LEN] = "YOUR_IOT_HUB_MESSAGE_HERE";
sendMessage(iotHubClientHandle, messagePayload);
messageCount++;
delay(interval);
}
IoTHubClient_LL_DoWork(iotHubClientHandle);
delay(10);
}