feature/change_sensor_handling #1

Merged
moltox merged 9 commits from feature/change_sensor_handling into main 2024-04-27 21:38:03 +00:00
6 changed files with 133 additions and 10 deletions
Showing only changes of commit e0a2ab9fd7 - Show all commits

3
REDME.md Normal file
View File

@ -0,0 +1,3 @@
# Show output on terminal #
`minicom -b 115200 -o -D /dev/cu.usbmodem3301`

24
grow_system.py Normal file
View File

@ -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)

31
main.py
View File

@ -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/

21
moisture_sensor.py Normal file
View File

@ -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()

View File

@ -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)

33
wlan.py Normal file
View File

@ -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()