Arduino Communication

RPi communication with Arduino is modulated in an Arduino class. The communication is established using the serial library, by specifying the baud rate and ACM file defined in the parameters in serial.Serial(). Upon a successful connection between the Arduino board and the RPi, the read and write operation can be performed using serial.readline() and serial.write().

Functions Defined


connect():

connect the Arduino board to the RPi through SER_PORT0, which is defined in “/dev/ttyACM0”. If failed another connection attempt will be made through SER_PORT1, which is defined in “/dev/ttyACM1”. “Connect to Arduino 0/1 successfully.” will be printed for a successful connection. An error message will prompt for if both connection attempts failed

readThread(pc, android):

read messages from the Arduino using serial.readline(). If messages are received, the following processing will carry out:

  1. split the message string by newline.
  2. if the message starts with 65, remove the header 68 and write the message to Android by calling android.write().
  3. if the message starts with 80, remove the header 80 and write the message to PC by calling pc.write().
write(message):

write the message to Arduino by calling serial.write()

 

Code Example


import serial
import threading
import time


class ArduinoV3:
    def __init__(self):
        self.baudrate = 115200
        self.serial = 0
        self.connected = False

    def connect(self):
        try:
            self.serial = serial.Serial("/dev/ttyACM0", self.baudrate, write_timeout=0)
            print("Connected to Arduino 0 successfully.")
            self.connected = True
            return 1
        except:
            try:
                self.serial = serial.Serial("/dev/ttyACM1", self.baudrate, write_timeout=0)
                print("Connected to Arduino 1 successfully.")
                self.connected = True
                return 1
            except Exception as e2:
                print("Failed to connect to Arduino: %s" % str(e2))
                self.connected = False
                return 0

    def readThread(self, pc, android):
        while True:
            try:
                message = self.serial.readline()
                print("Read from Arduino: %s" % str(message))

                if len(message) <= 1:
                    continue

                if (message[0] == 80):
                    pc.write(message[1:])
                    continue

                if (message[0] == 68):
                    android.write(message[1:])
                    continue
            except Exception as e:
                print("Failed to read from Arduino: %s" % str(e))
                self.connected = False
                return

    def write(self, message):
        try:
            self.serial.write(message)
            print("Write to Arduino: %s" % str(message))
            print()
        except Exception as e:
            print("Failed to write to Arduino: %s" % str(e))