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

Commit e0136d6

Browse files
committed
Merge pull request #9 from AttunixCorp/master
ESP8266 (Huzzah and Thing Dev) and SAMD (Feather M0) remote monitorin…
2 parents b10ee9d + a332a6c commit e0136d6

22 files changed

+2204
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// This example only works with Arduino IDE 1.6.8 or later.
5+
6+
#include <ESP8266WiFi.h>
7+
#include <time.h>
8+
#include "command_center_http.h"
9+
10+
11+
const char ssid[] = "[SSID]"; // your WiFi SSID (name)
12+
const char pass[] = "[PASSWORD]"; // your WiFi password (use for WPA, or use as key for WEP)
13+
const char connectionString[] = "HostName=[HubName].azure-devices.net;DeviceId=[DeviceName];SharedAccessKey=[KEY]";
14+
15+
int status = WL_IDLE_STATUS;
16+
17+
///////////////////////////////////////////////////////////////////////////////////////////////////
18+
void setup() {
19+
initSerial();
20+
initWifi();
21+
initTime();
22+
23+
// This function doesn't exit.
24+
simplesample_http_run();
25+
}
26+
27+
///////////////////////////////////////////////////////////////////////////////////////////////////
28+
void loop() {
29+
// Not used.
30+
}
31+
32+
///////////////////////////////////////////////////////////////////////////////////////////////////
33+
void initSerial() {
34+
// Start serial and initialize stdout
35+
Serial.begin(115200);
36+
Serial.setDebugOutput(true);
37+
}
38+
39+
///////////////////////////////////////////////////////////////////////////////////////////////////
40+
void initWifi() {
41+
// Attempt to connect to Wifi network:
42+
Serial.print("Attempting to connect to SSID: ");
43+
Serial.println(ssid);
44+
45+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
46+
status = WiFi.begin(ssid, pass);
47+
48+
while (WiFi.status() != WL_CONNECTED) {
49+
delay(500);
50+
Serial.print(".");
51+
}
52+
53+
Serial.println("Connected to wifi");
54+
}
55+
56+
///////////////////////////////////////////////////////////////////////////////////////////////////
57+
void initTime() {
58+
time_t epochTime;
59+
60+
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
61+
62+
while (true) {
63+
epochTime = time(NULL);
64+
65+
if (epochTime == 0) {
66+
Serial.println("Fetching NTP epoch time failed! Waiting 2 seconds to retry.");
67+
delay(2000);
68+
} else {
69+
Serial.print("Fetched NTP epoch time is: ");
70+
Serial.println(epochTime);
71+
break;
72+
}
73+
}
74+
}
75+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////////
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
5+
#include <stdlib.h>
6+
7+
#include <stdio.h>
8+
#include <stdint.h>
9+
#include <pgmspace.h>
10+
#include <Arduino.h>
11+
#include <time.h>
12+
13+
#include "dht22.h"
14+
#include "command_center_http.h"
15+
16+
/* This sample uses the _LL APIs of iothub_client for example purposes.
17+
That does not mean that HTTP only works with the _LL APIs.
18+
Simply changing the using the convenience layer (functions not having _LL)
19+
and removing calls to _DoWork will yield the same results. */
20+
21+
#include "AzureIoT.h"
22+
23+
24+
//static const char* connectionString = "HostName=[host].azure-devices.net;DeviceId=[device];SharedAccessKey=[key]";
25+
static const char connectionString[] = "HostName=[host].azure-devices.net;DeviceId=[device];SharedAccessKey=[key]";
26+
27+
static int redLedPin = 12;
28+
static int redLedState = LOW;
29+
static int greenLedPin = 13;
30+
static int greenLedState = LOW;
31+
32+
// Define the Model
33+
BEGIN_NAMESPACE(WeatherStation);
34+
35+
DECLARE_MODEL(ContosoAnemometer,
36+
WITH_DATA(ascii_char_ptr, DeviceId),
37+
WITH_DATA(ascii_char_ptr, EventTime),
38+
WITH_DATA(int, WindSpeed),
39+
WITH_DATA(int, MTemperature),
40+
WITH_DATA(int, Humidity),
41+
WITH_ACTION(TurnFanOn),
42+
WITH_ACTION(TurnFanOff),
43+
WITH_ACTION(SetAirResistance, int, Position)
44+
);
45+
46+
END_NAMESPACE(WeatherStation);
47+
48+
DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES)
49+
50+
EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
51+
{
52+
(void)device;
53+
54+
LogInfo("Toggling red LED.\r\n");
55+
if (redLedState != LOW)
56+
{
57+
redLedState = LOW;
58+
}
59+
else
60+
{
61+
redLedState = HIGH;
62+
}
63+
digitalWrite(redLedPin, redLedState);
64+
65+
return EXECUTE_COMMAND_SUCCESS;
66+
}
67+
68+
EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
69+
{
70+
(void)device;
71+
72+
LogInfo("Toggling green LED.\r\n");
73+
if (greenLedState != LOW)
74+
{
75+
greenLedState = LOW;
76+
}
77+
else
78+
{
79+
greenLedState = HIGH;
80+
}
81+
digitalWrite(greenLedPin, greenLedState);
82+
83+
return EXECUTE_COMMAND_SUCCESS;
84+
}
85+
86+
EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
87+
{
88+
(void)device;
89+
LogInfo("Setting Air Resistance Position to %d.\r\n", Position);
90+
return EXECUTE_COMMAND_SUCCESS;
91+
}
92+
93+
void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
94+
{
95+
int messageTrackingId = (intptr_t)userContextCallback;
96+
97+
LogInfo("Message Id: %d Received.\r\n", messageTrackingId);
98+
99+
LogInfo("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
100+
}
101+
102+
static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
103+
{
104+
static unsigned int messageTrackingId;
105+
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
106+
if (messageHandle == NULL)
107+
{
108+
LogInfo("unable to create a new IoTHubMessage\r\n");
109+
}
110+
else
111+
{
112+
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
113+
{
114+
LogInfo("failed to hand over the message to IoTHubClient");
115+
}
116+
else
117+
{
118+
LogInfo("IoTHubClient accepted the message for delivery\r\n");
119+
}
120+
IoTHubMessage_Destroy(messageHandle);
121+
}
122+
free((void*)buffer);
123+
messageTrackingId++;
124+
}
125+
126+
/*this function "links" IoTHub to the serialization library*/
127+
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
128+
{
129+
IOTHUBMESSAGE_DISPOSITION_RESULT result;
130+
const unsigned char* buffer;
131+
size_t size;
132+
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
133+
{
134+
LogInfo("unable to IoTHubMessage_GetByteArray\r\n");
135+
result = EXECUTE_COMMAND_ERROR;
136+
}
137+
else
138+
{
139+
/*buffer is not zero terminated*/
140+
char* temp = malloc(size + 1);
141+
if (temp == NULL)
142+
{
143+
LogInfo("failed to malloc\r\n");
144+
result = EXECUTE_COMMAND_ERROR;
145+
}
146+
else
147+
{
148+
memcpy(temp, buffer, size);
149+
temp[size] = '\0';
150+
EXECUTE_COMMAND_RESULT executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
151+
result =
152+
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
153+
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
154+
IOTHUBMESSAGE_REJECTED;
155+
free(temp);
156+
}
157+
}
158+
return result;
159+
}
160+
161+
void simplesample_http_run(void)
162+
{
163+
initDht();
164+
165+
digitalWrite(redLedPin, redLedState);
166+
pinMode(redLedPin, OUTPUT);
167+
168+
digitalWrite(greenLedPin, greenLedState);
169+
pinMode(greenLedPin, OUTPUT);
170+
171+
if (serializer_init(NULL) != SERIALIZER_OK)
172+
{
173+
LogInfo("Failed on serializer_init\r\n");
174+
}
175+
else
176+
{
177+
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
178+
srand((unsigned int)time(NULL));
179+
int avgWindSpeed = 10.0;
180+
181+
if (iotHubClientHandle == NULL)
182+
{
183+
LogInfo("Failed on IoTHubClient_LL_Create\r\n");
184+
}
185+
else
186+
{
187+
unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/
188+
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
189+
{
190+
LogInfo("failure to set option \"MinimumPollingTime\"\r\n");
191+
}
192+
193+
#ifdef MBED_BUILD_TIMESTAMP
194+
// For mbed add the certificate information
195+
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
196+
{
197+
LogInfo("failure to set option \"TrustedCerts\"\r\n");
198+
}
199+
#endif // MBED_BUILD_TIMESTAMP
200+
201+
ContosoAnemometer* myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
202+
if (myWeather == NULL)
203+
{
204+
LogInfo("Failed on CREATE_MODEL_INSTANCE\r\n");
205+
}
206+
else
207+
{
208+
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
209+
{
210+
LogInfo("unable to IoTHubClient_SetMessageCallback\r\n");
211+
}
212+
else
213+
{
214+
myWeather->DeviceId = "myFirstDevice";
215+
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
216+
{
217+
unsigned char* destination;
218+
size_t destinationSize;
219+
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed) != IOT_AGENT_OK)
220+
{
221+
LogInfo("Failed to serialize\r\n");
222+
}
223+
else
224+
{
225+
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
226+
if (messageHandle == NULL)
227+
{
228+
LogInfo("unable to create a new IoTHubMessage\r\n");
229+
}
230+
else
231+
{
232+
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
233+
{
234+
LogInfo("failed to hand over the message to IoTHubClient\r\n");
235+
}
236+
else
237+
{
238+
LogInfo("IoTHubClient accepted the message for delivery\r\n");
239+
}
240+
241+
IoTHubMessage_Destroy(messageHandle);
242+
}
243+
free(destination);
244+
}
245+
}
246+
247+
/* wait for commands */
248+
long Prev_time_ms = millis();
249+
char buff[11];
250+
int timeNow = 0;
251+
252+
while (1)
253+
{
254+
long Curr_time_ms = millis();
255+
if (Curr_time_ms >= Prev_time_ms + 5000)
256+
{
257+
Prev_time_ms = Curr_time_ms;
258+
259+
timeNow = (int)time(NULL);
260+
sprintf(buff, "%d", timeNow);
261+
262+
float Temp_c__f, Humi_pct__f;
263+
getNextSample(&Temp_c__f, &Humi_pct__f);
264+
myWeather->DeviceId = "myFirstDevice";
265+
myWeather->EventTime = timeNow;
266+
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
267+
myWeather->MTemperature = (int)Temp_c__f;
268+
myWeather->Humidity = (int)Humi_pct__f;
269+
{
270+
unsigned char* destination;
271+
size_t destinationSize;
272+
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->EventTime, myWeather->WindSpeed, myWeather->MTemperature, myWeather->Humidity) != IOT_AGENT_OK)
273+
{
274+
LogInfo("Failed to serialize\r\n");
275+
}
276+
else
277+
{
278+
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
279+
if (messageHandle == NULL)
280+
{
281+
LogInfo("unable to create a new IoTHubMessage\r\n");
282+
}
283+
else
284+
{
285+
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
286+
{
287+
LogInfo("failed to hand over the message to IoTHubClient\r\n");
288+
}
289+
else
290+
{
291+
LogInfo("IoTHubClient accepted the message for delivery\r\n");
292+
}
293+
294+
IoTHubMessage_Destroy(messageHandle);
295+
}
296+
free(destination);
297+
}
298+
}
299+
}
300+
301+
IoTHubClient_LL_DoWork(iotHubClientHandle);
302+
ThreadAPI_Sleep(100);
303+
}
304+
}
305+
306+
DESTROY_MODEL_INSTANCE(myWeather);
307+
}
308+
IoTHubClient_LL_Destroy(iotHubClientHandle);
309+
}
310+
serializer_deinit();
311+
}
312+
}
313+
314+

0 commit comments

Comments
 (0)