from machine import Pin, PWM
import machine
import network
import time
import dht
from blynkLib import Blynk # for blynk
import urequests # for http API for thingspeak
import sys # for thingspeak
# Connect to Wi-Fi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
while not wifi.isconnected():
pass
print("Wifi Connected Successfully")
# <--- connecting to blynk
BLYNK_AUTH_TOKEN = "YJSdmJQoY9ACyUCgvxbrH4JN-IS38uaC"
# Initialize Blynk
blynk = Blynk(BLYNK_AUTH_TOKEN)
# connecting to blynk --->
# <--- connecting to thingspeak
# Thingspeak HTTP API Protocol (Connection)
HTTP_HEADERS = {'Content-Type': 'application/json'}
THINGSPEAK_WRITE_API_KEY = 'KDNPIDZSZAZM9HZB' # Write API of the Channel
# Mohit channel api key
# variables
dryCount = 0
wetCount = 0
wrongInputCount = 0
# connecting to thingspeak --->
ledGreen1 = Pin(4,Pin.OUT)
ledGreen2 = Pin(16,Pin.OUT)
ledGreen3 = Pin(17,Pin.OUT)
ledRed1 = Pin(19,Pin.OUT)
ledRed2 = Pin(18,Pin.OUT)
ledRed3 = Pin(5,Pin.OUT)
Green = [ledGreen1, ledGreen2, ledGreen3,ledGreen1, ledGreen2, ledGreen3,ledGreen1, ledGreen2, ledGreen3]
Red = [ledRed1, ledRed2, ledRed3]
s1 = PWM(Pin(23), freq=50) # Initialize servo on pin 23
trigP1 = Pin(33, Pin.OUT)
echoP1 = Pin(32, Pin.IN)
trigP2 = Pin(26, Pin.OUT)
echoP2 = Pin(25, Pin.IN)
dht22 = dht.DHT22(Pin(21)) # DHT22 sensor on pin 21
buzzerPin = Pin(22, Pin.OUT)
moistureThreshold = 50 # Adjust this value based on your sensor's reading
#function to configure ultra-sonic
def read(trig, echo):
trig.off()
time.sleep_us(2)
trig.on()
time.sleep_us(10)
trig.off()
duration = machine.time_pulse_us(echo, 1)
distance = duration * 0.034 / 2
print("distance=",distance)
return distance
# Servo control function by blynk for dry section
def servo_control1(value1):
if int(value1[0]) == 1:
# Rotate the servo 90 degrees counterclockwise
for pos in range(77, -1, -1): # Equivalent to 90 to 0 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
else:
# Rotate the servo 90 degrees clockwise
for pos in range(0, 78): # Equivalent to 0 to 90 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
# Servo control function by blynk for wet section
def servo_control2(value2):
if int(value2[0]) == 1:
# Rotate the servo 90 degrees counterclockwise
for pos in range(77,181): # Equivalent to 90 to 0 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
else:
# Rotate the servo 90 degrees clockwise
for pos in range(180,76,-1): # Equivalent to 0 to 90 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
# Register Blynk handler for V3
blynk.on("V1", servo_control1)
blynk.on("V2", servo_control2)
while True:
# Measure distance from the sensor
distance1 = read(trigP1, echoP1)
distance2 = read(trigP2, echoP2)
print("Distance dry 1: {} cm, Wet 2: {} cm".format(distance1, distance2))
dht22.measure()
temperature = dht22.temperature()
humidity = dht22.humidity()
c=0 # counting variable for wrongInputCount
# Control servo based on the sensor values
# DRY SECTION WITH SOIL MOISTURE
if distance1 < 25:
for pos in range(77, -1, -1): # Equivalent to 90 to 0 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
dryCount = dryCount + 1 # Thingspeak
time.sleep(3)
dht22.measure()
temperature = dht22.temperature()
humidity = dht22.humidity()
while humidity > moistureThreshold:
# Air is humid, so turn on the buzzer
buzzerPin.on()
print("BUZZER ON")
dht22.measure()
temperature = dht22.temperature()
humidity = dht22.humidity()
print("TEMPERATURE: {} C, HUMIDITY: {}%".format(temperature, humidity))
c=1
for i in Red:
i.on()
time.sleep_ms(100)
i.off()
if c == 1:
wrongInputCount = wrongInputCount + 1 # Thingspeak
buzzerPin.off()
print("BUZZER OFF")
for i in Green:
i.on()
time.sleep_ms(200)
i.off()
time.sleep(2)
for pos in range(0, 78): # Equivalent to 0 to 90 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
# WET SECTION
if distance2 < 25:
for pos in range(77, 181): # Equivalent to 90 to 180 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
wetCount = wetCount + 1 # Thingspeaks
time.sleep(3)
for pos in range(181, 77, -1): # Equivalent to 180 to 90 degrees in Arduino
s1.duty(pos)
time.sleep_ms(5)
blynk.run() #code to run blynk
# Data sending to Thingspeak
dustbin_readings = {'field1':dryCount, 'field2':wetCount, 'field3':wrongInputCount}
request1 = urequests.post( 'http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json = dustbin_readings, headers = HTTP_HEADERS )
request1.close()
print("Dry Waste : ",dryCount)
print("Wet Waste : ",wetCount)
print("Wrong Input : ",wrongInputCount)
print(" Msg sent to Thingspeak channel successfully...")
print(" ********************************************")
time.sleep_ms(1000)