Arduino YUN custom openwrt C program communication with arduino

Hello,

I have created my own custom openwrt build of Arduino YUN, and want to run my own C application on the openwrt that can communicate with the arduino side of YUN over the serial port.

My C program running on openwrt needs very fast serial communication with another program on the arduino side of the YUN, with low overhead/low latency (few micro seconds at most).

So my questions are:

  1. Should I bypass the bridge system on both openwrt side and arduino side to achive fast communcation?

  2. How can it be bypassed?

  3. If bypassed, which serial device should my C program open on the openwrt to communicate with arduino side, is that ttyUSB0?

  4. If I do not bypass bridge, how can my C program running on openwrt communicate using bridge with arduino?

thanks
rough

Did you read my reply in you other thread? How can C program running on YUN linux, can comminucate with Arduino? - Arduino Yún - Arduino Forum

kind regards,

Jos

Q:My C program running on openwrt needs very fast serial communication with another program on the arduino side of the YUN, with low overhead/low latency (few micro seconds at most).

A: The serial communication( <1Mb) is no longer count as fast communication, if you are working on PHY 100Gb then C is even too slow, The assembly language is the way to go. Plus to make C work at Yun openwrt we need either cross compile or load gcc into Yun. Last thing, python or lua use c extension to access serial port and no speed lost here.

So my questions are:

  1. Should I bypass the bridge system on both openwrt side and arduino side to achive fast communcation?

A: Yes and No, if speed is goal then bypass the bridge system.

  1. How can it be bypassed?

A: Owning /dev/ttyATH0 for use with Python (Tornado) - Arduino Yún - Arduino Forum

  1. If bypassed, which serial device should my C program open on the openwrt to communicate with arduino side, is that ttyUSB0?

A: "ttyATH0"

  1. If I do not bypass bridge, how can my C program running on openwrt communicate using bridge with arduino?

A: Wrote C application access Restful web service of bridge.

Hello Thanks for reply,

My C program opens /dev/ttyATH0 at only 300 baud, I also open serial1 on arduino at 300 baud, but communications is flaky, most of the time it doesnt work. I commented out the line in inittab on the linux side,

#ttyATH0::askfirst:/bin/ash --login

that may cause some other process opening the port and gumming up the works, but still I cant even communicate at 300 baud reliably, it fails to send and receive the bytes correctly over 90% of the time.

Does anybody have any program that can send data back and forth between a program running on YUN and the Arduino without using the internet and all that overhead which I'm not using.

Thanks

My C program opens /dev/ttyATH0 at only 300 baud

The speed is not issue here, if I were you, I will use bridge for communication. Go through IDE bridge example and search this forum.

Can I use the bridge with a local program running on the linux side of YUN that can send bytes toarduino side without using internet?

I'm using C program on the linux side,want to send data over serial to arduino side, keep getting lots of errors, wrong bytes received bytes came in out of sequence etc.

Plan A:

#include <Bridge.h>
void setup()
{
    	Bridge.begin();    
}
void loop()
{
	char lbuffer[256]; 
	Bridge.get("D12", lbuffer, 256); 
     	Bridge.put("D13", String(random(1, 100)));
	delay(1000);	
}
opkg update
opkg install php5-cli
opkg install php5-mod-sockets
opkg install php5-mod-json
nano hello.php

#!/usr/bin/php-cli
<?php
require ("/usr/lib/php/bridge/bridgeclient.class.php");
$client = new bridgeclient();
$client->put("D12","Test");
$test= $client->get("D13");
echo $test;
?>
chmod 755 hello.php

./hello.php

Plan B:

#include <Bridge.h>
void setup()
{
    	Bridge.begin();    
}
void loop()
{
	char lbuffer[256]; 
	Bridge.get("D12", lbuffer, 256); 
     	Bridge.put("D13", String(random(1, 100)));
	delay(1000);	
}
nano hello.py

#!/usr/bin/python
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
value.put("D12","Test")
print value.get('D13')
chmod 755 hello.py
./hello.py

Post your C code here?

  1. Disable redirect UART to ash shell (Bridge is disabled!)
nano /etc/inittab
#ttyATH0::askfirst:/bin/ash --login

http://forum.arduino.cc/index.php?topic=191820.msg1679588#msg1679588

  1. Make sure Yun run correct buad rate:

Upgrade firmware.

http://forum.arduino.cc/index.php?topic=191820.msg1786485#msg1786485

  1. Fix kernel command line warning error message:

http://forum.arduino.cc/index.php?topic=265031.msg1870219#msg1870219

dmesg

[   79.510000] wlan0: RX AssocResp from d8:50:e6:d7:ea:1e (capab=0xc11 status=0 aid=1)
[   79.520000] wlan0: associated
[   79.520000] ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready

anything after boot .

Arduino code:

void setup()
 
{
 
Serial.begin(9600);
Serial1.begin(9600);
 
}
 
void loop()
 
{
 
Serial.print("Hello World\n");
Serial1.print("Hello World\n");
 
delay(1000);
 
}

Install screen for testing:

opkg update
opkg install screen

screen /dev/ttyATH0 9600
Hello World
           Hello World
                      Hello World
                                 Hello World

To exit screen type “ctrl-a \” without the quotes.

root@Arduino:/mnt/sda1# nano arduino.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
 
/* Yun is on /dev/ttyATH0 */
char *portname = "/dev/ttyATH0";
char buf[256];
 
int main(int argc, char *argv[])
{
 int fd;
 
/* Open the file descriptor in non-blocking mode */
 fd = open(portname, O_RDWR | O_NOCTTY);
 
/* Set up the control structure */
 struct termios toptions;
 
 /* Get currently set options for the tty */
 tcgetattr(fd, &toptions);
 
/* Set custom options */
 
/* 9600 baud */
 cfsetispeed(&toptions, B9600);
 cfsetospeed(&toptions, B9600);
 /* 8 bits, no parity, no stop bits */
 toptions.c_cflag &= ~PARENB;
 toptions.c_cflag &= ~CSTOPB;
 toptions.c_cflag &= ~CSIZE;
 toptions.c_cflag |= CS8;
 /* no hardware flow control */
 toptions.c_cflag &= ~CRTSCTS;
 /* enable receiver, ignore status lines */
 toptions.c_cflag |= CREAD | CLOCAL;
 /* disable input/output flow control, disable restart chars */
 toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
 /* disable canonical input, disable echo,
 disable visually erase chars,
 disable terminal-generated signals */
 toptions.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 /* disable output processing */
 toptions.c_oflag &= ~OPOST;
 
/* wait for 24 characters to come in before read returns */
 toptions.c_cc[VMIN] = 12;
 /* no minimum time to wait before read returns */
 toptions.c_cc[VTIME] = 0;
 
/* commit the options */
 tcsetattr(fd, TCSANOW, &toptions);
 
/* Wait for the Arduino to reset */
 usleep(1000*1000);
 /* Flush anything already in the serial buffer */
 tcflush(fd, TCIFLUSH);
 /* read up to 128 bytes from the fd */
 int n = read(fd, buf, 128);
 
/* print how many bytes read */
 printf("%i bytes got read...\n", n);
 /* print what's in the buffer */
 printf("Buffer contains...\n%s\n", buf);
 
return 0;
}
root@Arduino:/mnt/sda1# gcc -o arduino arduino.c
root@Arduino:/mnt/sda1# ./arduino
12 bytes got read...
Buffer contains...
Hello World