Skip to content

Commit

Permalink
1.0.39 release
Browse files Browse the repository at this point in the history
  • Loading branch information
az-iot-builder-01 committed Oct 5, 2017
1 parent 6a95dd2 commit d2adf54
Show file tree
Hide file tree
Showing 19 changed files with 571 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You should have the following ready before beginning with any board:
* 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 [Adafruit WINC1500 wifi library](https://learn.adafruit.com/adafruit-feather-m0-wifi-atwinc1500/using-the-wifi-module)
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.
Expand Down
259 changes: 259 additions & 0 deletions examples/simplesample_http/simplesample_http.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <stdlib.h>

#include <stdio.h>
#include <stdint.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
#include "certs.h"
#endif // MBED_BUILD_TIMESTAMP

/*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]";

// Define the Model
BEGIN_NAMESPACE(WeatherStation);

DECLARE_MODEL(ContosoAnemometer,
WITH_DATA(ascii_char_ptr, DeviceId),
WITH_DATA(int, WindSpeed),
WITH_DATA(float, Temperature),
WITH_DATA(float, Humidity),
WITH_ACTION(TurnFanOn),
WITH_ACTION(TurnFanOff),
WITH_ACTION(SetAirResistance, int, Position)
);

END_NAMESPACE(WeatherStation);

static char propText[1024];

EXECUTE_COMMAND_RESULT TurnFanOn(ContosoAnemometer* device)
{
(void)device;
(void)printf("Turning fan on.\r\n");
return EXECUTE_COMMAND_SUCCESS;
}

EXECUTE_COMMAND_RESULT TurnFanOff(ContosoAnemometer* device)
{
(void)device;
(void)printf("Turning fan off.\r\n");
return EXECUTE_COMMAND_SUCCESS;
}

EXECUTE_COMMAND_RESULT SetAirResistance(ContosoAnemometer* device, int Position)
{
(void)device;
(void)printf("Setting Air Resistance Position to %d.\r\n", Position);
return EXECUTE_COMMAND_SUCCESS;
}

void sendCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
unsigned int messageTrackingId = (unsigned int)(uintptr_t)userContextCallback;

(void)printf("Message Id: %u Received.\r\n", messageTrackingId);

(void)printf("Result Call Back Called! Result is: %s \r\n", ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}

static void sendMessage(IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
{
static unsigned int messageTrackingId;
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
}
else
{
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)(uintptr_t)messageTrackingId) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
}
IoTHubMessage_Destroy(messageHandle);
}
free((void*)buffer);
messageTrackingId++;
}

/*this function "links" IoTHub to the serialization library*/
static IOTHUBMESSAGE_DISPOSITION_RESULT IoTHubMessage(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
IOTHUBMESSAGE_DISPOSITION_RESULT result;
const unsigned char* buffer;
size_t size;
if (IoTHubMessage_GetByteArray(message, &buffer, &size) != IOTHUB_MESSAGE_OK)
{
printf("unable to IoTHubMessage_GetByteArray\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
/*buffer is not zero terminated*/
char* temp = malloc(size + 1);
if (temp == NULL)
{
printf("failed to malloc\r\n");
result = IOTHUBMESSAGE_ABANDONED;
}
else
{
EXECUTE_COMMAND_RESULT executeCommandResult;

(void)memcpy(temp, buffer, size);
temp[size] = '\0';
executeCommandResult = EXECUTE_COMMAND(userContextCallback, temp);
result =
(executeCommandResult == EXECUTE_COMMAND_ERROR) ? IOTHUBMESSAGE_ABANDONED :
(executeCommandResult == EXECUTE_COMMAND_SUCCESS) ? IOTHUBMESSAGE_ACCEPTED :
IOTHUBMESSAGE_REJECTED;
free(temp);
}
}
return result;
}

void simplesample_http_run(void)
{
if (platform_init() != 0)
{
printf("Failed to initialize the platform.\r\n");
}
else
{
if (serializer_init(NULL) != SERIALIZER_OK)
{
(void)printf("Failed on serializer_init\r\n");
}
else
{
IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol);
int avgWindSpeed = 10;
float minTemperature = 20.0;
float minHumidity = 60.0;

srand((unsigned int)time(NULL));

if (iotHubClientHandle == NULL)
{
(void)printf("Failed on IoTHubClient_LL_Create\r\n");
}
else
{
// Because it can poll "after 9 seconds" polls will happen
// effectively at ~10 seconds.
// Note that for scalabilty, the default value of minimumPollingTime
// is 25 minutes. For more information, see:
// https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
unsigned int minimumPollingTime = 9;
ContosoAnemometer* myWeather;

if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
{
printf("failure to set option \"MinimumPollingTime\"\r\n");
}

#ifdef MBED_BUILD_TIMESTAMP
// For mbed add the certificate information
if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
{
(void)printf("failure to set option \"TrustedCerts\"\r\n");
}
#endif // MBED_BUILD_TIMESTAMP

myWeather = CREATE_MODEL_INSTANCE(WeatherStation, ContosoAnemometer);
if (myWeather == NULL)
{
(void)printf("Failed on CREATE_MODEL_INSTANCE\r\n");
}
else
{
if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, IoTHubMessage, myWeather) != IOTHUB_CLIENT_OK)
{
printf("unable to IoTHubClient_SetMessageCallback\r\n");
}
else
{
myWeather->DeviceId = "myFirstDevice";
myWeather->WindSpeed = avgWindSpeed + (rand() % 4 + 2);
myWeather->Temperature = minTemperature + (rand() % 10);
myWeather->Humidity = minHumidity + (rand() % 20);
{
unsigned char* destination;
size_t destinationSize;
if (SERIALIZE(&destination, &destinationSize, myWeather->DeviceId, myWeather->WindSpeed, myWeather->Temperature, myWeather->Humidity) != CODEFIRST_OK)
{
(void)printf("Failed to serialize\r\n");
}
else
{
IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize);
if (messageHandle == NULL)
{
printf("unable to create a new IoTHubMessage\r\n");
}
else
{
MAP_HANDLE propMap = IoTHubMessage_Properties(messageHandle);
(void)sprintf_s(propText, sizeof(propText), myWeather->Temperature > 28 ? "true" : "false");
if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
{
printf("ERROR: Map_AddOrUpdate Failed!\r\n");
}

if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendCallback, (void*)1) != IOTHUB_CLIENT_OK)
{
printf("failed to hand over the message to IoTHubClient");
}
else
{
printf("IoTHubClient accepted the message for delivery\r\n");
}

IoTHubMessage_Destroy(messageHandle);
}
free(destination);
}
}

/* wait for commands */
while (1)
{
IoTHubClient_LL_DoWork(iotHubClientHandle);
ThreadAPI_Sleep(100);
}
}

DESTROY_MODEL_INSTANCE(myWeather);
}
IoTHubClient_LL_Destroy(iotHubClientHandle);
}
serializer_deinit();
}
platform_deinit();
}
}
17 changes: 17 additions & 0 deletions examples/simplesample_http/simplesample_http.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef SIMPLESAMPLEHTTP_H
#define SIMPLESAMPLEHTTP_H

#ifdef __cplusplus
extern "C" {
#endif

void simplesample_http_run(void);

#ifdef __cplusplus
}
#endif

#endif /* SIMPLESAMPLEHTTP_H */
18 changes: 6 additions & 12 deletions examples/simplesample_http/simplesample_http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#elif ARDUINO_SAMD_FEATHER_M0
// for Adafruit WINC1500
#include <Adafruit_WINC1500.h>
#include <Adafruit_WINC1500SSLClient.h>
#include <Adafruit_WINC1500Udp.h>
#include <NTPClient.h>
#else
#include <WiFi101.h>
#include <WiFiSSLClient.h>
Expand All @@ -35,8 +29,6 @@
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
#endif

#include <AzureIoTHub.h>
Expand Down Expand Up @@ -68,9 +60,14 @@ void initSerial() {

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 :
Expand All @@ -97,11 +94,8 @@ void initWifi() {

void initTime() {
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_FEATHER_M0)
#ifdef ARDUINO_SAMD_FEATHER_M0
Adafruit_WINC1500UDP ntpUdp; // for Adafruit WINC1500
#else
WiFiUDP ntpUdp;
#endif

NTPClient ntpClient(ntpUdp);

ntpClient.begin();
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AzureIoTUtility
version=1.0.36
version=1.0.39
author=Microsoft
maintainer=Microsoft <[email protected]>
sentence=Azure C shared utility library for Arduino. For the Arduino MKR1000 or Zero and WiFi Shield 101, Adafruit Huzzah and Feather M0, or SparkFun Thing.
Expand Down
2 changes: 1 addition & 1 deletion src/AzureIoTUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
#include "azure_c_shared_utility/lock.h"
#include "azure_c_shared_utility/threadapi.h"

#define AzureIoTUtilityVersion "1.0.36"
#define AzureIoTUtilityVersion "1.0.39"

#endif //AZUREIOTUTILITY_H
5 changes: 0 additions & 5 deletions src/adapters/sslClient_arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ static WiFiClientSecure sslClient; // for ESP8266
#include "WiFi.h"
#include "WiFiClientSecure.h"
static WiFiClientSecure sslClient; // for ESP32
#elif ARDUINO_SAMD_FEATHER_M0
#include "Adafruit_WINC1500.h"
#include "Adafruit_WINC1500Client.h"
#include "Adafruit_WINC1500SSLClient.h"
static Adafruit_WINC1500SSLClient sslClient; // for Adafruit WINC1500
#else
#include "WiFi101.h"
#include "WiFiSSLClient.h"
Expand Down
Loading

0 comments on commit d2adf54

Please sign in to comment.