---
# System prepended metadata

title: Motion Flow

---

randy johnson rj222rq

# Overview
Motion Flow is two PIR motion sensors that indicate the direction of flow through an area.
The idea is to understand the frequency and direction of traffic in a specific area. 
The project has two PIR motion sensors that are not overlapping but are triggered by the same path. Mine are very close but angled 45 degrees from each other. The logic then reads the direction of the motion. The result is then published via MQTT to the ADAfruit dashboard.

Time to complete: an afternoon.

# Objective
Understanding the traffic flow through a specific area can be very useful. Warehouses with human driven forklifts are always trying to improve efficentcy, this might be an interesting way to collect data if applied in a larger scale.
In addition to data collection this could be a very cost effective saftey percaution. Today it is common for these fork lifts to use a colored light aimed at the ground a few meters in front of a moving truck.
The objective is to see the movement of objects and log it in a simple way and evaluate the results. I expect this single sensor will give an idea to if it is worth while to expand to multiple nodes. As a mesh I believe you could map the traffic and make informed decisions from that.

# Bill of materials
Everything purchased from electrokit.

- raspberry pi pico W
    - The controller
- Two PIR motion sensors
    - The sensors
- LED lamp (Optional)
    - Used during setup as a visual rep. of the pir sensors.
- Two 330ohm resistors (*For LED, also optional)
    - Current limiting as to protect the LED
- Wire
    - Connecting the components
- 47uf Capacitor 
    - Optional, helps stabalize the power supply to the sensors.
- Bredboard (optional, you can solder if you like)

# Assembly
*LDR is for future use.
![](https://hackmd.io/_uploads/ry0-UyeKn.png)

*Note PIR sensors are extremely sensitive and need to stable. 

# Setup
- IDE
    - VScode for programming
    - PyMakr plugin (For interfacing with the Pico)
- Node js needs to be installed.

# Platform
We are using adafruitIO for this project (free version). This platform was chosen for its simplicity. This project is stand alone, but if it succeeds it needs to be scaled and most likely modified. Adafruit IO is cloud based.
Server side:
We create a 'feed' for each datapoint we want to visualize, then we create a dashboard where we visualize the data.
Pico side:
We are using MQTT messaging, we are sending subscibed messages to be visualized. MQTT is nice to use in low power installations. For notifications I am publishing to a discord server. To do this I make use of the webhooks templates.

# Code
I have chosen a low code solution,
The PIR sensors are read as digital signals.
The logic reads the time each sensor is triggered.
The times then evaluate what direction the motion is.

Determine the direction of movement based on the timestamps
        if pir_1_time > 0 and pir_3_time > 0:
            if pir_1_time >= pir_3_time:
                print('Moving Left')

The connection with adafruit:
![](https://hackmd.io/_uploads/H13ZbfZth.png)


# Connectivity
I am utilizing the onboard WIFI. This requires the network and time libraries. 

MQTT protocal. 
Messaging is limited by a 20 second interval to prevent exceeding the data limits. However the data is not stored, we are only sending the most recent movement. If better data is needed this interval should be adjusted or the data should be sent differently.


# Library use
- time : enables use of intervals and time out functions.
- mqtt : enabes communication using MQTT protocol
- ubinascii : ASCI - Bianary conversions
- machine : micropython access to GPIO
- micropython : enables python on microcontroller
- network : enables access to internet
- secrets : avoiding inline network credentials.

# Reasoning

This project is thought to be inside a warehouse, WIFI is aboundant. The Pico has onboard WIFI.
This project has very little requirements on the data transfer. MQTT can send the packages with not problem. The main plus from the MQTT is the low power alternatives, although not used on this stage of the project.

# Dashboard
I have chosen adafruit IO fo rmy dashboard. I have been set the data collection to a one hour window but that is open to change in the dashboard,  this may be usefull while collecting data. 
I also send the data as a digital signal. 1 = left movement and 2= right movement.
![](https://hackmd.io/_uploads/Sy2lMyeFn.png)
 
 Inside the adafruit IO I created an action that made use of webhooks and published the data onto my personal discord server.
![](https://hackmd.io/_uploads/HJ6iRbZFh.png)

 # Summery
These sensors are not ideal in this layout there was overlap that created conflict. 
The connectivity worked great, adafruit IO was nice to work with. The raspberry pi pico was fun to work with. I think this sensor could be worth exploring. 


![](https://hackmd.io/_uploads/SJRPBJgt3.jpg)

![](https://hackmd.io/_uploads/BkDuSkeFh.jpg)

