Arduino Yun – TCP/IP to Serial1 redirect

!! Code available on my GitHub toskyRocker account !!

Many of us, Arduino lovers, want to get rid of the Bridge library, due to its lame performance, it’s slow and heavy.
A simple way to avoid this problem is to replace the Bridge library (Atmega side) with a python script (Atheros side) that redirects the TCP/IP stream to the Serial1 .

1. First of all replace inittab (using copy over SSH) on OpenWrt with the one given in my GitHub account.
[sourcecode language=”bash” wraplines=”false” toolbar=”true” collapse=”false”]>>scp inittab root@192.168.240.1:/etc/[/sourcecode]
This step makes Serial1 available on startup.
NOTE: Arduino Yun’s IP is usually 192.168.240.1

2. Copy tcp_serial_redirect.py in the right place on OpenWrt.
[sourcecode language=”bash” wraplines=”false” toolbar=”true” collapse=”false”]>>scp tcp_serial_redirect.py root@192.168.240.1:/root/[/sourcecode]

3. Copy pyserial-2.7 folder on OpenWrt
[sourcecode language=”bash” wraplines=”false” toolbar=”true” collapse=”false”]>>scp -r pyserial-2.7/ root@192.168.240.1:/root/[/sourcecode]

4. Install pyserial on OpenWrt[sourcecode language=”bash” wraplines=”false” toolbar=”true” collapse=”false”]
>>ssh root@192.168.240.1
>>cd pyserial-2.7
>>python setup.py install
[/sourcecode]

5. In order to enable the redirect type this command in the OpenWrt terminal:
[sourcecode language=”bash” wraplines=”false” toolbar=”true” collapse=”false”]
>>python tcp_serial_redirect.py -p /dev/ttyATH0 -b 115200 -P 8888
[/sourcecode]
115200 is the baudrate selected.
8888 the port for the connection.

6. You’re ready to use the serial1 communication on Atmega32u4. You can write something like this to test your connection:
[sourcecode language=”cpp” wraplines=”false” toolbar=”true” collapse=”false”]
void setup() {
Serial1.begin(115200);
}

void loop() {
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.print(inByte, BYTE);
}
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.print(inByte, BYTE);
}
}
[/sourcecode]

Don’t forget to use the same baudrate for both the python script and Arduino sketch

Comments (5)

  1. Todd

    Dec 16, 2015 at 8:58 pm

    Very nice, this fixed my stability problem trying to use Console.
    I considered doing a C version, clever to use pyserial.

  2. Todd

    Dec 17, 2015 at 9:05 pm

    So I am a fan, but there are some issues – rebooting the Yun is a real
    issue, as the Atmega application I am running spews data into the USB console. I can only control a bit by unplugging my sampling device, wait for the YUN to connect/network, then unblock the atmega app at startup.

    while(Serial1!); is not enough!

    any other workarounds?

    • AndrewTosky

      Dec 17, 2015 at 11:16 pm

      Thank you for appreciating my work 🙂

      Do you need the script running on Yun startup?

  3. Eduardo

    Sep 19, 2017 at 1:32 pm

    Interesting post, I will test with project because I have strange problems with bridge library.

  4. Fred Briggs

    Nov 28, 2017 at 11:19 pm

    Very interesting, I have been looking for a way to bypass bridge because it is to slow. I am trying to write to the SD card at 200Hz and with bridge I only get 40Hz. How would I use this in that environment? Sorry but I am new to python and Linux so I am just trying to learn.

Leave a Reply to Todd

This site uses Akismet to reduce spam. Learn how your comment data is processed.