-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunicator.py
More file actions
42 lines (34 loc) · 1.4 KB
/
Communicator.py
File metadata and controls
42 lines (34 loc) · 1.4 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
import serial
from time import sleep
class Communicator:
"""
The Communicator class handles a serial connection from the Pi to the ESP32.
To set this up, the two devices need to be connected together like so:
Raspberry Pi ESP32 Dev Board
+--------------+ +---------------+
| Pin 6 (GND) ------ GND |
| Pin 8 (TX) ------ RX2 (GPIO 16) |
| Pin 10 (RX) ------ TX2 (GPIO 17) |
+--------------+ +---------------+
Additionally, the Pi's serial monitor must be disabled:
sudo raspi-config -> Interface Options -> I6 Serial Port. Disable login shell but enable serial hardware
"""
def __init__(self, port="/dev/serial0", baud=115200, debug=False):
self.port = port
self.baudrate = baud
self.serial = None
self.debug = debug
try:
self.serial = serial.Serial(self.port, self.baudrate, timeout=1)
if self.debug: print("Opened Serial Port!")
sleep(2)
except serial.SerialException as e:
print("Could not open serial port %s" % e)
def send(self, text: str):
if self.serial is not None:
data_string = text + "\n"
self.serial.write(data_string.encode('utf-8'))
if self.debug: print(data_string.strip())
def finish(self):
if self.serial is not None:
self.serial.close()