Created
May 16, 2016 18:37
-
-
Save programmersommer/66cd7945d67615634866ad7b0377fc09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <WiFi101.h> | |
unsigned long lastConnectionTime = 0; | |
const unsigned long pollingInterval = 2L * 1000L; // 5 sec polling delay, in milliseconds | |
// Generate SAS key using https://github.com/Azure/azure-iot-sdks/releases | |
char hostname[] = "alexey.azure-devices.net"; | |
char authSAS[] = "SharedAccessSignature sr=alexey.azure-devices.net%2fdevices%2fmyDevice&sig=D7OxGEm98bqAQDYk33d0DzPB92EuGMkjkzKBCsBBksc%3d&se=1493799405"; | |
String deviceName = "myDevice"; | |
char ssid[] = "xxx"; // your network SSID (name) | |
char pass[] = "xxxxxxxx"; // your network password | |
String uri = "/devices/myDevice/messages/devicebound?api-version=2016-02-03"; | |
int status = WL_IDLE_STATUS; | |
WiFiSSLClient client; | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Demo for Microsoft Azure"); | |
// 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); | |
status = WiFi.begin(ssid, pass); | |
// wait 10 seconds for connection: | |
delay(10000); | |
} | |
Serial.println("Connected to wifi"); | |
} | |
void loop() { | |
String response = ""; | |
char c; | |
///read response if WiFi Client is available | |
while (client.available()) { | |
c = client.read(); | |
response.concat(c); | |
} | |
if (!response.equals("")) | |
{ | |
//if there are no messages in the IoT Hub Device queue, Azure will return 204 status code. | |
if (response.startsWith("HTTP/1.1 204")) | |
{ | |
Serial.println("Good"); | |
Serial.println(response); | |
} | |
else | |
{ | |
Serial.println(response); | |
} | |
} | |
// polling..if pollingInterval has passed | |
if (millis() - lastConnectionTime > pollingInterval) { | |
azureHttpRequest(); | |
} | |
} | |
void azureHttpRequest() { | |
// close any connection before send a new request. | |
// This will free the socket on the WiFi shield | |
client.stop(); | |
// if there's a successful connection: | |
if (client.connect(hostname, 443)) { | |
//make the GET request to the Azure IOT device feed uri | |
client.print("GET "); //Do a GET | |
client.print(uri); // On the feedURI | |
client.println(" HTTP/1.1"); | |
client.print("Host: "); | |
client.println(hostname); //with hostname header | |
client.print("Authorization: "); | |
client.println(authSAS); //Authorization SAS token obtained from Azure IoT device explorer | |
client.println("Connection: close"); | |
client.println(); | |
// note the time that the connection was made: | |
lastConnectionTime = millis(); | |
} | |
else { | |
// if you couldn't make a connection: | |
Serial.println("connection failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment