-
1Wiring of the Raspberry Pi 4B
[Needed : 3x 220/ 1x 750/ 1x 1500 Ω resistors, Raspberry Pi 4B, US-015, LL509RGB, jumper cables, breadboard]
Using the breadboard, wire the Raspberry Pi following this diagram :
-
2Configuration of the Raspberry Pi
[Needed : microSD reader, network, USB-C to USB cable, power source]
The whole configuration process has been explained in this log : [Extra Session] Focusing on the network and code environment (Raspberry Pi 4).
Follow those steps but be aware that the configuration is tricky and you might spend a lot of time on it.
-
3Create a database
[Needed : Google account]
Create a new database on https://firebase.google.com/ and copy the link (add .json at the end) :
-
4Code on the Raspberry Pi
[Needed : microSD reader, network, USB-C to USB cable, power source]
- Connect to the Raspberry with your computer
- Type in the root of the Raspberry "touch main_program.py" and "sudo nano main_program.py" start coding the program.
- Write the following code and save using Ctrl+X > Y > Enter.
import RPi.GPIO as GPIO import time import requests """ < VAR DEF > """ TRIG=18 ECHO=24 RED=2 BLUE=8 GREEN=25 ln_trash = 50 #Length of the trash ln_avg = 5 #Number of values used to calculate the avg length values = [] """ < SETUP OF THE GPIOs > """ GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG,GPIO.OUT) GPIO.setup(ECHO,GPIO.IN) GPIO.setup(RED,GPIO.OUT) GPIO.setup(GREEN,GPIO.OUT) GPIO.setup(BLUE,GPIO.OUT) """ < RESET OF THE LEDS > """ GPIO.output(RED, False) GPIO.output(BLUE, False) GPIO.output(GREEN, False) """ < MAIN PROCESS > """ while (True) : #CALCULATING THE DISTANCE GPIO.output(TRIG,False) time.sleep(0.5) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG,False) while GPIO.input(ECHO)==0: pulse_start = time.time() while GPIO.input(ECHO)==1: pulse_end=time.time() pulse_duration=pulse_end-pulse_start distance=pulse_duration*17000 distance=round(distance,1) #Calculating the avg value if len(values) == ln_avg : values.pop(0) values.append(distance) avg_value = 0 for val in values : avg_value += val avg_distance = avg_value/len(values) print("Distance : ",avg_distance,"cm") #UPDATING THE DATA AND THE LED if (distance < ln_trash*0.3) : # < 30% remaining GPIO.output(RED, True) GPIO.output(BLUE, False) GPIO.output(GREEN, False) print("LED color : RED") value = 100 elif (distance < ln_trash*0.6) : # <60% remaining GPIO.output(RED, True) GPIO.output(BLUE, False) GPIO.output(GREEN, True) print("LED color : YELLOW") value = 60 else : # >60% remaining GPIO.output(RED, False) GPIO.output(BLUE, False) GPIO.output(GREEN, True) print("LED color : GREEN") value = 30 #SENDING DATA TO THE SERVER PART response = requests.delete("https://iot-trashcan-default-rtdb.europe-west1.firebasedatabase.app/events.json") print("> Delete data : ",response.status_code) params = {'value': value} headers = {'Content-Type': 'application/json'} response = requests.post("https://iot-trashcan-default-rtdb.europe-west1.firebasedatabase.app/events.json", json = params, headers = headers) print("> Send data : ",response.status_code,"\n") GPIO.cleanup()
/!\ Change the link of the database to the on you got from Firebase on the third step /!\
- To test the program type "python3 main_program.py" in the console and to stop, press Ctrl+Z or unplug the Raspberry, the value on Firebase should be updating live while running.
-
5Host the webpage
[Needed : Netlify/GitHub account]
- Open Netlify by linking your Github account
- Post on GitHub the files of the .zip.
- Start the server : you can access the the website
-
6Lasercut the box
[Needed : lasercutting machine, plexiglas, glue gun]
- Lasercut the 2 svg files (Big box, small box)
- Make holes : one for the LED, one to have access to the battery, one to pass the cables and one for the sensors
- Build the boxes and put you device in them (small box for the sensor and the big one for all the rest)
-
7End of the project
- Put your device on the cover of a trashcan
- Change the "ln_trash" value in the python script according to the size of the bin
- Start the python script in a new screen (type in the console : "screen -S new_screen" and "python3 main_program.py")
- Look in live the map updating to the emptiness of the bin
Example website : https://iot-trashcan.netlify.app/
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.