From e0a2ab9fd793724aa8944c933ba7d5d2e468b936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20Mu=CC=88ller?= Date: Thu, 25 Apr 2024 14:44:04 +0200 Subject: [PATCH] [WIP] moisture sensor working so far --- REDME.md | 3 +++ grow_system.py | 24 +++++++++++++++++++ main.py | 31 +++++++++++++++++-------- moisture_sensor.py | 21 +++++++++++++++++ notes/code_sample_moisture_sensor.txt | 31 +++++++++++++++++++++++++ wlan.py | 33 +++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 REDME.md create mode 100644 grow_system.py create mode 100644 moisture_sensor.py create mode 100644 notes/code_sample_moisture_sensor.txt create mode 100644 wlan.py diff --git a/REDME.md b/REDME.md new file mode 100644 index 0000000..bc52edf --- /dev/null +++ b/REDME.md @@ -0,0 +1,3 @@ +# Show output on terminal # +`minicom -b 115200 -o -D /dev/cu.usbmodem3301` + diff --git a/grow_system.py b/grow_system.py new file mode 100644 index 0000000..72f72af --- /dev/null +++ b/grow_system.py @@ -0,0 +1,24 @@ +import time +from moisture_sensor import MoistureSensor + + +class GrowSystem: + + moisture_sensor = None + + most_recent_values = {} + + def __init__(self, settings): + print("hello from GrowSystem") + print(settings) + if not self.moisture_sensor: + self.moisture_sensor = MoistureSensor(settings['moisture_sensor']) + + def start(self): + print("Start reading sensors ...") + while True: + self.moisture_sensor.read() + self.most_recent_values['moisture_sensor'] = self.moisture_sensor.most_recent_value + print(self.most_recent_values) + time.sleep(1) + diff --git a/main.py b/main.py index 94e3a87..90c34d2 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,27 @@ -# This is a sample Python script. +# GrowSystem +# Author: Maik Müller (maik@muelleronlineorg) -# Press ⌃R to execute it or replace it with your code. -# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. +# WIP! +# This file should do only: +# - provide constants for settings +# - eventually necessary system settings +# - Call base class, permitting the configured constants +from grow_system import GrowSystem +settings = { + 'wlan_ssid': 'Oppa-95.lan', + 'wlan_pw': '95%04-MM', + 'led_pin_int': 15, + 'moisture_sensor': { + 'pin_int': 26 + }, + 'pump_pin_int': 24 +} # Press the green button in the gutter to run the script. if __name__ == '__main__': - print_hi('PyCharm') + print("Start grow system") + gs = GrowSystem(settings) + gs.start() -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ + # See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/moisture_sensor.py b/moisture_sensor.py new file mode 100644 index 0000000..a7e6deb --- /dev/null +++ b/moisture_sensor.py @@ -0,0 +1,21 @@ +# Moisture Sensor Class +from machine import ADC, Pin + + +class MoistureSensor: + moisture_sensor_pin_int = -1 + + moisture_sensor = None + + most_recent_value = -1 + + def __init__(self, moisture_sensor_data): + self.moisture_sensor_pin_int = moisture_sensor_data['pin_int'] + print("Hello from moisture sensor. Sensor pin is: " + str(self.moisture_sensor_pin_int)) + self.moisture_sensor = ADC(Pin(self.moisture_sensor_pin_int)) + + def read(self): + self.most_recent_value = self.moisture_sensor.read_u16() + + + diff --git a/notes/code_sample_moisture_sensor.txt b/notes/code_sample_moisture_sensor.txt new file mode 100644 index 0000000..64a5eaf --- /dev/null +++ b/notes/code_sample_moisture_sensor.txt @@ -0,0 +1,31 @@ +from machine import Pin, ADC +import time +import network +import urequests +import statistics +import secrets +sensor = ADC(Pin(26)) +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(secrets.SSID, secrets.PASSWORD) +time.sleep(5) +print(wlan.isconnected()) +readings = [] +try: + while True: + for i in range(5): + reading = sensor.read_u16() + readings.append(reading) + print(readings) + time.sleep(1) + median_value = statistics.median(readings) + if median_value < 400: + urequests.get("https://api.telegram.org/bot"+secrets.API+"/sendMessage?text=Gary is thirsty&chat_id="+secrets.ID) + print("Message Sent") + else: + print("Gary has enough water") + time.sleep(3600) +except OSError: + print("@"*68) + print("@ Cannot connect to the Wi-Fi, please check your SSID and PASSWORD @") +print("@"*68) \ No newline at end of file diff --git a/wlan.py b/wlan.py new file mode 100644 index 0000000..5050ba7 --- /dev/null +++ b/wlan.py @@ -0,0 +1,33 @@ +import machine +import network +import time + +# network.country('DE') + + +class WlanClient: + + ssid = '' + pw = '' + wlan = network.WLAN(network.STA_IF) + # Status-LED + led_onboard = machine.Pin('LED', machine.Pin.OUT) + led_onboard.value(False) + + def __init__(self, ssid, pw): + self.ssid = ssid + self.pw = pw + + def connect(self): + if not self.isConnected(): + print('No WLAN connected. Connecting ...') + self.wlan.active(True) + self.wlan.connect(self.ssid, self.pw) + for i in range(10): + if self.wlan.status() < 0 or self.wlan.status() >= 3: + break + led_value = self.led_onboard.value() == 1 + self.led_onboard.value(led_value) + + def isConnected(self): + return self.wlan.isconnected() \ No newline at end of file