serial communication between arduino ide and MRL

HI!

i am testing to send a simple script from the arduino to start serial communication.

on the arduino

 
void setup() {
  
  Serial.begin(9600);       // use the serial port
}
 
void loop() {
   Serial.println(1);
   delay(5000);
   Serial.println(2);
   delay(5000);
   Serial.println(3);
   delay(5000);  // delay to avoid overloading the serial port buffer
}
 
serial monitor arduino ide showing
 
1
2
3
 
serial monitor on MRL shows
 
000 224 224 000 000 224 000 224 000 000 224 224 000 000 224 000 
224 000 000 224 224 000 000 224 000 000 000 000 224 224 000 000 
224 000 224 000 000 224 224 000 000 224 000 224 000 000 224 224 
000 000 224 000 000 000 000 224 224 000 000 224 000 224 000 000 
224 224 000 000 224 000 224 000 000 224 224 000 000 224 000 000 
000 000 224 224 000 000 224 000 224 000 000 224 224 000 000 224 
000 224 000 000 224 224 000 000 224 000 000 000 000 224 224 000 
000 224 000 224 000 000 224 224 000 000 224 000 224 000 000 224 
224 000 000 224 000 000 000 000 224 224 000 000 224 000 224 000 
000 224 224 000 000 224 000 224 000 000 224 224 000 000 224 000 
000 000 000 224 224 000 000 224 000 224 000 000 224 224 000 000 
224 000 224 000 000 224 224 000 000 224 000 000 000 000 224 224 
000 000 224 000 224 000 000 224 224 000 000 224 000 224 000 000 
224 224 000 000 224 000 000 000 000 224 224 000 000 224 000 224 
000 000 224 224 000 000 224 000 224 000 000 224 224 000 000 224 
 
i would like to make this useful like a in a if statement or something
 
how should the programming be on the arduino and in MRL?
Mats's picture

Hi Test

Hi 

Test with Serial.begin(57600);

 

 

 

Markus's picture

thanks i now have 123 in MRL

thanks i now have 123 in MRL allso

 

Markus's picture

now to make a if statement

now to make a if statement

GroG's picture

Looks a little buggy @ 9600 -

Looks a little buggy @ 9600 - I see it working at 57600

 

Python Script - careful of the indentation

serial = Runtime.start('serial','Serial')
serial.connect('COM9')
python.subscribe('serial','publishRX')
def publishRX(data):
       print(chr(data))
       #print(data)
 
Arduino sketch
 
void setup() {
  
  Serial.begin(57600);       // use the serial port
}
 
void loop() {
   Serial.println(1);
   delay(5000);
   Serial.println(2);
   delay(5000);
   Serial.println(3);
   delay(5000);  // delay to avoid overloading the serial port buffer
}
 

You'll see extra carriage return line feeds in the MRL display, because it automatically adds them and the skech is putting them in too with the println

GroG's picture

Ok, I put it on github

Ok, I put it on github because Alessandro (rightly so) - told me that its the right way (and deleted our code in comments)

I tested this in 119 - I had 2 Arduinos connected and it did not throw any errors.  I think it should work for you.

Let us know how it goes:


#file : /home/Markus/Skin.py edit raw
from time import sleep
from org.myrobotlab.service import Speech
from org.myrobotlab.framework import MRLListener
  
  
# this subscribe is easy shorthand method
# Name it "speech".
speech = Runtime.create("speech","Speech")
speech.startService()
  
speech.setGoogleURI("http://thehackettfamily.org/Voice_api/api2.php?voice=Laura&txt=") 
  
serial1 = Runtime.start('serial1','Serial')
serial2 = Runtime.start('serial2','Serial')
speech.speak("hello")
  
serial1.connect('COM8')
serial2.connect('COM6')
 
# python.subscribe('serial1','publishRX')
# python.subscribe('serial2','publishRX')
# this subscribe with 4 parameters has all details - subscribe to and callback info
# we subscribe to one service's topic to one method and the other to a different method
 
# this is how its done in latest mrl - nice no ?
# python.subscribe('serial1','publishRX', python.getName(), 'serial1RX')
# python.subscribe('serial2','publishRX', python.getName(), 'serial2RX')
 
# this is how its done in 119
listener1 = MRLListener('publishRX', 'python', 'serial1RX', None)
serial1.addListener(listener1)
listener2 = MRLListener('publishRX', 'python', 'serial2RX', None)
serial2.addListener(listener2)
  
#  i want this to be the data from serial1
  
def serial1RX(data):
    print(data)
    num = data
    #num = chr(data)
    #print(num)
    if (num == 1):
       speech.speak("1")
    if (num == 2):
       speech.speak("2")
    if (num == 3):
       speech.speak("3")
    if (num == 4):
       speech.speak("4")         
    if (num == 5):
       speech.speak("5")
    if (num == 6):
       speech.speak("6")
    if (num == 7):
       speech.speak("7")
    if (num == 8):
       speech.speak("8")
    if (num == 9):
       speech.speak("9")
    if (num == 10):
       speech.speak("10")
    if (num == 11):
       speech.speak("11")
    if (num == 12):
       speech.speak("12")
  
#  and this to be the data from serial2
  
def serial2RX(data):
    print(data)
    num = data
    #num = chr(data)
    #print(num)
    if (num == 1):
       speech.speak("1")
    if (num == 2):
       speech.speak("2")
    if (num == 3):
       speech.speak("3")
    if (num == 4):
       speech.speak("4")         
    if (num == 5):
       speech.speak("5")
    if (num == 6):
       speech.speak("6")
    if (num == 7):
       speech.speak("7")
    if (num == 8):
       speech.speak("8")
    if (num == 9):
       speech.speak("9")
    if (num == 10):
       speech.speak("10")
    if (num == 11):
       speech.speak("11")
    if (num == 12):
       speech.speak("12")