Sending data to mBot via Bluetooth


#1

Hi all,

I’m trying to send some data to my mBot (Bluetooth version) via Bluetooth (from an Android app). They pair.

I tried to use the code showed here (http://learn.makeblock.com/me-bluetooth-moduledual-mode/) but it simply doesn’t work as I don’t have the RJ45 version of the Bluetooth module. Which API/library I need in order to use the built-in Bluetooth module?

How can I achieve my goal?

Thanks.


Connecting Lego EV3 to mBot via Bluetooth
#2

You need to enable the “Communication” extension in mBlock. After that you find some new commands in the Robot section.
I already tried to send data from the mBot to an Android phone this way, that worked. I did not use the other direction yet, please tell me if it works too.


#3

Hi Andreas,

thank you for your kind support.

I used this code:

[code]#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

MeDCMotor motor_9(9);
MeDCMotor motor_10(10);

void move(int direction, int speed)
{
int leftSpeed = 0;
int rightSpeed = 0;
if(direction == 1){
leftSpeed = speed;
rightSpeed = speed;
}else if(direction == 2){
leftSpeed = -speed;
rightSpeed = -speed;
}else if(direction == 3){
leftSpeed = -speed;
rightSpeed = speed;
}else if(direction == 4){
leftSpeed = speed;
rightSpeed = -speed;
}
motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
}

double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
MeSerial se;

void setup(){
Serial.begin(9600);
if(se.available()){
Serial.println(“hello”);
}

}

void loop(){

_loop();

}

void _delay(float seconds){
long endTime = millis() + seconds * 1000;
while(millis() < endTime)_loop();
}

void _loop(){

}
[/code]

But, if I try to send something from my Android app (a simple one made with MIT App Inventor) to mBot, nothing happens.

Any other suggestions?

Thank you!


#4

Ok I tried that. I think an issue with your code is the baud rate - it might be that 115200 works.

I also could not get the mBot to receive strings using Scratch, however single bytes worked.

So the code below receives one byte per second and displays its value in decimal on the LED matrix.
Right LED turns yellow when data is available, blue otherwise.
Left LED just blinks to indicate the program is running.
The mBot sends the timer as a string each second through serial.

Please note that the mBot will use thatever is connected to serial … that can be something on USB or bluetooth (I used the usual Bluetooth module, not the RJ25 version)

I used this as MIT App Inventor Code on the Android:
BTArduinoClient2Devices_ahu.zip (3.1 KB)

You will need to pair the mBot with the Android device using the Bluetooth settings 1st, then connect within the app.

Funny thing is that when you send a string, the mBot side will pull them one by one and display the numeric byte value, each for a second.

I also attached the mBot code.bluetest_arduino.sb2 (76.1 KB)

I noticed that this does not seem to work in online mode, only in Arduino mode.


Sending bluetooth serial data to mbot
#5

Hi Andreas,

again, thank you for your support. I tried your code (both sides) and it works.

Now I just want to show something on the serial monitor and my code is:

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

#include <MeMCore.h>

MeDCMotor motor_9(9);
MeDCMotor motor_10(10);		

void move(int direction, int speed)
{
      int leftSpeed = 0;
      int rightSpeed = 0;
      if(direction == 1){
        	leftSpeed = speed;
        	rightSpeed = speed;
      }else if(direction == 2){
        	leftSpeed = -speed;
        	rightSpeed = -speed;
      }else if(direction == 3){
        	leftSpeed = -speed;
        	rightSpeed = speed;
      }else if(direction == 4){
        	leftSpeed = speed;
        	rightSpeed = -speed;
      }
      motor_9.run((9)==M1?-(leftSpeed):(leftSpeed));
      motor_10.run((10)==M1?-(rightSpeed):(rightSpeed));
}
				
double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
double currentTime = 0;
double lastTime = 0;

double getLastTime(){
    	return currentTime = millis()/1000.0 - lastTime;
}

char receivedChar;
boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvOneChar();
    showNewData();
    _delay(1.0);
}

void recvOneChar() {
    if (Serial.available() > 0) {
        receivedChar = Serial.read();
        newData = true;
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChar);
        newData = false;
    }
}

void _delay(float seconds){
    long endTime = millis() + seconds * 1000;
    while(millis() < endTime)_loop();
}

void _loop(){
    
}

But it seems that Serial.available() is never > 0 and I never print what mBot receives. I’m using your Android app and both 9600/115200 bauds.

What do you think about it?

Thanks again!


#6

Maybe it does not work because you try to put the received data into a char instead of a number?


#7

Hi, well, documentation say you have to write the code according to your brand Mbot core (mCore, Auriga, Orion, MegaPi, …).
Some command may be different according to your brand Mbot code.
I can tell you about my tests:
I use mCore and I use this code (I use serial port end bluetooth, but you ca use the serial one only):

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
  bluetooth.begin(115200);    //The factory default baud rate is 115200
  Serial.println("Bluetooth Start!");
}...

if ( Serial.available()) {
String serialResponse = Serial.readStringUntil(’\r\n’);

Serial.println("Read: " + serialResponse);

I use: 115200 for serial port, Serial.available() like you and Serial.readStringUntil(’\r\n’) to read data.
I hope it will be usefull.


#8

Could this be used to send data between mbots or between other makeblock systems that have the Bluetooth module?


#9