This repository was archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (91 loc) · 3.46 KB
/
main.py
File metadata and controls
110 lines (91 loc) · 3.46 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Author : Joel Garcia (@Newtoniano20 / Newtoniano#1173 on discord)
This code was created for the Project Shaking Hands Overseas with the purpouse of moving a hand from the other side
of the ocean. Any questions feel free to ask.
"""
from serial import Serial
from time import sleep
from requests import get
from json import loads
from time import sleep
# Code intro:
print('Arduino Serial Sender \n Author: @Newtoniano20 (Joel Garcia) \n Github: https://github.com/Shaking-Hands-Overseas/Arduino-Serial-Sender \n')
# Global Variables.
# SERIAL PORT: Windows = COM1, COM2,... // Linux = /dev/ttyACM0, /dev/ttyACM1,...
SERIAL_PORT = ['COM1', 'COM2', 'COM3', '/dev/ttyACM0', '/dev/ttyACM1', '/dev/ttyACM2']
# ARDUINO SERIAL BAUDRATE
BAUDRATE = 9600
# API URL FOR RECEIVING DATA
URL = 'https://xlbi6e.deta.dev/reciever'
def ask_user():
print(f"\n[0]'COM1', [1]'COM2', [2]'COM3', \n[3]'/dev/ttyACM0', [4]'/dev/ttyACM1', [5]'/dev/ttyACM2' ")
return input('Select a Serial Port:')
def arduino_connect(SELECTION):
print(f'Connecting to Serial Port {SERIAL_PORT[int(SELECTION)]}')
try:
ard = Serial(port=SERIAL_PORT[int(SELECTION)], baudrate=BAUDRATE, timeout=.1)
print(f"Connected with Arduino in serial port: {SERIAL_PORT[int(SELECTION)]}")
return ard
except:
print(f"[ERROR] Arduino not connected to serial port {SERIAL_PORT[int(SELECTION)]}")
raise Exception('Error while connecting to serial port specified')
# redundancy
SELECTION = ask_user()
i = True
while i:
try:
arduino = arduino_connect(int(SELECTION))
i = False
except:
SELECTION = ask_user()
def write_read(x):
data1 = bytes(x, 'utf-8')
#print(data1)
arduino.write(data1)
sleep(0.05)
data = arduino.readline()
return data
def getserver():
print('Connecting to Server...')
req = get(URL)
print(f'RESPONSE {req.status_code}')
if req.status_code == 500:
sleep(1)
return req
def jsonify_content(x):
return loads(x.content.decode())
while True:
try:
x = getserver()
try:
ct = jsonify_content(x) # Convert JSON => Dictionary Python
except:
print("[ERROR] Error while Jsonifying content")
ct = {"s1": 200, "s2": 200, "s3": 200, "s4": 200, "s5": 200}
except:
print(f"[ERROR] Error while connecting to the server {URL}")
ct = {"s1": 200, "s2": 200, "s3": 200, "s4": 200, "s5": 200}
cnt_index = ["s1", "s2", "s3", "s4", "s5"] #The indices of your data in the recieved JSON file
for index in cnt_index:
if int(ct[index]) < 10: # If the number is lower than 10
ct[index] = f"00{ct[index]}" #We add two zeros to the data
elif int(ct[index]) < 100: # If the number is lower than 100
ct[index] = f"0{ct[index]}" # We add one zero to the data
num = str(f'{ct["s1"]}{ct["s2"]}{ct["s3"]}{ct["s4"]}{ct["s5"]}') #The String That will be sent to the arduino with the information
try:
value = write_read(num)
print(value)
except:
i1 = 0
print(f"[ERROR] Error while sending data to arduino in port {SERIAL_PORT[int(SELECTION)]}")
try:
arduino = arduino_connect(SELECTION)
except:
i += 1
if i > 10:
print('\n\n')
SELECTION = ask_user()
try:
arduino = arduino_connect(SELECTION)
except:
pass