# Count things passing
Anton Ohlslöf
ao223nx
This is a tutorial that will help you make a device that can count how many times a solid form (a person, a remote-controlled car or similair) has passed it. This infrormation will later be possible to read from the pybytes.pycom.io website.
Time needed for completion: ~3 hours
## Objective
I chose the project since I belive it is of interest to people in general to be able to measure the flow of people in certain places as well as other things. It serves the purpose of beinge able to know the approximate number of passings happening in an area of a building. This would be handy information if one were to renovate a big building and want to know how many paople are using certain passageways. If deploying multipe devices one could easily create a heatmap of the movement of paople within the building, helping the arcitects make decitions about what passageways are needed and what can be used for other things.
## Material
I bought a bundle with all things needed for the prpject accept for the sensor used wich I just bought from the same place when I purchased the bundle. Link to bundle: https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/
The total cost of all things in the list below is 1216 SEK.
|Name | Function | Purchase link |Price|
| -------- | -------- | -------- |-------- |
| Pycom Lopy 4| "The brain" of the project. | https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/ |949 SEK|
|Pycom Expansion Board v3.1 |Enabling power, uploading and easy connections to the Lopy 4 |in bundle|in bundle|
|Ultra Sound distance sensor, HY-SRF05 |Performing distance measurments | https://www.electrokit.com/produkt/avstandsmatare-ultraljud-hy-srf05-2-450cm/ |49 SEK|
|Bread board | Making wiring easier | in bundle |in bundle|
|4 jumper wires | for wiring | in bundle |in bundle|
|Phone charger, output: 5V/1A |Powering the device | https://www.kjell.com/se/produkter/mobilt/ladda-koppla/mobilladdare/usb-laddare/linocell-mini-usb-laddare-24-a-vit-p90218 |99 SEK|
|Micro-Usb Cable | Connecting to computer and powering the device | https://www.kjell.com/se/produkter/mobilt/ladda-koppla/kablar-adaptrar/micro-usb-kablar/micro-usb-kabel-18-m-p68688 |119 SEK|

Lopy 4 from pycom.io

Expansion Board from pycom.io

Ultra-Sound Sensor from https://www.electrokit.com/produkt/avstandsmatare-ultraljud-hy-srf05-2-450cm/
## Computer Setup
Wondows 10
Node js
Firmware updater (did not need to update firware on exspansion board)
My computer is running Windows 10 as an operating system and theses are the software I needed to install for this project.
### Pycom Firmware Updater
I did not need to update the firmware of the Expansion Board, since it is quite new. I recommend trying to complete the project without updating doing this, just do it if you run into problems that are pointing you in the direction that this might be needed. However, updating the firmware of the Lopy 4 is a must to be able to connect to the Pybytes wbsite. You will therefore need to download the Pycom Firmware Updater. Follow this link for a tutorial of wor to performe a Firmware updater on a Pycom device: https://docs.pycom.io/updatefirmware/device/
### Node.js
Installation can be found here: https://nodejs.org/en/
### Visual Studio Code
For IDE i chose VSCode: https://code.visualstudio.com/
### Step-By-step
1. Update the Lopy4 firmware with the Pybytes token (follow this tutorial:https://docs.pycom.io/pybytes/gettingstarted/)
2. Install the Pymakr console in VSCode: https://docs.pycom.io/gettingstarted/software/vscode/
3. Wire everything up in accordence with the diagram.
4. Connect the Decive to your computer with Micro-USB.
5. Upload the Code provided below
6. Set up at the place were you want to measure and plug the device in at a regulair power socket with the Phone Charger
## Circuit diagram
Connect the pins from the sensor with the Pycom as can be seen below. The "OUT" pin on the sensor is not used.

I used a bread board for easier connections.

## Platform
I used the Pybytes platform for storing my data in the cloud. https://pybytes.pycom.io/
The data is stored for a month and I think this is a sufficient solution if you only want to measure for a monthly period. This was fitting for my needs and I do believe it can be a sufficient solution for people that want to measure for a bit longer as well. Since you only need to check and transfer the data once a month and it will simply be a figure of number of passages since the last reset.

Measuments of passages from https://pybytes.pycom.io/
## The Code
I Used the library for a sensor called HC-SR04, it worked well and can be found here: https://gitlab.lnu.se/1dt305/sensor-libs/-/tree/master/HC-SR04%20-%20Ultrasonic%20Sensor
The only differende is that the sensor I used (HY-SRF05) has the OUT-pin, but it worked to just not use it. Since the page demands a log in, I chose to include the "library-code" below as well. But I commented #Copied library starts and #Copied library ends, to make it more clear.
```
#Copied library starts
# https://core-electronics.com.au/tutorials/hc-sr04-ultrasonic-sensor-with-pycom-tutorial.html
import utime
import pycom
import machine
from machine import Pin
# initialise Ultrasonic Sensor pins
echo = Pin(Pin.exp_board.G7, mode=Pin.IN) # Lopy4 specific: Pin('P20', mode=Pin.IN)
trigger = Pin(Pin.exp_board.G8, mode=Pin.OUT) # Lopy4 specific Pin('P21', mode=Pin.IN)
trigger(0)
# Ultrasonic distance measurment
def distance_measure():
# trigger pulse LOW for 2us (just in case)
trigger(0)
utime.sleep_us(2)
# trigger HIGH for a 10us pulse
trigger(1)
utime.sleep_us(10)
trigger(0)
# wait for the rising edge of the echo then start timer
while echo() == 0:
pass
start = utime.ticks_us()
# wait for end of echo pulse then stop timer
while echo() == 1:
pass
finish = utime.ticks_us()
# pause for 20ms to prevent overlapping echos
utime.sleep_ms(20)
# calculate distance by using time difference between start and stop
# speed of sound 340m/s or .034cm/us. Time * .034cm/us = Distance sound travelled there and back
# divide by two for distance to object detected.
distance = ((utime.ticks_diff(start, finish)) * .034)/2
return distance * -1
#Copied library ends
#counter that is sent to pybytes
passing = 0
#variable used to manipulate ifelse statement
active = 2
#infinate loop that stops when device in unplugged
while True:
#performs a measurement and stores it in the value
value = distance_measure()
# used to sense when sensor starts being blocked
if value < 45 and active > 1:
#increase the passing counter
passing = passing + 1
#Sending passing to pybytes in channel 1
pybytes.send_signal(1, passing)
#printing in terminal
print("sending: {}".format(passing))
#change active to tigger "elif" in next loop
active = 0
#triggered if sensor is still blocked
elif value < 45 and active < 1 :
#printing in terminal
print("sensor blocked: {}".format(value))
#triggered when nothing is blocking the sensor
else:
#printing in terminal
print("no passage detected: {}".format(value))
#ensures the sensor will send to pybytes the next time it is blocked
active = 2
#sleep for 10 milliseconds
utime.sleep_ms(10)
```
## Transmitting Data / Connectivity
Data is sent to pybytes only when there is somthing in fron of the sensor within the specified distace. Which is in my code 45cm. I chose to use Wifi since I did not have coverage to any other network at my location. Since wifi is used along with connecting to Pybytes, the build in MQTT brokers is used for transporting the data.
## Presenting the data

Since the device is quite simple and the device is intended to be checked upon daily the dashboard that is used is the Pybytes website. It displays the number that the counter is curently at and that provides a sufficient user interface for this project.
## Finalizing the design
Below are pictures of the final project design. Evrything fit neatly into an empty and clean ice cream pack. I cut hoes for the usb-power coord and for the sensor to "look out". Over all I am quite pleased with the project, I might not be super proud of it but I think it was a nice way to get a very good overview and knowledge about iot and hor to build them, this is something I might very well use in the future, when I feel the need for it.
The things I would have wanted to do differently is to make the device not need to be plugged into the wall. However, the pycom has to have 5V power when using the Ultra sound sensor and it did not work at all when I tried the 3V that it gets from the battery pack I also bought. I might have been possible to solve but I felt like it was atill acceptable to power it with a regulair phone brick so I settled on it anyways.



