-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raspberry Steuerung Thonny
108 lines (93 loc) · 3.47 KB
/
Raspberry Steuerung Thonny
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
import machine
import utime
import dht
import network
import picozero
import socket
from time import sleep
# 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 for timed devices
light_state = {"on": False, "last_switch": utime.ticks_ms()}
motor_state = {"on": False, "last_switch": utime.ticks_ms()}
# Function to measure distance using HC-SR04
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
# Function to read moisture level from the sensor
def read_moisture():
moisture_value = moisture_sensor_pin.read_u16()
return moisture_value
# Function to update the state of a relay based on timing
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
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 moisture level
moisture_level = read_moisture()
print("Moisture Level:", moisture_level)
try:
# Read temperature and humidity from DHT11 sensor
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print('Temperature: {}°C, Humidity: {}%'.format(temp, hum))
# Control heating element based on temperature
if temp < target_temperature:
heating_relay.value(0)
else:
heating_relay.value(1)
# Control humidifier based on humidity
if hum < target_humidity:
humidifier_relay.value(0)
else:
humidifier_relay.value(1)
except OSError as e:
print('Failed to read sensor.')
# Measure distance using HC-SR04
distance = measure_distance()
alrm = 'keine'
water_level_percent = (1 - distance / tank_depth_cm) * 100 # Convert to percentage of tank depth
print('Water Level: {:.2f}%'.format(water_level_percent))
if water_level_percent < water_warning:
alrm = 'Tank Leer '
print(alrm) # Warning message
else:
print(alrm)
utime.sleep(2) # Avoid tight loop, giving CPU time for other tasks