6

My mac is connected to four raspberry pi's using a desktop switch. My mac has ip 192.168.1.90 and my RPi's 192.168.1.11, 192.168.1.12, 192.168.1.13, 192.168.1.14. I can control them via ssh using the terminal on my mac, e.g. ssh pi@192.168.1.11 and can have four terminal screens open to control each one of them.

I would like to sent the same command (to control the camera) to each of the four raspberry pi's at the same time. Instead of copy pasting the command to the four terminal windows, would it be possible to somehow control the four pi's at the same time in one terminal window?

crazjo
  • 391
  • 2
  • 9
  • 18

4 Answers4

4

There is MQTT, a publish/subscribe messaging system. Under Linux it seems to be supported by the mosquitto package.

apt-cache search mosquitto
libmosquitto-dev - MQTT version 3.1 client library, development files
libmosquitto1 - MQTT version 3.1 client library
libmosquittopp-dev - MQTT version 3.1 client C++ library, development files
libmosquittopp1 - MQTT version 3.1 client C++ library
mosquitto - MQTT version 3.1/3.1.1 compatible message broker
mosquitto-clients - Mosquitto command line MQTT clients
mosquitto-dbg - debugging symbols for mosquitto binaries
python-mosquitto - MQTT version 3.1 Python client library
python3-mosquitto - MQTT version 3.1 Python 3 client library
libmosquitto0 - MQTT version 3.1 client library
libmosquitto0-dev - MQTT version 3.1 client library, development files
libmosquittopp0 - MQTT version 3.1 client C++ library
libmosquittopp0-dev - MQTT version 3.1 client C++ library, development files

That would allow you to publish the event (take photo) on one machine and have your Pi's subscribed to that event.

This would be a programming solution. I'm not sure if there is a bash interface to the publish/subscribe methods.

I HAVE NOT used it. I'm aware of it simply because I see it often recommended for this sort of task.

You give no indication of any timing constraints, e.g. how closely the photos must be synchronised in time.

EDITED TO ADD

I have had a quick play with mosquitto

sudo apt-get install python-mosquitto # on server and clients
sudo apt-get install mosquitto # only needed on server

I wrote a couple of scripts.

The server tells the clients when to take the photos (now or some time in the future).

The clients wait for the specified time and then take the photo (actually it prints diagnostics).

A webm video here

Server Python script

#!/usr/bin/env python

import mosquitto
import time

mq = mosquitto.Mosquitto()

# Connect to mercury
mq.connect("mercury")

# Take a photo now
t = time.time()
print("take photo at {}".format(t))
mq.publish("photo/trigger", "{}".format(t))

time.sleep(1)

# Take a photo in 10 seconds
t = time.time() + 10
print("take photo at {}".format(t))
mq.publish("photo/trigger", "{}".format(t))

time.sleep(11)

# Take a photo in 5 seconds
t = time.time() + 5
print("take photo at {}".format(t))
mq.publish("photo/trigger", "{}".format(t))

time.sleep(6)

# Take a photo now
t = time.time()
print("take photo at {}".format(t))
mq.publish("photo/trigger", "{}".format(t))

Client Python script

#!/usr/bin/env python

import mosquitto
import time

def on_connect(mosq, obj, msg):
   print "Connected"

def on_message(mosq, obj, msg):
   msg_time = time.time()
   snap_time = float(msg.payload)
   while time.time() < snap_time:
      pass
   now_time = time.time()

   print ("msg={}, snap={}, now={}".format(msg_time, snap_time, now_time))

mq = mosquitto.Mosquitto()

#define callbacks
mq.on_message = on_message
mq.on_connect = on_connect

#connect
mq.connect("mercury")

#subscribe to topic 
mq.subscribe("photo/trigger")

#keep connected to broker
while mq.loop() == 0:
   pass
joan
  • 71,024
  • 5
  • 73
  • 106
  • Thanks Joan. That sounds like an interesting idea but the suggestion of Hari to create some kind of shell/bash script sounds easier. – crazjo Nov 07 '14 at 08:48
  • @JolJols I have edited my post to add an example script. I thought I might as well try mosquitto. – joan Nov 07 '14 at 10:59
  • Wow thanks for investing that time. The video and script were very helpful. Could I load the python script on the four raspberry pi's and then run specific python scripts on the server (my mac)? For example, one script would be to record 4 images/sec of a certain resolution for 15min and store them on an usb drive while another would record for 30min at 1fps. Would I be able to run one such script on the terminal, let it terminate, then select another script and run that at a later point in time? – crazjo Nov 07 '14 at 11:34
  • 1
    @JolJols Yes, it is flexible enough to handle that. The message names are quite arbitrary as are the strings associated with a message. You could have different scripts on the Pis to process the same message in a different way on each Pi. I've only scratched the surface of MQTT capabilities. If I was going to use it myself I'd probably search for on-line tutorials and spend an hour or so just trying things out in Python or the command line. sudo apt-get install mosquitto-clients will let you send messages from the command line, e.g. mosquitto_pub -t "photo/trigger" -m "23". – joan Nov 07 '14 at 12:09
4

If you're asking about controlling the Pi's camera module specifically (as opposed to something like a USB webcam), you might want to have a look at my compoundpi project which was built for this purpose (it'll handle 4 quite happily; I originally made it for a group at the local university which had 20!).

It includes a console and GUI client which should work on Mac (it's all written in Python; the console client should work "out of the box" on Mac but the GUI client will involve installing the PyQt4 bindings). There's also a Python-based client library for scripting.

At the moment, the client has only been tested on Linux (Ubuntu) and Windows (works, but it's a pain to install). I'd be interested to hear of Mac experiences!

Dave Jones
  • 3,978
  • 15
  • 22
  • Very nice indeed! +beer – Piotr Kula Nov 07 '14 at 11:39
  • Just to let you know also when navigation to your org.uk site I can see your cert data. better sort that out quick, revoke and regenerate – Piotr Kula Nov 07 '14 at 11:40
  • Wow this looks brilliant and exactly what I was looking for! Thanks for sharing. I will need some more time to try it all out and go through all the documents, but will get back to you next week with an update. It would be great if you could help me further with this. I will use it for research on animal behaviour and could acknowledge you for your contributions if interested. Is there an address I could contact you at? – crazjo Nov 07 '14 at 11:40
  • @ppumpkin don't worry about those - that's just a self-signed CA I used to use (a few years ago) for internal signing purposes (and it's only the public cert - doesn't matter if anyone gets that). I really should find some time to build a proper website ... too busy with other projects though! – Dave Jones Nov 07 '14 at 13:33
  • 1
    @JolJols - you can find my e-mail address on my GitHub page; I'm trying to find some time to polish off release 0.4 of compoundpi (to take advantage of recent changes in the picamera library, which I also wrote) but it'll probably be December before I to spend any serious time on it. Feel free to e-mail me with any questions/issues though! – Dave Jones Nov 07 '14 at 13:36
2

The simplest way would be to create some kind of a Bash script on your Mac, if you are running bash.

for i in 11 12 13 14; do ssh pi@192.168.1.$i "your command here"; done

Maybe some kind of a terminal multiplexer which allows you to send a command to all shells? For instance, on Windows I use Putty, and a tool called "MTPutty" that wraps multiple putty into tabs in the same window, and there's an option to send a script to all Putty sessions simultaneously.

Personally, I would go with the scripting option. Even create a shell script maybe.

  • That is a good idea. I have no experience with bash scripts on mac but will look into it. If I could indeed run a single line like you suggest in one terminal window that could do the trick. If you have any further advice that would be appreciated. – crazjo Nov 07 '14 at 08:47
  • Nice one liner :) +beer – Piotr Kula Nov 07 '14 at 11:46
2

clusterssh allows you to connect via SSH to multiple servers and send the same commands to each one : clusterssh annotated screenshot

You could also use a terminal emulator that supports groups, such as terminator terminator groups