-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raspberry Steuerung mit HMI
155 lines (137 loc) · 4.81 KB
/
Raspberry Steuerung mit HMI
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
import machine
import utime
import dht
import network
import picozero
import socket
from time import sleep
import machine
import errno
# WiFi credentials
ssid = '' # write the WLAN Name ''
password = '' # write the password between ''
# Initialize GPIO pins
sensor = dht.DHT11(machine.Pin(16))
light_relay = machine.Pin(15, machine.Pin.OUT, value=0)
heating_relay = machine.Pin(14, machine.Pin.OUT, value=0)
motor_relay = machine.Pin(0, machine.Pin.OUT, value=1)
humidifier_relay = machine.Pin(1, machine.Pin.OUT, value=0)
trig_pin = machine.Pin(7, machine.Pin.OUT)
echo_pin = machine.Pin(6, machine.Pin.IN)
moisture_sensor_pin = machine.ADC(machine.Pin(26))
# Tank and environmental configurations
tank_depth_cm = 23
target_temperature = 30
target_humidity = 70
water_warning = 15 - 0.5
# Timing configurations
light_timing = (14400, 36000) # 10 hours/day ON --> (OFF, ON) seconds
motor_timing = (259200, 5) # 5 seconds/3days ON --> (OFF, ON) seconds
# Initial relay states
light_state = {"on": False, "last_switch": utime.ticks_ms()}
motor_state = {"on": False, "last_switch": utime.ticks_ms()}
def measure_distance():
trig_pin.value(0)
utime.sleep_us(2)
trig_pin.value(1)
utime.sleep_us(10)
trig_pin.value(0)
while echo_pin.value() == 0:
signal_off = utime.ticks_us()
while echo_pin.value() == 1:
signal_on = utime.ticks_us()
time_passed = signal_on - signal_off
distance = (time_passed * 0.0343) / 2
return distance
def read_moisture():
return moisture_sensor_pin.read_u16()
# updating relay based on time (light & Pumpe)
def update_relay(relay, timing, state):
current_time = utime.ticks_ms()
off_time, on_time = timing
if state["on"] and utime.ticks_diff(current_time, state["last_switch"]) >= on_time * 1000:
relay.value(1)
state["on"] = False
state["last_switch"] = current_time
elif not state["on"] and utime.ticks_diff(current_time, state["last_switch"]) >= off_time * 1000:
relay.value(0)
state["on"] = True
state["last_switch"] = current_time
# Initialize WiFi connection
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('Network config:', wlan.ifconfig())
return wlan.ifconfig()[0]
# opening a socket to receive data from the pico (IP from DHCP-Server, port:80)
def open_socket(ip):
addr = (ip, 80)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(addr)
s.listen(1)
print('Listening on', addr)
return s
# Webpage configuration (With HTML)
def webpage(temp, hum, moisture, water_level, motor, light, heating, humidifier):
html = f"""<!DOCTYPE html>
<html>
<head>
<title>Gewaechshaus</title>
<meta http-equiv="refresh" content="10">
</head>
<body>
<h1>Gewaechshaus</h1>
<p>Temperatur: {temp} grad C</p>
<p>Luftfeuchtigkeit: {hum}%</p>
<p>FeuchtigkeitErde: {moisture}</p>
<p>FuellstandTank: {water_level}%</p>
<p>Alarm: {alrm}</p>
<p>Pumpe: {motor}</p>
<p>Licht: {light}</p>
<p>Heizung: {heating}</p>
<p>Luftbefeuchter : {humidifier}</p>
</body>
</html>"""
return html
# Main loopwhile
try:
ip = connect_wifi(ssid, password)
s = open_socket(ip)
while True:
# Update light and motor relays based on timing
update_relay(light_relay, light_timing, light_state)
update_relay(motor_relay, motor_timing, motor_state)
# Read sensors
moisture = read_moisture()
distance = measure_distance()
water_level = max(0, min(100, (1 - distance / tank_depth_cm) * 100))
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
alrm = 'keine'
if water_level < water_warning:
alrm = ' Tank Leer '
# Update based on conditions
heating_relay.value(temp > target_temperature)
humidifier_relay.value(hum > target_humidity)
# Serve webpage
client, addr = s.accept()
print('Connection from %s' % str(addr))
request = client.recv(1024)
client.send('HTTP/1.1 200 OK\n')
client.send('Content-Type: text/html\n')
client.send('Connection: close\n\n')
client.sendall(webpage(str(temp), str(hum), str(moisture), str(int(water_level)),
"EIN" if motor_relay.value() == 0 else "AUS",
"EIN" if light_relay.value() == 0 else "AUS",
"EIN" if heating_relay.value() == 0 else "AUS",
"EIN" if humidifier_relay.value() == 0 else "AUS"))
client.close()
utime.sleep(2) # Avoid tight loop, giving CPU time for other tasks
except KeyboardInterrupt:
machine.reset()