Disable the bridge
As mentioned in previous episodes the plan was to remove the bridge as that provided much more functionality than it needed to and it also provided root access to the Linino side from the Arduino code. Hans thought would replace it with direct serial communications between the two CPUs.
To stop the bridge /etc/inittab is edited to disable the connection of the console to the serial port ttyATH0, the Yún needs to be rebooted after this change.
::sysinit:/etc/init.d/rcS S boot ::shutdown:/etc/init.d/rcS K shutdown #Disable Bridge #ttyATH0::askfirst:/bin/ash --login
Checking we can communicate
To test the solution Hans produced a simple loopback example
Python
To get Python to listen to the serial port the PySerial module needs to be installed. That's simply a case of using the package manager.
opkg update opkg install pyserial
The port is configured as /dev/ttyATH0 and when we open the port we can specify the speed and any timeouts. For testing purposes the timeouts were set to 60s each and the test programme simply echoed the inbound stream back down the line.
import serial try: ser = serial.Serial('/dev/ttyATH0', 115200, timeout=60, writeTimeout=60) # Baudrate needs to be mirrored on ATMega side except: exit(1) # quit as we can't start the serial port line = ser.readline() # read a '\n' terminated line while line != "": print(line); # Display on console ser.write(line) # Echo it back line = ser.readline() # read next line print("timeout") ser.close()
Arduino
On the Arduino side the serial port is configured as Serial1 and the code to communicate over serial is very straight forward we can echo the message to and from the PC across to the Linino.
void setup() { Serial.begin(115200); //To PC Serial1.begin(115200); //To Linino } void loop() { //From PC to Linino int c = Serial.read(); // read from USB-CDC if (c != -1) { Serial1.write(c); // write char to UART } //From Linino to PC c = Serial1.read(); // read from UART if (c != -1) { // got anything? Serial.write(c); // write to USB-CDC } }
Test results
The Python script was started up manually using SSH so that the output could be monitored. A sequence of messages were sent from the PC down the USB, over the bridge and then bounced back again.
Give that the test was successful the next step is to add this functionality into our existing scripts and configure the inittab to run our weather script.
Code at: https://github.com/Workshopshed/EnchantedObjects/tree/master/Code/Examples/SerialEcho
Next: A bit of 3D Printing
Reference
Welcome to pySerial’s documentation — pySerial 2.7 documentation