Bary Nusz
Published © GPL3+

PIR Sensor with a Photon, Particle Cloud, and a UWA

Simple project that shows how to integrate the PIR Sensor that comes with the Particle Maker Kit with a Photon, Particle Cloud, and a UWA.

BeginnerFull instructions provided7,078
PIR Sensor with a Photon, Particle Cloud, and a UWA

Things used in this project

Hardware components

Photon
Particle Photon
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015

Story

Read more

Schematics

Photon PIR Motion Sensor

Code

PIR Motion Stream UWA

C#
You need to make sure to use your own Device ID and Access Token. As I did with in my Useless Machine project, I’m connecting to a Particle Event Stream. In this case the event name is “Motion”. We’re reading the Json event data and then displaying the motion indicator based upon the event value.
public sealed partial class MainPage : Page
{
    const string PHOTON1DEVICEID = "{YOURDEVICEID}";
    const string ACCESS_TOKEN = "{YOURACCESSTOKEN}";
    SolidColorBrush MotionColor = new SolidColorBrush(Colors.Red);
    SolidColorBrush ClearColor = new SolidColorBrush(Colors.Green);
    public MainPage()
    {
        this.InitializeComponent();
    }
    private async void Open_Stream_Click(object sender, RoutedEventArgs e)
    {
        (sender as Button).IsEnabled = false;
        string url = String.Format("https://api.particle.io/v1/devices/{0}/events/Motion?access_token={1}", PHOTON1DEVICEID, ACCESS_TOKEN);
        WebRequest request = WebRequest.Create(url);
        request.Method = "GET";
        using (WebResponse response = await request.GetResponseAsync())
        {
            using (Stream stream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(stream);
                int blankRead = 0;
                while (blankRead <= 5)
                {
                    var str = await reader.ReadLineAsync();
                    if (string.IsNullOrEmpty(str))
                    {
                        ++blankRead;
                        if (blankRead > 1)
                        {
                            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                streamResultTextBox.Text = "Blank: " + blankRead.ToString();
                            });
                        }
                    }
                    else if (str == "event: Motion")
                    {
                        blankRead = 0;
                        var data = await reader.ReadLineAsync();
                        var jsonData = JsonConvert.DeserializeObject<ParticleEvent>(data.Substring(data.IndexOf("data:") + 5));
                        await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            streamResultTextBox.Text = jsonData.data > 2048 ? "Motion" : "Clear";// "Data: " + jsonData.data;
                            streamResultTextBox.Foreground = jsonData.data > 2048 ? MotionColor : ClearColor;
                        });
                    }
                }
            }
        }
        (sender as Button).IsEnabled = true;
    }
}
public class ParticleEvent
{
    public int data { get; set; }
    public string ttl { get; set; }
    public string published_at { get; set; }
    public string coreid { get; set; }
}

PIR Motion Sensor Firmware

C/C++
I’m using a loop to take 100 quick readings within 1 second. The reason I’m doing this is in case the output latch delay on the motion sensor is set to go off in less than a second. The loop takes multiple values and keeps the largest one. Every second that max value is sent to the cloud using the “Motion” event.
#define PIRPIN A0
int val;
void setup() {

}

void loop() {
    val = 0;
    for(int i = 0; i < 100; i += 1)
    {
        int rval = analogRead(PIRPIN);
        if (rval > val)
        {
            val = rval;
        }
        delay(10);
    }
    Particle.publish("Motion",String(val));
}

Photon PIR Motion Sensor UWA

Credits

Bary Nusz

Bary Nusz

8 projects • 32 followers
Nusz Labs “Tinkerer-in-Chief”.

Comments

Add projectSign up / Login