Skip to content

Commit

Permalink
added library to communicate
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMarche committed Jan 8, 2023
1 parent 7845afc commit ba4138c
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions mushroom_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import serial
import serial.tools.list_ports
import time

def get_ports():
ports = serial.tools.list_ports.comports(include_links=False)
if len(ports) == 0:
raise Exception('failed to find a port')
return ports

def get_connection():
ports = get_ports()

for port in ports:
try:
ser = serial.Serial("COM12", baudrate=9600, timeout=10)
time.sleep(5)
return ser
except Exception as e:
print(e)
continue

def set_temperature(conn, temp):
to_send = '1 '+str(temp)+"\r\n"
conn.write(to_send.encode())
c = conn.readline().rstrip()
return float(c)

def set_hygro(conn, hygro):
to_send = '2 '+str(hygro)+"\r\n"
conn.write(to_send.encode())
c = conn.readline().rstrip()
return float(c)

def get_expected_tmp(conn):
conn.write(b"3\r\n")
c = conn.readline().rstrip()
return float(c)

def get_expected_hygro(conn):
conn.write(b"4\r\n")
c = conn.readline().rstrip()
return float(c)

def get_actual_tmp(conn):
conn.write(b"5\r\n")
c = conn.readline().rstrip()
return float(c)

def get_actual_hygro(conn):
conn.write(b"6\r\n")
c = conn.readline().rstrip()
return float(c)

if __name__ == "__main__":
conn = get_connection()
print("===DEFAULT COMMAND===")
print("Temperature command before : ", get_expected_tmp(conn))
print("Hygro command before : ", get_expected_hygro(conn))
print("===UPDATING COMMANDS===")
print("Setting temperature command to : ", set_temperature(conn, 25.0))
print("Setting hygro command to : ", set_hygro(conn, 60.0))
print("===COMMANDS AFTER UPDATING===")
print("Temperature command after : ", get_expected_tmp(conn))
print("Hygro command after : ", get_expected_hygro(conn))
print("===CURRENT VALUES===")
print("Current temperature : ", get_actual_tmp(conn))
print("Current Hygro : ", get_actual_hygro(conn))

0 comments on commit ba4138c

Please sign in to comment.