From 7eb95e986da3a9424f4f233d2ceea0e8fac3e871 Mon Sep 17 00:00:00 2001 From: Arthur Ma Date: Fri, 20 Oct 2017 18:54:37 +0800 Subject: [PATCH 1/2] Use DevKitMQTTClient for DoorMonitor. (#634) --- .../examples/DoorMonitor/.vscode/arduino.json | 5 + .../examples/DoorMonitor/DoorMonitor.ino | 293 ++++++++++-------- .../DoorMonitor/iothub_client_sample_mqtt.cpp | 93 ------ .../DoorMonitor/iothub_client_sample_mqtt.h | 12 - 4 files changed, 169 insertions(+), 234 deletions(-) create mode 100644 AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/.vscode/arduino.json delete mode 100644 AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.cpp delete mode 100644 AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.h diff --git a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/.vscode/arduino.json b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/.vscode/arduino.json new file mode 100644 index 000000000..912513e89 --- /dev/null +++ b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/.vscode/arduino.json @@ -0,0 +1,5 @@ +{ + "board": "AZ3166:stm32f4:MXCHIP_AZ3166", + "configuration": "upload_method=OpenOCDMethod", + "sketch": "DoorMonitor.ino" +} \ No newline at end of file diff --git a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/DoorMonitor.ino b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/DoorMonitor.ino index b50f67724..5e2cdfddd 100644 --- a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/DoorMonitor.ino +++ b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/DoorMonitor.ino @@ -1,60 +1,180 @@ // 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/door-monitor?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode -#include "LIS2MDLSensor.h" #include "AZ3166WiFi.h" -#include "iothub_client_sample_mqtt.h" -#include -#include "telemetry.h" - -DevI2C *i2c; -LIS2MDLSensor *lis2mdl; -int axes[3]; - -int base_x; -int base_y; -int base_z; -int count = 0; -int expectedCount = 5; -bool initialized = false; -bool hasWifi = false; -bool preOpened = false; -bool telemetrySent = false; +#include "AzureIotHub.h" +#include "DevKitMQTTClient.h" +#include "LIS2MDLSensor.h" +#include "OledDisplay.h" -void setup() -{ - Serial.begin(115200); - initWifi(); +#define APP_VERSION "ver=1.0" +#define LOOP_DELAY 1000 +#define EXPECTED_COUNT 5 - if (!hasWifi) - { - return; - } +// The magnetometer sensor +static DevI2C *i2c; +static LIS2MDLSensor *lis2mdl; - // Microsoft collects data to operate effectively and provide you the best experiences with our products. - // We collect data about the features you use, how often you use them, and how you use them. - send_telemetry_data("", "DoorMonitorSetup", ""); +// Data from magnetometer sensor +static int axes[3]; +static int base_x; +static int base_y; +static int base_z; - i2c = new DevI2C(D14, D15); - lis2mdl = new LIS2MDLSensor(*i2c); - // init - lis2mdl->init(NULL); +// Indicate whether the magnetometer sensor has been initialized +static bool initialized = false; - iothubInit(); - Screen.print(0, "Initializing..."); +// The open / close status of the door +static bool preOpened = false; + +// Indicate whether DoorMonitorSucceed event has been logged +static bool telemetrySent = false; + +// Indicate whether WiFi is ready +static bool hasWifi = false; + +// Indicate whether IoT Hub is ready +static bool hasIoTHub = false; + +////////////////////////////////////////////////////////////////////////////////////////////////////////// +// 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 "); + } } -void loop() +static void InitMagnetometer() { - if (!hasWifi) + Screen.print(2, "Initializing..."); + i2c = new DevI2C(D14, D15); + lis2mdl = new LIS2MDLSensor(*i2c); + lis2mdl->init(NULL); + + lis2mdl->getMAxes(axes); + base_x = axes[0]; + base_y = axes[1]; + base_z = axes[2]; + + int count = 0; + int delta = 10; + char buffer[20]; + while (true) + { + delay(LOOP_DELAY); + lis2mdl->getMAxes(axes); + + // Waiting for the data from sensor to become stable + if (abs(base_x - axes[0]) < delta && abs(base_y - axes[1]) < delta && abs(base_z - axes[2]) < delta) + { + count++; + if (count >= EXPECTED_COUNT) + { + // Done + Screen.print(0, "Monitoring..."); + break; + } + } + else { - delay(1000); - return; + count = 0; + base_x = axes[0]; + base_y = axes[1]; + base_z = axes[2]; } + sprintf(buffer, " %d", EXPECTED_COUNT - count); + Screen.print(1, buffer); + } +} + +void CheckMagnetometerStatus() +{ + char *message; + int delta = 30; + bool curOpened = false; + if (abs(base_x - axes[0]) < delta && abs(base_y - axes[1]) < delta && abs(base_z - axes[2]) < delta) + { + Screen.print(0, "Door closed"); + message = "Door closed"; + curOpened = false; + } + else + { + Screen.print(0, "Door opened"); + message = "Door opened"; + curOpened = true; + } + // send message when status change + if (curOpened != preOpened) + { + if (DevKitMQTTClient_SendEvent(message)) + { + if (!telemetrySent) + { + telemetrySent = true; + LogTrace("DoorMonitorSucceed", APP_VERSION); + } + } + preOpened = curOpened; + } +} - // getMAxes +////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Arduino sketch +void setup() +{ + Screen.init(); + Screen.print(0, "DoorMonitor"); + + 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("DoorMonitor", APP_VERSION); + + // IoT hub + Screen.print(3, " > IoT Hub"); + if (!DevKitMQTTClient_Init()) + { + Screen.clean(); + Screen.print(0, "DoorMonitor"); + Screen.print(2, "No IoT Hub"); + hasIoTHub = false; + return; + } + hasIoTHub = true; + + Screen.print(3, " > Magnetometer"); + InitMagnetometer(); +} + +void loop() +{ + if (hasWifi && hasIoTHub) + { + // Get data from magnetometer sensor lis2mdl->getMAxes(axes); - Serial.printf("Axes: x - %d, y - %d, z - %d\n", axes[0], axes[1], axes[2]); + Serial.printf("Axes: x - %d, y - %d, z - %d\r\n", axes[0], axes[1], axes[2]); char buffer[50]; @@ -67,92 +187,7 @@ void loop() sprintf(buffer, "z: %d", axes[2]); Screen.print(3, buffer); - checkMagnetometerStatus(); - - delay(1000); -} - -void initWifi() -{ - Screen.print("IoT DevKit\r\n \r\nConnecting...\r\n"); - - if (WiFi.begin() == WL_CONNECTED) - { - IPAddress ip = WiFi.localIP(); - Screen.print(1, ip.get_address()); - hasWifi = true; - Screen.print(2, "Running...\r\n"); - } - else - { - Screen.print(1, "No Wi-Fi\r\n"); - } + CheckMagnetometerStatus(); + } + delay(LOOP_DELAY); } - -void checkMagnetometerStatus() -{ - if (initialized) - { - char *message; - int delta = 30; - bool curOpened = false; - if (abs(base_x - axes[0]) < delta && abs(base_y - axes[1]) < delta && abs(base_z - axes[2]) < delta) - { - Screen.print(0, "Door closed"); - message = "Door closed"; - curOpened = false; - } - else - { - Screen.print(0, "Door opened"); - message = "Door opened"; - curOpened = true; - } - // send message when status change - if (curOpened != preOpened) - { - iothubSendMessage((const unsigned char *)message); - iothubLoop(); - - if (!telemetrySent) - { - telemetrySent = true; - send_telemetry_data("", "DoorMonitorSucceed", ""); - } - preOpened = curOpened; - } - } - else - { - if (base_x == 0 && base_y == 0 && base_z == 0) - { - base_x = axes[0]; - base_y = axes[1]; - base_z = axes[2]; - } - else - { - int delta = 10; - if (abs(base_x - axes[0]) < delta && abs(base_y - axes[1]) < delta && abs(base_z - axes[2]) < delta) - { - char buffer[50]; - sprintf(buffer, "Initialize %d", expectedCount - count); - Screen.print(0, buffer); - - count++; - if (count >= expectedCount) - { - initialized = true; - } - } - else - { - Screen.print(0, "Initializing..."); - count = 0; - base_x = axes[0]; - base_y = axes[1]; - base_z = axes[2]; - } - } - } -} \ No newline at end of file diff --git a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.cpp b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.cpp deleted file mode 100644 index f24d9a97d..000000000 --- a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.cpp +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -#include "AzureIotHub.h" -#include "Arduino.h" -#include "EEPROMInterface.h" -#include "iothub_client_sample_mqtt.h" - -static IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle; - -void iothubInit() -{ - srand((unsigned int)time(NULL)); - - // Load connection from EEPROM - EEPROMInterface eeprom; - uint8_t connString[AZ_IOT_HUB_MAX_LEN + 1] = { '\0' }; - int ret = eeprom.read(connString, AZ_IOT_HUB_MAX_LEN, 0x00, AZ_IOT_HUB_ZONE_IDX); - if (ret < 0) - { - LogInfo("ERROR: Unable to get the azure iot connection string from EEPROM. Please set the value in configuration mode."); - return; - } - else if (ret == 0) - { - LogInfo("INFO: The connection string is empty. Please set the value in configuration mode."); - return; - } - - if (platform_init() != 0) - { - LogInfo("Failed to initialize the platform."); - return; - } - - if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString((char*)connString, MQTT_Protocol)) == NULL) - { - LogInfo("The iotHubClientHandle is NULL!"); - return; - } - - if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK) - { - LogInfo("Failed to set option \"TrustedCerts\""); - return; - } -} - -static void sendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void *userContextCallback) -{ - if (IOTHUB_CLIENT_CONFIRMATION_OK == result) - { - LogInfo("Message sent to Azure IoT Hub"); - } - else - { - LogInfo("Failed to send message to Azure IoT Hub"); - } -} - -void iothubSendMessage(const unsigned char *text) -{ - IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(text, strlen((const char *)text)); - if (messageHandle == NULL) - { - LogInfo("unable to create a new IoTHubMessage"); - return; - } - LogInfo("Sending message: %s", text); - if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messageHandle, sendConfirmationCallback, NULL) != IOTHUB_CLIENT_OK) - { - LogInfo("Failed to hand over the message to IoTHubClient"); - return; - } - LogInfo("IoTHubClient accepted the message for delivery"); - IoTHubMessage_Destroy(messageHandle); -} - -void iothubLoop(void) -{ - IOTHUB_CLIENT_STATUS sendStatusContext; - unsigned char doWorkLoopCounter = 0; - do - { - IoTHubClient_LL_DoWork(iotHubClientHandle); - ThreadAPI_Sleep(10); - } while (++doWorkLoopCounter < 10 && (IoTHubClient_LL_GetSendStatus(iotHubClientHandle, &sendStatusContext) == IOTHUB_CLIENT_OK) && (sendStatusContext == IOTHUB_CLIENT_SEND_STATUS_BUSY)); -} - -void iothubClose(void) -{ - IoTHubClient_LL_Destroy(iotHubClientHandle); -} \ No newline at end of file diff --git a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.h b/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.h deleted file mode 100644 index 09b0bdbed..000000000 --- a/AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/iothub_client_sample_mqtt.h +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. - -#ifndef IOTHUB_CLIENT_SAMPLE_MQTT_H -#define IOTHUB_CLIENT_SAMPLE_MQTT_H - -void iothubInit(void); -void iothubSendMessage(const unsigned char *); -void iothubLoop(void); -void iothubClose(void); - -#endif /* IOTHUB_CLIENT_SAMPLE_MQTT_H */ \ No newline at end of file From 59d36f755f204baa99361c57e698908b51bc1d8e Mon Sep 17 00:00:00 2001 From: Arthur Ma Date: Mon, 23 Oct 2017 10:04:14 +0800 Subject: [PATCH 2/2] Upgrade the ReleaseNotes. (#635) * Upgrade the ReleaseNotes. * add sth.~ --- AZ3166/src/ReleaseNotes.html | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/AZ3166/src/ReleaseNotes.html b/AZ3166/src/ReleaseNotes.html index ac8997b5f..aaf95e56e 100644 --- a/AZ3166/src/ReleaseNotes.html +++ b/AZ3166/src/ReleaseNotes.html @@ -214,8 +214,8 @@

Build your first DevKit project now!

-

Version 1.1.0

-

Notice: This release involves breaking changes, please upgrade the DevKit firmware first.
DevKit now officially has full support for ST-SAFE, the security chip that provides secure authentication and data management for IoT solutions. Since it's enabled on bootloader level, a firmware upgrade is mandatory to make the DevKit work properly.
Read the full release notes

+

Version 1.2.0

+

If you are upgrading from version 1.0.2 or even earlier, please upgrade the firmware first.
No more manual steps to prepare your DevKit development environment on macOS! The time saving one-click installation now support macOS as well. And yes, we love bash.
Read the full release notes.

@@ -237,7 +237,6 @@

Azure IoT Hub Tutorials

Documentations

Getting Started
Projects Catalog
-
Upgrade DevKit
API References
FAQ
@@ -247,7 +246,6 @@

Documentations

Blog Posts

-
MXChip IoT DevKit is available for pre-ordering
IoT Developers, Check Out These Development Tools!