-
Notifications
You must be signed in to change notification settings - Fork 1
/
code Circuit ESP32.py
161 lines (134 loc) · 3.78 KB
/
code Circuit ESP32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
MicroPython IoT Green house Control and managment
https://wokwi.com/projects/350213119863161428
"""
import network
import time
import machine
from machine import Pin ,PWM,SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
import dht
import ujson
from umqtt.simple import MQTTClient
from time import sleep
# MQTT Server Parameters
MQTT_CLIENT_ID = "micropython-weather-demo"
MQTT_BROKER = "broker.hivemq.com"
MQTT_USER = "A.D.M.S"#"ISI_MCU_Project"
MQTT_PASSWORD = ""#"STM32groupeISI"
MQTT_TOPIC = "ISIariana/2ING2/my_GreenHouse/sensors"
# compounds declaration
sensor = dht.DHT22(Pin(15))
led=Pin(13,Pin.OUT)
# servo (verin)
pwm = PWM(Pin(23))
pwm.freq(50)
#lcd
sdaPIN=machine.Pin(21) #for ESP32
sclPIN=machine.Pin(19)
I2C_ADDR = 0x27
totalRows = 4
totalColumns = 20
#initializing the I2C method for ESP32
i2c = SoftI2C(scl=Pin(19), sda=Pin(21), freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
I2C_ADDR = 0x27
totalRows = 4
totalColumns = 20
#initialisation of pins
led.value(1)
for position in range(5000,1000,-500):
pwm.duty_u16(position)
#initialisation of variables
waterOn="true"
lightValue="true"
prev_Data = ""
prev_waterOn=""
prev_Light=""
strLight="ON"
strWater="ON"
# here we will treat all the comming info from user (light / run water)
def sub_cb(topic, msg):
global prev_waterOn
global prev_Light
global strLight
global strWater
global lightValue
global waterOn
res=msg.decode('UTF-8')# res= "water value|light value"
info=res.split("|")# decomposing res
print(info)
waterOn=info[0]
lightValue=info[1]
# water control
if waterOn != prev_waterOn:
if(waterOn=="true"):
strWater="ON"
for position in range(9000,1000,-50):
pwm.duty_u16(position)
sleep(0.01)
if(waterOn=="false"):
strWater="OFF"
for position in range(1000,9000,50):
pwm.duty_u16(position)
sleep(0.01)
prev_waterOn=waterOn
# light control
if(lightValue!=prev_Light):
if(lightValue=="false"):
strLight="OFF"
led.value(0)
if(lightValue=="true"):
strLight="ON"
led.value(1);
prev_Light=lightValue
lcd.clear()
lcd.putstr("my green house info:")
lcd.putstr( "t: "+ str(sensor.temperature())+" H: "+str(sensor.humidity())+" ")
lcd.putstr( "Light: "+strLight+" " )
lcd.putstr( "Water Van: "+strWater )
# connection to wifi
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 to WiFi!")
# connection to wifi
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
#setting client handler
client.set_callback(sub_cb)
client.connect()
print("Connected!")
#listennig for controllers values (water|light)
client.subscribe("ISIariana/2ING2/my_GreenHouse/Controllers")
lcd.putstr("my green house info:")
while True:
#reading controller if exists new values (subscribed )
client.check_msg()
# reading sensor values
print("Measuring weather conditions... ", end="")
sensor.measure()
message = ujson.dumps({
"t": sensor.temperature(),
"h": sensor.humidity(),
"l": lightValue,
"w": waterOn
})
if message != prev_Data:
print("Updated!")
print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC, message))
client.publish(MQTT_TOPIC, message)
prev_Data = message
lcd.clear()
lcd.putstr("my green house info:")
lcd.putstr( "t: "+ str(sensor.temperature())+" H: "+str(sensor.humidity())+" ")
lcd.putstr( "Light: "+strLight+" " )
lcd.putstr( "Water Van: "+strWater )
else:
print("No change")
time.sleep(1)