from machine import Pin
from machine import PWM
from time import sleep
import dht
import network
import time
import ujson
from umqtt.simple import MQTTClient
# MQTT Server Parameters
MQTT_CLIENT_ID = ""
MQTT_BROKER = "broker.hivemq.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "temp/az"
MQTT_TOPIC1 = "hum/az"
MQTT_TOPIC2 = "nit/az"
MQTT_TOPIC3 = "pho/az"
MQTT_TOPIC4 = "pot/az"
MQTT_TOPIC5 = "npk/az"
sensor = dht.DHT22(Pin(2))
nitrogen = dht.DHT22(Pin(14))
phosphorus = dht.DHT22(Pin(12))
potassium = dht.DHT22(Pin(13))
pwm = PWM(Pin(19))
pwm.freq(50)
led = Pin(21, Pin.OUT)
def servo():
for position in range(1000,9000,50):
pwm.duty_u16(position)
sleep(0.01)
for position in range(9000,1000,-50):
pwm.duty_u16(position)
sleep(0.01)
def blink():
led.value(1)
sleep(1)
led.value(0)
sleep(1)
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
prev_weather = ""
prev_weather1 = ""
def msg():
#message5 = ujson.dumps()
print("Reporting to MQTT topic {}:{} ".format(MQTT_TOPIC5, "Plant Critical Condition!"))
client.publish(MQTT_TOPIC5, "Critical Condition Detected!")
def msg1():
print("Reporting to MQTT topic {}:{} ".format(MQTT_TOPIC5, "System Normal"))
client.publish(MQTT_TOPIC5, "System Normal")
def reset():
client.publish(MQTT_TOPIC5, "", 0, True)
while True:
print("Measuring weather conditions... ", end="")
#Temp & Humidity Read
sensor.measure()
nitrogen.measure()
phosphorus.measure()
potassium.measure()
temp = sensor.temperature()
hum = sensor.humidity()
nit = nitrogen.humidity()
pho = phosphorus.humidity()
pot = potassium.humidity()
print("Temperature: {}°C Soil Moist: {:.0f}% ".format(temp, hum))
print("Nitrogen: {} mg/kg ".format(nit))
print("Phosphorus: {} mg/kg ".format(pho))
print("Potassium: {} mg/kg ".format(pot))
sleep(2)
if temp > 38 and hum < 20:
servo()
blink()
msg()
elif hum < 20:
blink()
msg()
elif temp < 20:
blink()
msg()
elif nit < 50:
blink()
msg()
elif pho < 50:
blink()
msg()
elif pot < 50:
blink()
msg()
else:
led.value(0)
reset()
msg1()
message = ujson.dumps(sensor.temperature())
if message != prev_weather:
print("Updated!")
print("Reporting to MQTT topic {}:{}°C ".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
prev_weather = message
message1 = ujson.dumps(sensor.humidity())
if message1 != prev_weather:
print("Updated!")
print("Reporting to MQTT topic {}:{}% ".format(MQTT_TOPIC1, message1))
client.publish(MQTT_TOPIC1, message1)
prev_weather = message1
else:
print("No change")
message2 = ujson.dumps(nitrogen.humidity())
message3 = ujson.dumps(phosphorus.humidity())
message4 = ujson.dumps(potassium.humidity())
if message2 or message3 or message4 != prev_weather:
print("Updated!")
print("Reporting to MQTT topic {}:{} ".format(MQTT_TOPIC2, message2))
print("Reporting to MQTT topic {}:{} ".format(MQTT_TOPIC3, message3))
print("Reporting to MQTT topic {}:{} ".format(MQTT_TOPIC4, message4))
client.publish(MQTT_TOPIC2, message2)
client.publish(MQTT_TOPIC3, message3)
client.publish(MQTT_TOPIC4, message4)
prev_weather = message2
prev_weather = message3
prev_weather = message4
else:
print("No change")