Pflanzenmonitor¶
In diesem Projekt erfassen wir sowohl die Lichtintensität als auch den Feuchtigkeitsgehalt des Bodens und zeigen diese auf dem I2C LCD1602 an. Wenn Sie denken, dass die Bodenfeuchtigkeit unzureichend ist, können Sie das Tastenmodul drücken, um die Topfpflanze zu bewässern.
Schritte
In diesem Projekt wird ein I2C LCD1602 verwendet, daher ist es notwendig, die relevanten Bibliotheken herunterzuladen, um es zum Laufen zu bringen.
cd ~/ wget https://github.com/sunfounder/raphael-kit/blob/master/python/LCD1602.py
Installieren Sie
smbus2
für I2C.sudo pip3 install smbus2
Speichern Sie den folgenden Code auf Ihrem Raspberry Pi und geben Sie ihm einen Namen, zum Beispiel
plant_monitor.ty
.from robot_hat import ADC, Motors, Pin import LCD1602 import time import threading from robot_hat.utils import reset_mcu reset_mcu() time.sleep(.1) # Initialize objects light_sensor = ADC(1) moisture_sensor = ADC(0) motors = Motors() button = Pin('D0') # Thread running flag running = True def init_lcd(): LCD1602.init(0x27, 1) time.sleep(2) def update_lcd(light_value, moisture_value): LCD1602.write(0, 0, 'Light: %d ' % light_value) LCD1602.write(0, 1, 'Moisture: %d ' % moisture_value) def read_sensors(): light_value = light_sensor.read() time.sleep(0.2) moisture_value = moisture_sensor.read() time.sleep(0.2) return light_value, moisture_value def control_motor(): global running while running: button_pressed = button.value() == 0 if button_pressed: motors[1].speed(80) time.sleep(0.1) else: motors[1].speed(0) time.sleep(0.1) time.sleep(0.1) def setup(): init_lcd() def destroy(): global running running = False LCD1602.clear() def loop(): global running while running: light_value, moisture_value = read_sensors() update_lcd(light_value, moisture_value) time.sleep(.2) if __name__ == '__main__': try: setup() motor_thread = threading.Thread(target=control_motor) motor_thread.start() loop() except KeyboardInterrupt: motor_thread.join() # Wait for motor_thread to finish print("Program stopped") except Exception as e: print("Error:", e) finally: motors[1].speed(0) time.sleep(.1) destroy() print('end')
Verwenden Sie den Befehl
sudo python3 plant_monitor.ty
, um diesen Code auszuführen.