from machine import Pin
import utime
import dht # script für den Messer
import urequests
import os
import network
import json
import socket
machine.freq()
machine.freq(160000000)
rlayfan12=Pin(9,Pin.OUT)
rlayfancarbon=Pin(8,Pin.OUT)
rlaywaterbump=Pin(7,Pin.OUT)
rlayselond=Pin(6,Pin.OUT)
rlaygrowlight=machine.Pin(5,Pin.OUT)
sensor = dht.DHT22(Pin(12))
HEGRO1_SENSOR_PIN = machine.ADC(1)
HEGRO2_SENSOR_PIN = machine.ADC(2)
temperature_sensor = dht.DHT22(machine.Pin(12))
humidity_sensor = dht.DHT22(machine.Pin(12))
rtc=machine.RTC()
WIFI_SSID="MY WIFI SSD"
WIFI_PASSWORD="MY WIFI PASSWORD"
def connect_wifi():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.scan()
#wlan.isconnected()
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
if not wlan.isconnected() :
print('Waiting for connection...')
utime.sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
def open_socket(ip):
address=(ip,80)
connection=socket.socket()
connection.bind(address)
connection.listen(1)
return connection
def webpage(slight,sfan12,sfanc,swater,ssolind):
#Template HTML
html = f"""
<!DOCTYPE html>
</body>
<html>
<form action="./lighton">
<input type="submit" value="Light on" />
</form>
<form action="./lightoff">
<input type="submit" value="Light off" />
</form>
<p>LED is {slight}</p>
<hr>
<form action="./fan12on">
<input type="submit" value="fan12on" />
</form>
<form action="./fan12off">
<input type="submit" value="fan12off" />
</form>
<p>fan12 is {sfan12}</p>
<hr>
<form action="./carbonfanOn">
<input type="submit" value="carbonfan on" />
</form>
<form action="./carbonfanoff">
<input type="submit" value="carbonfan off" />
</form>
<p>CARBON FAN is {sfanc}</p>
<hr>
<form action="./waterbumbON">
<input type="submit" value="waterbumb on" />
</form>
<form action="./waterbumbOFF">
<input type="submit" value="waterbumb off" />
</form>
<p>waterbumb is {swater}</p>
<hr>
<form action="./solinedON">
<input type="submit" value="solined on" />
</form>
<form action="./solinedOFF">
<input type="submit" value="solined off" />
</form>
<p>solined is {ssolind}</p>
<hr>
<form action="./AUTOMATCON">
<input type="submit" value="auto on" />
</form>
<form action="./AUTOMATCOFF">
<input type="submit" value="auto off" />
</form>
<p>LED is {slight}</p>
<p> fan12 is {sfan12}</p>
<p> CARBON FAN is {sfanc}</p>
<p> waterbumb is {swater}</p>
<p> solined is {ssolind}</p>
<p> TEMP IS {state}</p>
<p> humo is {state}</p>
<p> water level{state}</p>
<hr>
</body>
making by RANA EL DAFAFEE
"""
return str(html)
def serve(connection):
#Start a web server
slight = 'OFF'
sfan12 ="OFF"
sfanc = 'OFF'
swater = 'OFF'
ssolind = 'OFF'
rlayfan12.off()
rlaygrowlight.off()
rlayfancarbon.off()
rlayselond.off()
rlaywaterbump.off()
fan=0
print("1")
while True:
print("2")
client = connection.accept()[0]
print("3")
request = client.recv(1024)
print("5")
request = str(request)
print("6")
try:
print("7")
request = request.split()[1]
except IndexError:
pass
if request == '/lighton?':
rlaygrowlight.on()
slight = 'ON'
elif request =='/lightoff?':
rlaygrowlight.off()
slight= 'OFF'
if request=='/fan12on?' :
rlayfan12.on()
sfan12 ='ON'
elif request=='/fan12off?' :
rlayfan12.on()
sfan12 ='OFF'
if request=='/carbonfanOn?' :
rlayfancarbon.on()
sfanc ='ON'
elif request=='/carbonfanoff?' :
rlayfancarbon.off()
sfanc ='OFF'
if request=='/waterbumbON?' :
rlaywaterbump.on()
swater ='ON'
elif request=='/waterbumbOFF?' :
rlaywaterbump.off()
swater='OFF'
if request=='/solinedON?' :
rlayselond.on()
ssolind='ON'
elif request=='/solinedOFF?':
rlayselond.off()
ssolind='OFF'
html = webpage(slight,sfan12,sfanc,swater,ssolind)
client.send(html)
client.close()
#function to set time when the light should start and stop
def set_light_schedule(start_hour,duration_hours):
current_time=rtc.datetime()
light_start_time=start_hour*3600
light_end_time=light_start_time+duration_hours*3600
return light_start_time,light_end_time
# Function to read soil moisture1
def read_soil_moisture1():
ldr_data = HEGRO1_SENSOR_PIN.read_u16()
onversion_factor = 100 / (65535)
moisture1 = 100 - (ldr_data* onversion_factor)
return moisture1
# Function to read soil moisture2
def read_soil_moisture2():
ldr_data1 = HEGRO2_SENSOR_PIN.read_u16()
onversion_factor = 100 / (65535)
moisture2 = 100 - (ldr_data1* onversion_factor)
return moisture2
# Function to read temperature
def read_temperature():
temperature_sensor.measure()
return temperature_sensor.temperature()
# Function to read humidity
def read_humidity():
humidity_sensor.measure()
return humidity_sensor.humidity()
# Function to control fans based on temperature
def control_temperature_fan():
temperature_threshold = 25 # Adjust the threshold as needed
temperature = read_temperature()
if temperature >= temperature_threshold:
rlayfan12.value(1)
print("temp fan on ")
# Turn on temperature fan
# Replace this with your actual fan control code
# Example: turn_on_fan(fan_temperature_pin)
pass
else:
rlayfan12.value(0)
print("temp fan off ")
# Turn off temperature fan
# Replace this with your actual fan control code
# Example: turn_off_fan(fan_temperature_pin)
pass
# Function to control fans based on humidity
def control_humidity_fan():
humidity_threshold = 70 # Adjust the threshold as needed
humidity = read_humidity()
if humidity >= humidity_threshold:
rlayfancarbon.value(1)
print("humidity fan on = ")
# Turn on humidity fan
# Replace this with your actual fan control code
# Example: turn_on_fan(fan_humidity_pin)
pass
else:
rlayfancarbon.value(0)
print("humidity fan off = ")
# Turn off humidity fan
# Replace this with your actual fan control code
# Example: turn_off_fan(fan_humidity_pin)
pass
# Function to control water pump based on soil moisture
# Function to control water pump based on soil moisture
def control_water_pump():
moisture_threshold = 50 # Adjust the threshold as needed
moisture11 = read_soil_moisture1()
moisture22 = read_soil_moisture2()
if moisture11 < moisture_threshold and moisture22 < moisture_threshold:
rlaywaterbump.value(1)
utime.sleep(1)
rlayselond.value(1)
print("water level under 50 water is on")
# Turn on water pump
# Replace this with your actual water pump control code
# Example: turn_on_water_pump(water_pump_pin)
pass
else:
rlaywaterbump.value(0)
rlayselond.value(0)
print("water level over 50 water is oof")
# Turn off water pump
# Replace this with your actual water pump control code
# Example: turn_off_water_pump(water_pump_pin)
pass
# Function to control grow light
def control_light(light_start,light_end):
current_time=rtc.datetime()
current_time_seconde=current_time[4]*3600+current_time[5]*60+current_time[6]
if light_start <=current_time_seconde< light_end:
rlaygrowlight.value(1)
print("light on")
else:
rlaygrowlight.value(0)
print("light off")
light_start,light_end=set_light_schedule(8,8)
print("testing the light on")
rlaygrowlight.value(1)
utime.sleep(5)
print("testing the light off")
rlaygrowlight.value(0)
utime.sleep(5)
print('systhem ON')
while True:
ip=connect_wifi()
connection=open_socket(ip)
serve(connection)
temperature = read_temperature()
humidity = read_humidity()
moisture = read_soil_moisture1()
moisture2 = read_soil_moisture2()
control_temperature_fan()
control_humidity_fan()
control_water_pump()
control_light(light_start,light_end)
# Adjust the sleep duration based on your application needs
utime.sleep(10) # Sleep for