Matha Goram
Published © GPL3+

Let Linux Serve Azure IoT Hub Device

Interface MXChip AZ3166 with Azure under Ubuntu (18.10). The IoT DevKit has interesting features compacted onto a single board for PoC.

IntermediateProtip1 hour501
Let Linux Serve Azure IoT Hub Device

Things used in this project

Hardware components

Seeed MXChip AZ3166 IoT DevKit
×1

Software apps and online services

Visual Studio Code Extension for Arduino
Microsoft Visual Studio Code Extension for Arduino
Microsoft Visual Studio Code

Story

Read more

Code

MXChipGetStarted.cpp

C/C++
Demo code bundled by Microsoft for Azure IoT DevKit examples to run within Visual Studio Code.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. 
// To get started please visit https://microsoft.github.io/azure-iot-developer-kit/docs/projects/connect-iot-hub?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode
#include "AZ3166WiFi.h"
#include "AzureIotHub.h"
#include "DevKitMQTTClient.h"

#include "config.h"
#include "utility.h"
#include "SystemTickCounter.h"

static bool hasWifi = false;
int messageCount = 1;
static bool messageSending = true;
static uint64_t send_interval_ms;

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Utilities
static void InitWifi()
{
Screen.print(2, "Connecting...");
if (WiFi.begin() == WL_CONNECTED)
{
IPAddress ip = WiFi.localIP();
Screen.print(1, ip.get_address());
hasWifi = true;
Screen.print(2, "Running... \r\n");
}
else
{
hasWifi = false;
Screen.print(1, "No Wi-Fi\r\n ");
}
}

static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result)
{
if (result == IOTHUB_CLIENT_CONFIRMATION_OK)
{
blinkSendConfirmation();
}
}

static void MessageCallback(const char* payLoad, int size)
{
blinkLED();
Screen.print(1, payLoad, true);
}

static void DeviceTwinCallback(DEVICE_TWIN_UPDATE_STATE updateState, const unsigned char *payLoad, int size)
{
char *temp = (char *)malloc(size + 1);
if (temp == NULL)
{
return;
}
memcpy(temp, payLoad, size);
temp[size] = '\0';
parseTwinMessage(updateState, temp);
free(temp);
}

static int DeviceMethodCallback(const char *methodName, const unsigned char *payload, int size, unsigned char **response, int *response_size)
{
LogInfo("Try to invoke method %s", methodName);
const char *responseMessage = "\"Successfully invoke device method\"";
int result = 200;

if (strcmp(methodName, "start") == 0)
{
LogInfo("Start sending temperature and humidity data");
messageSending = true;
}
else if (strcmp(methodName, "stop") == 0)
{
LogInfo("Stop sending temperature and humidity data");
messageSending = false;
}
else
{
LogInfo("No method %s found", methodName);
responseMessage = "\"No method found\"";
result = 404;
}

*response_size = strlen(responseMessage) + 1;
*response = (unsigned char *)strdup(responseMessage);

return result;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Arduino sketch
void setup()
{
Screen.init();
Screen.print(0, "IoT DevKit");
Screen.print(2, "Initializing...");
Screen.print(3, " > Serial");
Serial.begin(115200);

// Initialize the WiFi module
Screen.print(3, " > WiFi");
hasWifi = false;
InitWifi();
if (!hasWifi)
{
return;
}

LogTrace("HappyPathSetup", NULL);

Screen.print(3, " > Sensors");
SensorInit();

Screen.print(3, " > IoT Hub");
DevKitMQTTClient_SetOption(OPTION_MINI_SOLUTION_NAME, "DevKit-GetStarted");
DevKitMQTTClient_Init(true);

DevKitMQTTClient_SetSendConfirmationCallback(SendConfirmationCallback);
DevKitMQTTClient_SetMessageCallback(MessageCallback);
DevKitMQTTClient_SetDeviceTwinCallback(DeviceTwinCallback);
DevKitMQTTClient_SetDeviceMethodCallback(DeviceMethodCallback);

send_interval_ms = SystemTickCounterRead();
}

void loop()
{
if (hasWifi)
{
if (messageSending && 
(int)(SystemTickCounterRead() - send_interval_ms) >= getInterval())
{
// Send teperature data
char messagePayload[MESSAGE_MAX_LEN];

bool temperatureAlert = readMessage(messageCount++, messagePayload);
EVENT_INSTANCE* message = DevKitMQTTClient_Event_Generate(messagePayload, MESSAGE);
DevKitMQTTClient_Event_AddProp(message, "temperatureAlert", temperatureAlert ? "true" : "false");
DevKitMQTTClient_SendEventInstance(message);
send_interval_ms = SystemTickCounterRead();
}
else
{
DevKitMQTTClient_Check();
}
}
delay(1000);
}

Credits

Matha Goram

Matha Goram

14 projects • 2 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.

Comments

Add projectSign up / Login