IoT Device and Application - microsoft/uwp-shop-analytics-sample GitHub Wiki

[IN PROGRESS]

Hardware Requirements

To create the IoT Device that can detect exit or entry through a door or other enclosed structure, you will need the following hardware:

  • IoT board running Windows 10 IoT Core (view support boards and get started here)
  • 2 x Infrared beam break sensor (I used these sensors from adafruit)
  • Note - the sensors I used have a range of up to 20 inches. I your enclosure is bigger than 20 inches, you may need a more powerful LED/sensor
  • 2 x pull-up resistors for the IR beam break sensors
  • The requirements will vary depending on the board you use. For Raspberry Pi 2, I used 10K resistors
  • Optional: 2 LED used as sensor indicators (to notify you of the state of the beam break sensor)
  • Optional: 2 x pull-up resistors for the LEDs
  • I used the LEDs from this sample, and so I needed 2 x 220 ohm resistors
  • Some sort of enclosure to fasten the IR beam sensors and make sure they are aligned (such as a door or prototype door)
  • Fasteners for the IR beam sensor (glue, tape, nails, etc...)

#Schematic Below you can find the schematic that matches the code exactly using a Raspberry Pi 2 device. A brief explanation is included.

  • IR LEDs are on the left and are always on (when the Raspberry Pi 2 has power)
  • IR sensors are on the right
  • Blue wire indicates bridging the digital pins for the IR sensor to the Raspberry Pi 2
  • The IR sensors are connected to 10K pull-up resistors
  • LEDs are in the middle (towards the left)
  • Green wire indicates bridging the digital pins from the LEDs to the Raspberry Pi 2
  • The LEDs are connected to 220 OHM pull-up resistors
  • Red wire always bridges to hot (5V)
  • Black wire always bridges to ground Iot Schematic Image made with Fritzing

#IoT Application High Level Overview The IoT consists of three major parts:

  1. Initialization of the device and hardware
  2. Registering events based on sensor values
  3. Calling into web API to record an event happened

Initializing the Device and Hardware

There are a lot of samples out there for how this works with Windows 10 IoT core, but effectively all I need to do is register the pins for each of the devices I have connected to the Raspberry Pi 2 (4 in total, 2 x LEDs and 2 x IR Sensors). To do that, I use the GpioPin.OpenPin() method which takes in a parameter of the pin value from the Raspberry Pi 2 (if you follow the above schematic exactly, the pin values are already defined as constant integers).

Once the pin objects are opened, I can then set the drive mode of the open pin depending on if it is an input device (the sensors), or an output device (the LEDs).

Finally, for the input devices, I register two event handlers (one for each sensor) that detects when the value of that sensor pin changes. This event handler will execute code when the IR beam is broken by an object.

Registering the Events

To register an event, I want to detect when the beam is broken. Since the _ValueChanged methods will execute any time the value is changed (i.e. both when the beam is broken and when the beam is unbroken), I will have to check which event is actually happening. I can do that by checking the args.Edge enumeration for the GpioPinEdge value. If it is rising edge, the beam is unbroken so I don't want to execute any further logic (other than turning the light off). If it is falling edge, I want to then turn the light and execute further logic to detect what kind of event has happened (if any).

Event Detection Logic

To determine if someone has exited or entered the store, I have to figure out in which order were the sensors triggered. If "Enter" sensor is triggered immediately followed by "Exit" sensor, then someone has entered my store. If the opposite is true, then someone has exited my store.

To prevent false-positives, I also use a 500ms timer to reset the state of the device if one sensor is triggered but not the other. That way, if someone partially enters, or a fly triggers one of the sensors, the type of event that gets registered next will still be correct.

Calling the Web API to Record the Event

The event object is quite simple - it contains a boolean value (for enter or exit) and a DateTime value (for the time at which the event occurred). I then use the HttpClient class to create content to POST to my azure hosted web API.

⚠️ **GitHub.com Fallback** ⚠️