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

Commit

Permalink
add samples to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
jbobotek committed Dec 11, 2019
1 parent 01bf4f8 commit 4c1bc55
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/iothub_ll_telemetry_sample/iot_configs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef IOT_CONFIGS_H
#define IOT_CONFIGS_H

/**
* WiFi setup
*/
#define IOT_CONFIG_WIFI_SSID "your-wifi-name"
#define IOT_CONFIG_WIFI_PASSWORD "your-wifi-pwd"

/**
* Find under Microsoft Azure IoT Suite -> DEVICES -> <your device> -> Device Details and Authentication Keys
* String containing Hostname, Device Id & Device Key in the format:
* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
*/
#define DEVICE_CONNECTION_STRING "your-iothub-DEVICE-connection-string"

// The protocol you wish to use should be uncommented
//
#define SAMPLE_MQTT
//#define SAMPLE_MQTT_OVER_WEBSOCKETS
//#define SAMPLE_HTTP

#endif /* IOT_CONFIGS_H */
11 changes: 11 additions & 0 deletions examples/iothub_ll_telemetry_sample/sample_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#ifndef SAMPLE_INIT_H
#define SAMPLE_INIT_H
#if defined(ARDUINO_ARCH_ESP8266)
#define sample_init esp8266_sample_init
void esp8266_sample_init(const char* ssid, const char* password);
#endif

#endif // SAMPLE_INIT_H
224 changes: 224 additions & 0 deletions examples/iothub_ll_telemetry_sample/telemetry_sample.c.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <AzureIoTUtility.h>

// CAVEAT: This sample is to demonstrate azure IoT client concepts only and is not a guide design principles or style
// Checking of return codes and error values shall be omitted for brevity. Please practice sound engineering practices
// when writing production code.

#include <stdio.h>
#include <stdlib.h>

#include "iothub.h"
#include "iothub_client.h"
#include "iothub_device_client_ll.h"
#include "iothub_client_options.h"
#include "iothub_message.h"
#include "azure_c_shared_utility/crt_abstractions.h"
#include "azure_c_shared_utility/shared_util_options.h"
#include "azure_c_shared_utility/tlsio.h"
#include "azure_c_shared_utility/xlogging.h"
#include "certs/certs.h"

// You must set the device id, device key, IoT Hub name and IotHub suffix in
// iot_configs.h
#include "iot_configs.h"
#include "sample_init.h"

static char ssid[] = IOT_CONFIG_WIFI_SSID;
static char pass[] = IOT_CONFIG_WIFI_PASSWORD;

#ifdef SAMPLE_MQTT
#include "AzureIoTProtocol_MQTT.h"
#include "iothubtransportmqtt.h"
#endif // SAMPLE_MQTT
#ifdef SAMPLE_HTTP
#include "AzureIoTProtocol_HTTP.h"
#include "iothubtransporthttp.h"
#endif // SAMPLE_HTTP

/* This sample uses the _LL APIs of iothub_client for example purposes.
Simply changing the using the convenience layer (functions not having _LL)
and removing calls to _DoWork will yield the same results. */

static const char* connectionString = DEVICE_CONNECTION_STRING;
#define MESSAGE_COUNT 5
static bool g_continueRunning = true;
static size_t g_message_count_send_confirmations = 0;

/* Define several constants/global variables */
IOTHUB_MESSAGE_HANDLE message_handle;
size_t messages_sent = 0;
const char* telemetry_msg = "test_message";

// Select the Protocol to use with the connection
#ifdef SAMPLE_MQTT
IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = MQTT_Protocol;
#endif // SAMPLE_MQTT
#ifdef SAMPLE_HTTP
IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol = HTTP_Protocol;
#endif // SAMPLE_HTTP

IOTHUB_DEVICE_CLIENT_LL_HANDLE device_ll_handle;

static int callbackCounter;
int receiveContext = 0;

static IOTHUBMESSAGE_DISPOSITION_RESULT receive_message_callback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
{
int* counter = (int*)userContextCallback;
const char* buffer;
size_t size;
MAP_HANDLE mapProperties;
const char* messageId;

// Message properties
if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
{
messageId = "<null>";
}

// Message content
if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
{
LogInfo("unable to retrieve the message data\r\n");
}
else
{
LogInfo("Received Message [%d]\r\n Message ID: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, (int)size, buffer, (int)size);
// If we receive the work 'quit' then we stop running
if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
{
g_continueRunning = false;
}
}

/* Some device specific action code goes here... */
(*counter)++;
return IOTHUBMESSAGE_ACCEPTED;
}


static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
{
(void)userContextCallback;
// When a message is sent this callback will get envoked
g_message_count_send_confirmations++;
LogInfo("Confirm Callback");
// LogInfo("Confirmation callback received for message %lu with result %s\r\n", (unsigned long)g_message_count_send_confirmations, MU_ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
}

static void connection_status_callback(IOTHUB_CLIENT_CONNECTION_STATUS result, IOTHUB_CLIENT_CONNECTION_STATUS_REASON reason, void* user_context)
{
(void)reason;
(void)user_context;
// This sample DOES NOT take into consideration network outages.
if (result == IOTHUB_CLIENT_CONNECTION_AUTHENTICATED)
{
LogInfo("The device client is connected to iothub\r\n");
}
else
{
LogInfo("The device client has been disconnected\r\n");
}
}

void setup() {
int result = 0;

sample_init(ssid, pass);

device_ll_handle = IoTHubDeviceClient_LL_CreateFromConnectionString(connectionString, protocol);
// Used to initialize IoTHub SDK subsystem
(void)IoTHub_Init();

LogInfo("Creating IoTHub Device handle\r\n");
// Create the iothub handle here
if (device_ll_handle == NULL)
{
LogInfo("Failure createing Iothub device. Hint: Check you connection string.\r\n");
}
else
{
// Set any option that are neccessary.
// For available options please see the iothub_sdk_options.md documentation
// turn off diagnostic sampling
int diag_off=0;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_DIAGNOSTIC_SAMPLING_PERCENTAGE, &diag_off);

#ifndef SAMPLE_HTTP
// Can not set this options in HTTP
bool traceOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_LOG_TRACE, &traceOn);
#endif // SAMPLE_HTTP

// Setting the Trusted Certificate. This is only necessary on system with without
// built in certificate stores.
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_TRUSTED_CERT, certificates);

#if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS
//Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
//you are URL Encoding inputs yourself.
//ONLY valid for use with MQTT
bool urlEncodeOn = true;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
/* Setting Message call back, so we can receive Commands. */
if (IoTHubClient_LL_SetMessageCallback(device_ll_handle, receive_message_callback, &receiveContext) != IOTHUB_CLIENT_OK)
{
LogInfo("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
}
#endif // SAMPLE_MQTT

// Setting connection status callback to get indication of connection to iothub
(void)IoTHubDeviceClient_LL_SetConnectionStatusCallback(device_ll_handle, connection_status_callback, NULL);

do
{
if (messages_sent < MESSAGE_COUNT)
{
// Construct the iothub message from a string or a byte array
message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
//message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));

// Set Message property
/*(void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
(void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
(void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
(void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");*/

// Add custom properties to message
// (void)IoTHubMessage_SetProperty(message_handle, "property_key", "property_value");

LogInfo("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1));
result = IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);
// The message is copied to the sdk so the we can destroy it
IoTHubMessage_Destroy(message_handle);

messages_sent++;
}
else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
{
// After all messages are all received stop running
g_continueRunning = false;
}

IoTHubDeviceClient_LL_DoWork(device_ll_handle);
ThreadAPI_Sleep(3);

} while (g_continueRunning);

// Clean up the iothub sdk handle
IoTHubDeviceClient_LL_Destroy(device_ll_handle);
}
// Free all the sdk subsystem
IoTHub_Deinit();

LogInfo("done with sending");
return;
}

void loop(void)
{

}

0 comments on commit 4c1bc55

Please sign in to comment.