A few weeks ago I’ve bought a mbot robot out of curiosity (also as a gift), since they became available at a nearby major electronic retailer and cheaper than buying them online.
The mbot is a robot chassis with two wheels, some external and onboard sensors, including an external ultrasonic sensor, and all this supported on a custom version of Arduino 328 board whish incorporates a motor driver, battery charger and so on. The version that I’ve bought also came with a LED Matrix display where it is possible to draw faces, text and numbers (mbot Face version).
The mbot robot can be controlled or used by either some Android (and IOS) mobile applications, or by using the Scratch programming environment. MakeBlock has a specific mbot version for Scratch called mblock that supports a set of new programming blocks to control the robot. The use of Scratch and mbot makes it ideal combination for teaching kids about programming and robotics.
We caan communicate/interface with mbot either by using an USB cable, or either by Low Power Bluetooth (the mbot BLE version) or through a 2.4GHz radio (the mbot 2.4GHz version). The 2.4Ghz version is more adequate for a classroom environment, since each robot is automatically bounded by radio to the 2.4GHz USB computer stick radio controller, which basically makes it plug and go and no need to fiddle with BLE discovery and bounding.
Anyway, the version that I have is the BLE one, and this post is about how to use NodeJS with the BLE Noble Library to communicate with mbot when using the factory firmware.
To make this work we need to have some requisites first:
Since mbot uses BLE, the computer must support also BLE. In my case I’m using the CSR 4 BLE dongle available on eBay, Ali and so on, to have BLE support on my computer.
The mbot must be loaded with the factory firmware so the this code can work. This is off course just for testing this code.
The factory firmware can be loaded either by using the mblock program when connected by USB cable, or by using the Arduino IDE.
The mbot BLE module is connected to the serial pins of the onboard arduino, so while the factory firmware has a specific interface, nothing stops us from replacing it with our own code and interface. For now we just keep the factory interface that is based on messages that start with 0xFF 0x55 ….
The code was tested on Linux, and it works fine. No idea if it works on windows…
As far it goes today, the NodeJS Bleno library doesn’t work with the latest node version 10, so we need to use this with a previous version of NodeJS. I’m using NodeJS V8, and also use the NodeJS Version Manager to have several versions of NodeJS active and available.
The BLE interface:
Using the Nordic Connect mobile application, we turn on the mbot, and on the application we start the BLE scan:
A device named Makeblock_LE should appear. We can connect to it and see the published services and characteristics:
There are two known services, and two unknown services. After some testing writing data to those services the service ffe1 is the service that connects to the mbot arduino serial port, and the service ffe4 I have no idea what it is for. Probably for controlling something on the BLE module itself.
The characteristics that the service ffe1 service exposes are:
As we can see, on is for reading data: ffe2 and it supports notification. This means we are warned when data is available so we can read it. The other characteristic is ffe3 that is for writing.
Basically if we connect to the Makeblock_LE BLE device, use the ffe1 service and write on the ffe3 characteristic we can control the robot. Data from the robot is automatically sent to us if we have notifications enabled on the ffe2 characteristic.
The mbot protocol:
There is one post that explains the protocol structure to communicate with the mbot.
Basically every command begins with 0xff 0x55 and then a set of bytes to control something.
The responses follow the same principle of starting with 0xff 0x55 and can return several values types.
An easier way to see what to send is to use mblock, program a scratch example in Arduino mode, and on the mblock serial monitor see what is sent to the robot.
My GitHub code source has some command examples for sending to mbot, namely to control the WS2812 RGB leds, the buzzer, the Led Matrix and to read data from the ultrasonic sensor.
How to use it:
Download the code from here MBot_BLE.
git clone https://github.com/fcgdam/mbot_ble
Make sure that you are using NodeJS version 8:
node -v v8.11.3
If using Node V10, you can try to install the modules since in a future date from this post, the issues with Noble and NodeJS V10 might be solved.
Install the modules dependencies:
The code to access the BLE device needs root access, or check how to use Noble without root access:
sudo node mbotble.js
If the Ultrasonic distance sensor is connected to port 3, distance data is shown on the terminal.
The sample output for the mbotble.js when running as root on the RPI 3:
root@firefly:/home/pi/BLEMbot# node blembot.js - Bluetooth state change - Start scanning... ! Found device with local name: Makeblock_LE ! Mbot robot found! - Stopped scanning... - Connecting to Makeblock_LE [001010F13480] ! Connected to 001010F13480 ! mbot BLE service found! ! mbot READ BLE characteristic found. ! mbot WRITE BLE characteristic found. - End scanning BLE characteristics. ! Subscribed for mbot read notifications Reading the ultrasound sensor data... > mbot data received: "ff550002cb3db9410d0a" Distance: 23.15517234802246 Reading the ultrasound sensor data... > mbot data received: "ff5500020000bc410d0a" Distance: 23.5 Reading the ultrasound sensor data... > mbot data received: "ff5500027c1ab9410d0a" Distance: 23.13793182373047 Reading the ultrasound sensor data... > mbot data received: "ff5500028db0c0410d0a" Distance: 24.086206436157227 Reading the ultrasound sensor data... > mbot data received: "ff550002ddd398400d0a" Distance: 4.775862216949463 Reading the ultrasound sensor data... > mbot data received: "ff5500024f23d8410d0a" Distance: 27.017240524291992 ... ... ...