Bridge is slow, speed boost not 21,950% but 1,000,000% from Bridge.

Far better way:

Speed boost 1,000,000% from Bridge

Linux Industrial I/O Subsystem

Install software ser2net ( C programming based):

opkg update
opkg install ser2net
opkg install coreutils-stty

Disable serial port:

nano /etc/inittab
#ttyATH0::askfirst:/bin/ash --login

Restart Yun

Setup ser2net:

nano /etc/init.d/ser2net
#!/bin/sh /etc/rc.common
# Ser2Net Init Script
START=10
STOP=15
start() {
        /usr/bin/stty -F /dev/ttyATH0 raw -clocal -echo 115200
        ser2net -C "6001:raw:600:/dev/ttyATH0:115200"
}
stop(){
        pid=`ps w | grep "ser2net"  | grep -v grep |awk '{print $1}'`
        kill -9 $pid
}
chmod 755 /etc/init.d/ser2net

/etc/init.d/ser2net enable

/etc/init.d/ser2net start

ATmega32u4 code:

void setup() {
  Serial1.begin(115200);
}
void loop() {
  while (true) {
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (Serial1.read() != -1);
        break;
      }
    }
    delay(10);
  }
  for ( unsigned long  i = 0; i <= 4294967295; i++) {
    Serial1.print(i);
    Serial1.print(',');
    Serial1.println(random(1023));
    Serial1.flush();
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '^') {                 //   '^' key pressed for stop?
        break;
      }
    }
    delayMicroseconds(100);
  }
}

Testing:

telnet localhost 6001
0,897
1,40
2,630
...
30733,366
30734,389
30735,734

at 35 seconds, 878 trans/s. speed boost 21950%

Make above solution internet enable:

at firewall open tcp port 6001.

telnet 192.168.0.104 6001

192.168.0.104 is Yun 's IP address.

Save telnet console output into file:

mv /usr/bin/tee /usr/bin/tee.bk
opkg update
opkg  install coreutils-tee
telnet localhost 6001 | tee -a yun.txt

Save file into RAM disk for high speed:

telnet localhost 6001 | tee -a /tmp/yun.txt

Very interesting! Thank you for posting this alternative.

But I'm not sure where you get the 4 transactions per second? There may be things you can do with the Bridge (like synchronously run a long Process) that might be that slow, but the code you posted is essentially a replacement for the Console class, and I've never seen it be that slow. I've slightly modified your code to use the Console class (added Bridge.begin() and Console.begin() instead of Serial1.begin()) and changed all other instances of Serial1 to Console. When I run that code, I get 34 lines per second.

Yes, 34 lines per second is slower than 878 lines per second, but we can't make a direct comparison without repeating your test above on my setup. This is necessary because there are other variables at play besides the raw serial port speed, for example, the SSH output is going over our local networks and being read and displayed by our SSH clients - your network and client might be running at a very different speed than mine, and may have very different network loading.

So to have a meaningful comparison, I tried running your example, and I got hung up on the first step:

root@Yun4:~# opkg update
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.gz.
Updated list of available packages in /var/opkg-lists/attitude_adjustment.
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.sig.
Signature check passed.
root@Yun4:~# opkg install ser2net
Installing ser2net (2.7-2) to root...
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk.
Collected errors:
 * opkg_download: Failed to download http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk: Error.
 * opkg_install_pkg: Failed to download ser2net. Perhaps you need to run 'opkg update'?
 * opkg_install_cmd: Cannot install package ser2net.
root@Yun4:~#

Being unable to duplicate your test, we can't draw any valid conclusions. I'm willing to concede that the Bridge Console is going to be slower than ser2net, but I contend that the difference is not nearly as dramatic as you state.

Before trying your method, I think people should be able to answer YES to these three questions:

  • Do you really need the extra speed? (I'll bet most applications don't.)
  • Are you willing to give up all other Bridge capabilities to get a faster console?
  • Are you willing to disable your only Linux console port that would let you log in and fix problems if you run into Linux issues that prevent network access using SSH?

This is the sketch I used to test the Console speed:

#include <Bridge.h>
#include <Console.h>

void setup() {
  Bridge.begin();
  Console.begin();
}

void loop() {
  while (true) {
    if (Console.available()) {
      int c = Console.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (Console.read() != -1);
        break;
      }
    }
    delay(10);
  }
  for ( unsigned long  i = 0; i <= 4294967295; i++) {
    Console.print(i);
    Console.print(',');
    Console.println(random(1023));
    Console.flush();
    if (Console.available()) {
      int c = Console.read();
      if (c == '^') {                 //   '^' key pressed for stop?
        break;
      }
    }
    delayMicroseconds(100);
  }
}

And this is how I accessed it:

ssh root@ yourYunsName.local 'telnet localhost 6571'

There are no Linux changes required. Can you run that on your setup and post the speed result? That will be the only meaningful comparison.

root@Yun4:~# opkg update
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.gz.
Updated list of available packages in /var/opkg-lists/attitude_adjustment.
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.sig.
Signature check passed.
root@Yun4:~# opkg install ser2net
Installing ser2net (2.7-2) to root...
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk.
Collected errors:

wget http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk
opkg install ser2net_2.7-2_ar71xx.ipk

That works. I get 841 lines per second using ser2net, which corresponds reasonably well with your findings. Almost 24 times faster than using the Console class.

I tried one more test: using a YunServer/YunClient in the sketch, in conjunction with the Bridge. This allows an external Telnet client to connect directly with the Yun to transfer data, eliminating the need to SSH into the Yun, and then run a local Telnet client (it removes a little bit of the overhead. As expected, it is faster than using the Console class (and also more complicated) but not nearly as fast as ser2net.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server(255);
YunClient client;

void setup() {
  Bridge.begin();
   server.noListenOnLocalhost();
   server.begin();
   client = server.accept();
}

void loop() {
  while (client.connected()) {
    if (client.available()) {
      int c = client.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (client.read() != -1);
        break;
      }
    }
    delay(10);
  }
  
  if (client.connected()) {
    for ( unsigned long  i = 0; i <= 4294967295; i++) {
      client.print(i);
      client.print(',');
      client.println(random(1023));
      client.flush();
      if (client.available()) {
        int c = client.read();
        if (c == '^') {                 //   '^' key pressed for stop?
          break;
        }
      }
      delayMicroseconds(100);
    }
  }
  else
    client = server.accept();
}
[b]Method   Lines/sec   Improvement[/b]
Console       34             0%
YunClient     36             6%
ser2Net      841         2,374%

Console Class:
Pros:

  • Easy
  • Allows simultaneous use of other Bridge class functions
  • No changes to Linux
  • Keeps console TTY active for emergency use using YunSerialTerminal sketch
    Cons:
  • Slow (but may be fast enough for many applications, especially those using the Console as debug output.)

YunClient Class:
Pros:

  • Flexible, allows direct connection by any network socket
  • Allows simultaneous use of other Bridge class functions
  • No changes to Linux
  • Keeps console TTY active for emergency use using YunSerialTerminal sketch
    Cons:
  • Slow (but may be fast enough for many applications)
  • More complicated than Console class

ser2net:
Pros:- SPEED!!! (but not all applications need that speed.)

  • Simple (regular old serial port techniques in sketch.)Cons:
  • Additional packages to load into Linux
  • Requires Linux configuration files
  • Disables console TTY preventing emergency use using YunSerialTerminal sketch

This looks like a great alternative if you need raw unadulterated speed over the MCU/Linux serial port, and you don't need any other functions over that port.

Thanks SonnyYu.
On my todo list.
Thanks again
Jesse

sonnyyu:
ATmega32u4 code:

I've only noticed this Thread today.

The code in Reply #1 looks very like the code you would use if you were communicating between a Leonardo and a PC (except that then you would use Serial).

Has one our experts come to the conclusion that the Yun works well as a "normal" Linux computer? :slight_smile:

...R

Robin2:
The code in Reply #1 looks very like the code you would use if you were communicating between a Leonardo and a PC (except that then you would use Serial).

Yes, it looks like a way to take code that uses a serial port to talk to a remote computer, and change it so that it uses the network to talk to that remote computer. No changes to the sketch code, other than perhaps substituting Serial1 for Serial, but of course it takes changes to the PC code to use a network socket instead of a serial port.

Has one our experts come to the conclusion that the Yun works well as a "normal" Linux computer? :slight_smile:

Actually, it looks like quite the opposite to me. The Linux side is used to translate the serial port to a network socket server, to simplify access from a remote computer. In essence, the Linux side is pretty much bypassed and turned into a dumb communications server.

I suppose one could access the network socket from the Linux side by opening a port to localhost, and that would allow communications with code on the Linux side, but why would you do that? It would be much simpler and lower overhead to just have the Linux code use /dev/ttyATH0 to talk to the sketch directly.

Your goal of having someone turn a Yun into an RPi remains unfulfilled... :wink:

ShapeShifter:
Actually, it looks like quite the opposite to me. The Linux side is used to translate the serial port to a network socket server,

I guess that floated high over my head :slight_smile:

...R

ShapeShifter:
...
Cons:

  • Additional packages to load into Linux
  • Requires Linux configuration files
  • Disables console TTY preventing emergency use using YunSerialTerminal sketch

...

Disables console TTY preventing emergency use using YunSerialTerminal sketch

sonnyyu:
Not better solution but work around.

Rootfs on External Storage microSD card (extroot)

[OpenWrt Wiki] Extroot configuration

How to import new modules to Python ? - #6 by sonnyyu - Arduino Yún - Arduino Forum

Edit /etc/inittab at Rootfs on microSD card, comment out or delete:

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

Whenever you need access serial port again, just remove microSD card and reboot.

Whenever you need access serial port again, just remove microSD card and reboot.

ShapeShifter:
...
Before trying your method, I think people should be able to answer YES to these three questions:

  • Do you really need the extra speed? (I'll bet most applications don't.)
  • Are you willing to give up all other Bridge capabilities to get a faster console?
  • Are you willing to disable your only Linux console port that would let you log in and fix problems if you run into Linux issues that prevent network access using SSH?

...

Do you really need the extra speed? (I'll bet most applications don't.)

The race car could go up to 300 mph, same time it could run at 50 mph. make Toyota Corolla run at 200 mph?
I will start new thread about how to speed boost 1,000,000% from Bridge soon.

sonnyyu:
make Toyota Corolla run at 200 mph?

If you figured out how to make it go that fast, I wouldn't want to ride in it! :o

But at 50 mph that stock Corolla has many advantages over a race car that's also doing 50, as it's much more comfortable with a softer suspension, air conditioning/heating, nice sound system, adjustable seats, and many other refinements.

The metaphor stands up well, because while you could probably get that serial port to do all the things that the Bridge can do at once (run processes, read/write files, REST API, get/put data store, mailbox, etc.) it will take a lot more work to do so - if you don't need the speed, the Bridge is a lot easier (more comfortable.) :wink:

Don't get me wrong, I think figuring ser2net out is a great contribution to the Arduino community, I applaud you for that. With the right need and application, I'm sure it will help people out, I just hope people realize it's not a universal Bridge replacement for all applications (and yes, I don't think you meant it that way and I'm not trying to imply that you did.)

sonnyyu:
ATmega32u4 code:

void setup() {

Serial1.begin(115200);
}
void loop() {
  while (true) {
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '~') {                //    Tilde '~' key pressed  for start sampling?
        while (Serial1.read() != -1);
        break;
      }
    }
    delay(10);
  }
  for ( unsigned long  i = 0; i <= 4294967295; i++) {
    Serial1.print(i);
    Serial1.print(',');
    Serial1.println(random(1023));
    Serial1.flush();
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '^') {                //  '^' key pressed for stop?
        break;
      }
    }
    delayMicroseconds(100);
  }
}

ser2net seems to be much faster than client & console.

I wanna know how to write the arduino code in ser2net style to achieve REST API functions?

tim9510019:
ser2net seems to be much faster than client & console.

Yes, on the positive side, it is much faster than the Bridge Library's Console class.

But on the negative side, it prevents using any of the Bridge Library's functions. While the Bridge Library is not a speed demon, it can do many different things, and do them all at once. ser2net is very fast, but it's a one trick pony - it replaces the Console class and nothing else.

I wanna know how to write the arduino code in ser2net style to achieve REST API functions?

I'm not sure it's possible, that's not what ser2net was designed to do. Using ser2net means giving up the Bridge Library, which is what makes implementing a REST API in a sketch so easy. It makes it easy because there is code running on the Linux side which intercepts any requests to its web server that start with the token /arduino/. It takes such requests, and uses the Bridge Library to pass them to a YunServer object in the sketch, which creates a YunClient object to handle the actual request. At this point, all of the overhead of the request has already been handled, and just the remaining part of the URL (the part after /arduino/) is passed to the sketch. The sketch sends back a response through the YunClient object, and the Bridge Library and the code on the Linux side takes that response and adds in all of the overhead necessary to format it as a proper web request response.

If you want to do the same thing in a sketch while using ser2net, you will have to do all of that overhead processing that is currently being done on the Linux side - basically, you will need to write a web server inside of your sketch. Definitely a daunting task, with no assurance that it will actually work. I've written mini web servers before to handle specialized requests, and I don't think I would want to try it given the limited code space and RAM that is in the '32U4 processor that runs the sketch.

The other option is to implement your REST API on the Linux side, which eliminates the speed issues on the serial port between the two processors. There are many ways to do this, but I've had good luck using the Bottle Framework with some Python code. Of course, if the REST API needs information from the sketch (or sends information to the sketch) then you will have to have some way to do that - I typically use a Bridge Library Process object to do that, but one could use Bridge.put() or Bridge.get() or some other method. However, if you are using ser2net, you can't really use any of those other methods, and you may be in trouble.

ser2net is a wonderful replacement for the Console class, but it comes with some serious downsides. If all you need is a fast Console replacement, this is your ticket. But if you want to implement a REST API, I think it will only cause you heartache.

ShapeShifter, thanks for your reply.

Since using ser2net style to achieve REST API functions is really painful, one question came to my mind.

Does it exist another way to control arduino pins on the website with ser2net code?

tim9510019:
Does it exist another way to control arduino pins on the website with ser2net code?

On what web site? One hosted by the Yun? If so, I simply don't see where ser2net would buy you anything at all. ser2net allows a remote computer to talk serial communications with the sketch over the network. I don't see where it has anything interesting to offer to you, unless the web site is hosted by another computer, and that computer is fielding the REST API and turning it into serial commands that are sent to the sketch over ser2net. You would be pretty much bypassing the Linux side of the Yun, which is an unfortunate thought since that is what gives the Yun so much power.

Rather than being hung up on ser2net and trying to figure out how to make it do something that it was not intended to do, you would be better off describing what you are actually trying to do. So far, all I can guess is that you want to control some pins with a website, and you are concerned about speed. But that's not nearly enough to go on to make and recommendations. What is hosting the web pages - the Yun? How complicated are the pages? What kind of response time do you need? How often are you doing updates? What kind of data rate are you expecting? What are you trying to control? Information like that is needed.

I doubt ser2net is going to be your ultimate solution, so you are probably better off starting a new thread explaining just what you are trying to do.

sonnyyu:
Save telnet console output into file:

mv /usr/bin/tee /usr/bin/tee.bk

opkg update
opkg  install coreutils-tee






telnet localhost 6001 | tee -a yun.txt




Save file into RAM disk for high speed:



telnet localhost 6001 | tee -a /tmp/yun.txt

Hi,

Is there any way to run telnet localhost 6001 | tee -a yun.txt at linux startup and keep running?

I tried to put it in /etc/rc.local but it seems not working.

Thanks