Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Renhe Li committed Oct 23, 2017
2 parents 66d65b9 + 59d36f7 commit 3b17249
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 238 deletions.
6 changes: 2 additions & 4 deletions AZ3166/src/ReleaseNotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ <h2 class="caption">Build your first DevKit project now!</h2>
<tr>
<td>
<div>
<h2 class="caption">Version 1.1.0</h2>
<p><strong class="vscode-high-contrast">Notice: This release involves breaking changes, please upgrade the DevKit firmware first.</strong><br />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 <strong><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/upgrading/#upgrade-firmware">firmware upgrade</a></strong> is mandatory to make the DevKit work properly.<br /><a href="https://microsoft.github.io/azure-iot-developer-kit/versions?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">Read the full release notes</a></p>
<h2 class="caption">Version 1.2.0</h2>
<p><strong class="vscode-high-contrast">If you are upgrading from version 1.0.2 or even earlier, please upgrade the firmware first.</strong><br />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. <br /> <a href="https://microsoft.github.io/azure-iot-developer-kit/versions?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">Read the full release notes.</a></p>
</div>
</td>
</tr>
Expand All @@ -237,7 +237,6 @@ <h2 class="caption">Azure IoT Hub Tutorials</h2>
<h3 class="caption">Documentations</h3>
<div><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/get-started?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">Getting Started</a></div>
<div><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/projects?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">Projects Catalog</a></div>
<div><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/upgrading?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">Upgrade DevKit</a></div>
<div><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/apis/wifi?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">API References</a></div>
<div><a href="https://microsoft.github.io/azure-iot-developer-kit/docs/faq?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">FAQ</a></div>
</div>
Expand All @@ -247,7 +246,6 @@ <h3 class="caption">Documentations</h3>
<td>
<div>
<h3 class="caption">Blog Posts</h3>
<div><a href="https://blogs.msdn.microsoft.com/iotdev/2017/08/20/mxchip-iot-devkit-is-available-for-pre-ordering?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">MXChip IoT DevKit is available for pre-ordering</a></div>
<div><a href="https://blogs.msdn.microsoft.com/iotdev/2017/08/10/iot-developers-check-out-these-development-tools?utm_source=ArduinoExtension&utm_medium=ReleaseNote&utm_campaign=VSCode">IoT Developers, Check Out These Development Tools!</a></div>
</div>
</td>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"board": "AZ3166:stm32f4:MXCHIP_AZ3166",
"configuration": "upload_method=OpenOCDMethod",
"sketch": "DoorMonitor.ino"
}
293 changes: 164 additions & 129 deletions AZ3166/src/libraries/AzureIoT/examples/DoorMonitor/DoorMonitor.ino
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#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];

Expand All @@ -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];
}
}
}
}
Loading

0 comments on commit 3b17249

Please sign in to comment.