Mittwoch, 19. Februar 2014

Using the 'Console' to talk to the Yún MCU from Python

Playing around with the Yún led me to the problem that I had a Sketch up and running on the MCU that could be controlled from the Arduino serial console. To move the commanding part to a python script all I had to do was to do this communication from the Linux side of the Yún-Bridge system.

To initialize the Bridge all you have to do is call the Bridge.begin(); function in the setup part of the sketch. After that we initialize the Console with Console.begin(). To stop the execution of further commands we can poll the Console to become ready.
void setup() {
  // put your setup code here, to run once:
  Bridge.begin();
  Console.begin();
  while (!Console) {
  ;
  }
}
You can now use the print functions as usual:
  if(Console.available()) {
   c = Console.read();
   .
   .
   .
  }
On the Linux side we have a python script that will conncet to the bridge server and do the same as the Arduino serial console.
import telnetlib, time

tn = telnetlib.Telnet("localhost", 6571)

while True:
  print "w"
  tn.write("test")
  print tn.read_eager()
  time.sleep(1)