parent
6c32af997d
commit
1428fa5722
32
main.py
32
main.py
|
@ -0,0 +1,32 @@
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
try:
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import gpiozero
|
||||||
|
except ImportError:
|
||||||
|
print('Some modules are missing. Try to install them with "pip3 install -r requirements.txt"')
|
||||||
|
exit()
|
||||||
|
|
||||||
|
def debug_print(message: str):
|
||||||
|
now = datetime.now()
|
||||||
|
current_time = now.strftime("%H:%M:%S")
|
||||||
|
print('[DEBUG]['+current_time+'] '+message)
|
||||||
|
|
||||||
|
def error_print(message: str):
|
||||||
|
now = datetime.now()
|
||||||
|
current_time = now.strftime("%H:%M:%S")
|
||||||
|
print('[ERROR]['+current_time+'] '+message)
|
||||||
|
|
||||||
|
def relay_controller(pin: int, status: str):
|
||||||
|
if status == "ON":
|
||||||
|
relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
||||||
|
relay.on()
|
||||||
|
elif status == "OFF":
|
||||||
|
relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
||||||
|
relay.off()
|
||||||
|
else:
|
||||||
|
error_print("Unknown status for relay ("+status+")")
|
||||||
|
|
||||||
|
def get_water_lvl():
|
||||||
|
|
24
test.py
24
test.py
|
@ -1,17 +1,15 @@
|
||||||
|
import smbus
|
||||||
import time
|
import time
|
||||||
import os
|
|
||||||
import gpiozero
|
|
||||||
|
|
||||||
pins = [14,15]
|
address = 0x57
|
||||||
|
sensor = smbus.SMBus(1)
|
||||||
|
|
||||||
# for pin in pins:
|
while True:
|
||||||
# print(pin)
|
sensor.write_byte(address, 0x01)
|
||||||
# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
|
||||||
# relay.on()
|
|
||||||
# time.sleep(1)
|
|
||||||
# relay.off()
|
|
||||||
|
|
||||||
relay = gpiozero.OutputDevice(14, active_high=False, initial_value=False)
|
time.sleep(0.2)
|
||||||
relay.on()
|
|
||||||
time.sleep(30)
|
bytes = sensor.read_i2c_block_data(address, 0, 3)
|
||||||
relay.off()
|
distance = ((bytes[0] << 16) + (bytes[1] << 8) + bytes[2]) / 10000
|
||||||
|
|
||||||
|
print(distance)
|
Loading…
Reference in New Issue