In this scenario, we will use Grove – Button
to develop a project SOS event. In this case, we are check the value of the button sensor, and then call for help when it is pressed.
In this case, we using the Azure Function, when the button is pressed it will trigger the Microsoft Azure Function, and use twilio send sos message.
- Notice:Here we only provide a way to build the application. The sensor data can already be obtained from the iot-hub, and users should build the application according to their actual situation, instead of simply using the Demo.
Connecting Grove – Button
to GrovePi+'s D4 port, and then power the Raspberry Pi with USB.
hardware list:
- Raspberry Pi 3
- GrovePi+
- Grove – Button
- 1 x Grove Cable
- Micrsoft Azure IoT Hub: Use to manage and monitor Grove module.
- Micrsoft Azure Functions: We can use Azure Fuction to send an alert email via Twilio service.
-
create function app on
All resources --> New --> Compute --> Function App
Refer to: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function -
Copy IoTHub campatible EventHub endpoint name. Move on
Your_IoTHub --> GroveKitIotHub --> Endpoints --> Built-in endpoints
, Clickmessages/events
, copy value ofEvent Hub-compatible name
. And add another Consumer groups, namedfunction
. -
Copy
code/run.csx
to Azrue portal, and modifyAccount SID
,YOUR_TWILIO_NUMBER
andYOUR_FAMILY_NUMBER
on therun.csx
. On the right side to add file namedproject.json
, copycode/project.json
. -
Now, You can run AzureGroveKit UWP app to test function.
using System;
using Newtonsoft.Json;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
// Your Account SID from twilio.com/console
const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Your Auth Token from twilio.com/console
const string authToken = "auth_token";
const string YOUR_TWILIO_NUMBER = "";
const string YOUR_FAMILY_NUMBER = "";
public static void Run(string myEventHubMessage, TraceWriter log)
{
log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage}");
ButtonEvent m = JsonConvert.DeserializeObject<ButtonEvent>(myEventHubMessage);
if (m.Click)
{
SendSOS("Please help me. From Azure button.");
}
}
public static void SendSOS(String msg)
{
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
to: new PhoneNumber(YOUR_FAMILY_NUMBER),
from: new PhoneNumber(YOUR_TWILIO_NUMBER),
body: msg);
}
internal class ButtonEvent
{
public string DeviceId { get; set; }
public bool Click { get; set; }
public string Timestamp { get; set; }
}
{
"frameworks": {
"net46": {
"dependencies": {
"Microsoft.AspNet.WebApi.Client": "5.2.3",
"Microsoft.AspNet.WebApi.Core": "5.2.3",
"Newtonsoft.Json": "10.0.2",
"Twilio": "5.5.1"
}
}
}
}
Twilio binding for Azure Functions:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-twilio