There are many ways to get the Raspberry Pi's to talk to each other. Since you say each of them have a WiFi dongle, I assume you want to get them to communicate over the WiFi network. Even after making that assumption, there are still many ways to skin the cat, e.g. Use HTTP or MQTT, etc.
A lot of beginners find using Javascript and NodeJS easy. So if you are familiar with JavaScript, you could very easily host NodeJS HTTP servers on all your Raspberry Pis and have one master server control all of them over a simple RESTful interface. Every Pi has a unique IP address that can be used by the master server. Also if you prefer to use MQTT, you can still use NodeJS and modify the logic to use that instead of HTTP. Here is the NPM module for MQTT: https://www.npmjs.com/package/mqtt. But for now I am assuming you are okay with HTTP and my code examples below are for running it over HTTP:
Here is some example code for each of the slaves:
var express = require('express');
var app = express();
var start = function start() {
console.log("process start...this is run within the event loop so try to make it snappy");
};
var stop = function stop() {
console.log("process stop...this is run within the event loop so try to make it snappy");
};
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.get('/start', function (req, res) {
start();
res.send('Start!');
});
app.get('/stop', function(req, res) {
stop();
res.send('Stop!');
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
With the above code, you have a HTTP server listening on port 3000 and provides support for GET requests on 3 paths: '/', '/start', '/stop'.
Here is an example of master that you can use (you can modify this to connect to different IP addresses by simple code modifications). In the example below, the user has a command line interface to type "start", "stop", "home" and "exit" commands, and each of those result in a HTTP GET request to the server at IP address 10.0.1.5:
const request = require('request');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var pendingRequests = [];
rl.on('line', function(cmd) {
switch (cmd) {
case "start":
handleStart();
break;
case "stop":
handleStop();
break;
case "home":
handleHome();
break;
case "exit":
handleExit();
break;
default:
console.log("unknown command: " + cmd);
break;
};
});
const handleExit = function handleExit() {
var r;
//abort all pending requests
while(pendingRequests.length) {
r = pendingRequests.pop();
r.abort();
}
rl.close();
};
const handleHome = function handleHome() {
var r = request('http://10.0.1.5:3000', function (error, response, body) {
var index = pendingRequests.indexOf(r);
if(index < 0) {
console.log("Fatal Error: request not found in pendingRequests Array"); //this should never happen
process.exit(1);
} else {
pendingRequests.splice(index, 1);
}
if (error) {
console.log(error);
}
if (!error && response.statusCode == 200) {
console.log(body) // Print the response
}
});
pendingRequests.push(r);
};
const handleStart = function handleStart() {
var r = request('http://10.0.1.5:3000/start', function (error, response, body) {
var index = pendingRequests.indexOf(r);
if(index < 0) {
console.log("Fatal Error: request not found in pendingRequests Array"); //this should never happen
process.exit(1);
} else {
pendingRequests.splice(index, 1);
}
if (error) {
console.log(error);
}
if (!error && response.statusCode == 200) {
console.log(body) // Print the response
}
});
pendingRequests.push(r);
};
const handleStop = function handleStop() {
var r = request('http://10.0.1.5:3000/stop', function (error, response, body) {
var index = pendingRequests.indexOf(r);
if(index < 0) {
console.log("Fatal Error: request not found in pendingRequests Array"); //this should never happen
process.exit(1);
} else {
pendingRequests.splice(index, 1);
}
if (error) {
console.log(error);
}
if (!error && response.statusCode == 200) {
console.log(body) // Print the response
}
});
pendingRequests.push(r);
};
You can modify the boilerplate above and extend it as you please. You can download the code from my github repo here: https://github.com/ashishbajaj99/http-node-example
To install NodeJS, you can easily download NodeJS source code from here: https://nodejs.org/en/download/stable/ and compile the executable on Raspberry Pi. I've done that on my Pi - I am running a Raspbian Jessie image and had no trouble at all. There are some other alternatives - here are a couple of links:
http://www.andrewconnell.com/blog/setup-node-js-on-raspberry-pi-2-b
http://blog.wia.io/installing-node-js-v4-0-0-on-a-raspberry-pi/
https://stackoverflow.com/questions/32563173/installing-node-js-on-raspberry-pi-2
Let me know if you want to use MQTT or other alternatives and I can give you a detailed answer like above. Cheers.
goobering, I'm looking for a solution that goldilocks mentions, where one RPi is an access point for the others. So, it works as a router I guess.
– dnzzcn Feb 03 '16 at 19:51