from machine import Pin, SoftI2C
from utime import sleep
import oled_library, ultrasonic_library
import urequests as requests
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
trig = 19
echo = 18
led_pin = Pin(27, Pin.OUT)
button_pin = Pin(26, Pin.IN)
Calculate_of_car = 0
sensor = ultrasonic_library.HCSR04(trigger_pin=trig, echo_pin=echo)
screen = oled_library.SSD1306_I2C(width=128, height=64, i2c=oled_pin)
# Main loop
while True:
distance_in_cm = sensor.distance_cm()
logic_state = button_pin.value()
if logic_state == 0:
print("Button pressed!")
led_pin.on()
sleep(1)
screen.fill(0) # Clear the screen
screen.text("Streetlight", 0, 0)
screen.text("Status " , 0, 20)
screen.text("LED ON: Button", 0, 40)
screen.show()
# Check ultrasonic sensor
elif distance_in_cm < 10: # Adjust the distance threshold to 10 cm
print("Motion detected!")
screen.fill(0) # Clear the screen
screen.text("Streetlight", 0, 0)
screen.text("Status " , 0, 20)
screen.text("LED ON: MOTION", 0, 40)
screen.show()
Calculate_of_car = Calculate_of_car + 1
sleep(1)
print("Number Of Car =", Calculate_of_car)
led_pin.on()
else:
led_pin.off()
light_status = "LED OFF"
print("Number Of Car =", Calculate_of_car)
# Update OLED display
screen.fill(0) # Clear the screen
screen.text("Streetlight", 0, 0)
screen.text("Status " , 0, 20)
screen.text(light_status, 0, 40)
screen.show()
sleep(2)
#Step 1
#data_from_sensors = {"channel_id": 2585282,"field1": Calculate_of_car}
#Step 2
#HTTP_header = {'Content-Type' : 'application/json'}
#Step 3
#data_submit = requests.request("POST",url='https://api.thingspeak.com/update?api_key=V8U5KPS49CKNDZJM&',json=data_from_sensors,headers=HTTP_header)
#data_submit.close()