Вы находитесь на странице: 1из 5

An IOT Based Security System

1. PIR Sensor senses if any object moves over it, within 120 degrees.
2. Sends message(deviceID,TimeStamp) to Azure IoT hub when PIR Senses an Object.
3. Console Application(ServiceSide) reads event hub data.
4. After detecting consecutive 10 motions, Console App will send message as “Alert”.
5. Buzzer makes sound after receiving message as FacesDetected from hub.

Program to send message from Device to Cloud(D2C)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Gpio;
using Microsoft.Azure.Devices.Client;
using System.Text;

// The Blank Page item template is documented at


https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace homesecurity_clientapp
{
/// <summary>
/// An empty page that can be used on its own or navigated to
within a Frame.
/// </summary>
public sealed partial class MainPage : Page

{
const string deviceConnectionString = "HostName =
security.azure - devices.net;
SharedAccessKeyName=iothubowner;SharedAccessKey=nvkm6kUI287iGJnG/tgrtT
cUKOPdj/K5YpTU=";
private const int BUZZER_PIN = 12;
private const int PIR_PIN = 69;
private GpioPin BuzzerPin;
private GpioPin PirPin;
private GpioPinValue BuzzerPinValue;

public MainPage()
{
this.InitializeComponent();
initgpio();
Recieve();

public async Task Recieve()


{
string value = await
AzureIoTHub.ReceiveCloudToDeviceMessageAsync();
Status.Text = value;
if (value.Equals("Alert"))
{
BuzzerPinValue = GpioPinValue.High;
BuzzerPin.Write(BuzzerPinValue);
await Task.Delay(10000);
BuzzerPinValue = GpioPinValue.Low;
BuzzerPin.Write(BuzzerPinValue);

}
await Recieve();
}

public void initgpio()


{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
return;
}
PirPin = gpio.OpenPin(PIR_PIN);
BuzzerPin = gpio.OpenPin(BUZZER_PIN);

BuzzerPin.Write(GpioPinValue.Low);
BuzzerPin.SetDriveMode(GpioPinDriveMode.Output);

PirPin.SetDriveMode(GpioPinDriveMode.Input);
PirPin.DebounceTimeout = TimeSpan.FromMilliseconds(500);
PirPin.ValueChanged += buttonPin_ValueChanged;
}

private async void buttonPin_ValueChanged(GpioPin sender,


GpioPinValueChangedEventArgs e)
{
if (e.Edge == GpioPinEdge.FallingEdge)
{
string mydevice = "DragonBoard410c";
DateTime timestamp = DateTime.Now;
string message = mydevice + "," + timestamp;
await
AzureIoTHub.SendDeviceToCloudMessageAsync(message);
}
}

private async void Button_Click(object sender, RoutedEventArgs


e)
{
string mydevice = "DragonBoard410c";
DateTime timestamp = DateTime.Now;
string message = mydevice + "," + timestamp;
await AzureIoTHub.SendDeviceToCloudMessageAsync(message);

}
}
}

Program send message from Cloud to Device(C2D).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.ServiceBus.Messaging;
using Microsoft.Azure.Devices;

namespace home1
{
class Program
{
static ServiceClient serviceClient;
static DateTime startingDateTimeUtc;
static Dictionary<string, int> dictionary = new
Dictionary<string, int>();
//check this once!!
static string connectionString = "HostName=security.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=nvkm6kUI28
7iGJnG/tgrtTcUKOPdj/K5YpTdKJpqPGU=";
static void Main(string[] args)
{
serviceClient =
ServiceClient.CreateFromConnectionString(connectionString);
ConnectCloud();
}

public static void ConnectCloud()


{
string iotHubD2cEndpoint = "messages/events";
var eventHubClient =
EventHubClient.CreateFromConnectionString(connectionString,
iotHubD2cEndpoint);
startingDateTimeUtc = DateTime.Now;
var d2cPartitions =
eventHubClient.GetRuntimeInformation().PartitionIds;

foreach (string partition in d2cPartitions)


{
var receiver =
eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition,
startingDateTimeUtc);
ReceiveMessagesFromDeviceAsync(receiver);
}
Console.ReadLine();
}

public static async Task


ReceiveMessagesFromDeviceAsync(EventHubReceiver receiver)
{
while (true)
{
EventData eventData = await receiver.ReceiveAsync();
if (eventData == null)
continue;
string data =
Encoding.UTF8.GetString(eventData.GetBytes());
string[] value = data.Split(',');
DangerWindow(value[0], value[1]);
}
}

public static void DangerWindow(string deviceId, string


deviceTimeStamp)
{
int start, end;
DateTime dt = Convert.ToDateTime(deviceTimeStamp);
TimeSpan current = dt.TimeOfDay;
TimeSpan startTime = new TimeSpan(01, 00, 0);
TimeSpan endTime = new TimeSpan(20, 00, 00);
start = current.CompareTo(startTime);//current>startTime -
- start= +ve
end = current.CompareTo(endTime);//current<endTime -- end=
-ve
Console.WriteLine(start + " " + end);
if (start > 0 && end < 0)
{
if (dictionary.ContainsKey(deviceId))
{
dictionary[deviceId]++;
if (dictionary[deviceId] == 10)
{
SendCloudToDeviceMessageAsync(deviceId,
"Alert");
dictionary[deviceId] = 0;
}
Console.WriteLine("{0} , {1}", deviceId,
dictionary[deviceId]);
}
else
{
dictionary.Add(deviceId, 1);
Console.WriteLine("{0} , {1}", deviceId,
dictionary[deviceId]);
}
}
}

public async static Task SendCloudToDeviceMessageAsync(string


destination, string message)
{
var commandMessage = new
Message(Encoding.ASCII.GetBytes(message));
await serviceClient.SendAsync(destination,
commandMessage);
}
}
}

Вам также может понравиться