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 os
|
||||
import gpiozero
|
||||
|
||||
pins = [14,15]
|
||||
address = 0x57
|
||||
sensor = smbus.SMBus(1)
|
||||
|
||||
# for pin in pins:
|
||||
# print(pin)
|
||||
# relay = gpiozero.OutputDevice(pin, active_high=False, initial_value=False)
|
||||
# relay.on()
|
||||
# time.sleep(1)
|
||||
# relay.off()
|
||||
while True:
|
||||
sensor.write_byte(address, 0x01)
|
||||
|
||||
time.sleep(0.2)
|
||||
|
||||
relay = gpiozero.OutputDevice(14, active_high=False, initial_value=False)
|
||||
relay.on()
|
||||
time.sleep(30)
|
||||
relay.off()
|
||||
bytes = sensor.read_i2c_block_data(address, 0, 3)
|
||||
distance = ((bytes[0] << 16) + (bytes[1] << 8) + bytes[2]) / 10000
|
||||
|
||||
print(distance)
|
Loading…
Reference in New Issue