-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_communication.py
83 lines (72 loc) · 2.35 KB
/
arduino_communication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# @autor Dawid Sobczak
# @date 2022-06-03
# @brief Arduino serial port communication protocole
#
# Tested on:
# Arduino Micro
# IMPORTS ==============================================================================================================
import serial
import time
import json
from datetime import datetime
import sys
# SERIAL PORT ==========================================================================================================
# open serial port
arduino = serial.Serial('/dev/serial/by-id/usb-Arduino_LLC_Arduino_Micro-if00', 9600,
timeout=1, parity=serial.PARITY_NONE)
# print serial name to check
print(arduino.name)
# ping device
arduino.write(b'[OP_GET|PING]')
time.sleep(0.4)
text = arduino.read_until(expected=']')
time.sleep(1)
print(text)
# read data
tempIN = 0
tempOUT = 0
humdIN = 0
humdOUT = 0
dewPointIN = 0
dewPointOUT = 0
buffer = [[]]
tempINcmd = b'[OP_GET|TEMPERATURE_IN]'
humdINcmd = b'[OP_GET|HUMIDITY_IN]'
dewINcmd = b'[OP_GET|DEWPOINT_IN]'
tempOUTcmd = b'[OP_GET|TEMPERATURE_OUT]'
humdOUTcmd = b'[OP_GET|HUMIDITY_OUT]'
dewOUTcmd = b'[OP_GET|DEWPOINT_OUT]'
def main(args):
print(datetime.fromtimestamp(int(time.time())))
for arg in args:
if arg == "tempIN":
arduino.write(tempINcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg == 'tempOUT':
arduino.write(tempOUTcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg =='humdIN':
arduino.write(humdINcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg =='humdOUT':
arduino.write(humdOUTcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg =='dewIN':
arduino.write(dewINcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg =='dewOUT':
arduino.write(dewOUTcmd)
time.sleep(0.4)
print(arduino.read_until(expected=']'))
elif arg == "arduino_communication.py":
#NOTHING
print("~ARDUINO SERIAL COMMUNICATION~")
else:
print('Wrong or none argument!')
if __name__ == '__main__':
main(sys.argv)