-
Notifications
You must be signed in to change notification settings - Fork 22
Communication between Raspberry Pi and PC (Node.js)
In here, we use Node.js and Socket.IO to communicate between Raspberry Pi and PC. In addition, it can be used to communicate between other process in Raspberry Pi.
In this project, image processing is done by C++ and OpenCV and, Node.js server recieves the control signal from image processing. At the same time, the server can recieve the signal from other PC, so the user can stop or control the vehicle remotely.
#What the server does in this project
- Receive messages from image processing process
- Receive messages from PC
- Translate the control command to Arduino command
- Send command to Arduino with serial
- Visualize the results and control command
#Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
####1. Install nodejs, npm usually
$ sudo apt-get install -y nodejs npm
####2. Introduce n package
$ sudo npm cache clean
$ sudo npm install n -g
####3. Install node with n package
$ sudo n stable
$ sudo ln -sf /usr/local/bin/node /usr/bin/node
####4. Remove the old versions
$ sudo apt-get purge -y nodejs npm
####5. Check if successfully intsalled
$ node -v
Socket.IO enables real-time bidirectional event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.
###Install First, move to the working folder.
cd /path/to/working/directory
Then, install socket.io at the folder.
npm install socket.io
In here, we use Node.js and Socket.IO to communicate between Raspberry Pi and PC. In addition, it can be used to communicate between other process in Raspberry Pi.
In this project, image processing is done by C++ and OpenCV and, Node.js server recieves the control signal from image processing. At the same time, the server can recieve the signal from other PC, so the user can stop or control the vehicle remotely.
##What this server does
- Build a http server
- Wait connection from client
- After connection, receives an JSON message with the eventname "cmd_vel"
- Translate the cmd_vel message to Arduino command
- Send command to Arduino with serial
##Usage First, download this folder.
svn export https://github.com/mktk1117/six_wheel_robot/trunk/arduino_server
Then, you have to install socket.io and node-serialport.
cd arduino_server
npm install socket.io
npm install serialport
Run the server
node app.js
##Room Name definition
In this server, room is used to select who to send.
The name is like below.
- C++ program:
autocontrol
- web client:
remotecontrol
To join in the room, the client has to send messages of its room name.
- event name:
enter_room
- key name of the JSON data:
room
It can be sent like this.
socket.emit("enter_room", {room : "remotecontrol"});
In here, the client can be connected in the room remotecontrol
.
This event is to control the vehicle with cmd_vel message. This message contains linear velocity and angular velocity.
cmd_vel
- linear
- x
- y
- z
- angular
- x
- y
- z
It can be decoded like this.
socket.on('cmd_vel', function(data) {
var linear = data.linear;
var angular = data.angular;
console.log("linear = {" + linear.x + ", " + linear.y + "," + linear.z + "}");
console.log("angular = {" + angular.x + ", " + angular.y + "," + angular.z + "}");
});
By using this event data can be transferred to other room.
- event name:
transfer
- JSON data:
-
room
: name of the destination room -
eventname
: name of the event sent to the room -
data
: data to be sent
-