This repo includes a number of scripts that use the MQTT protocol to help implement IoT sentient spaces. While tested on Raspberry Pi and Ubuntu desktop the scripts will probably run on most embedded/desktop Linux systems.
The scripts are:
- RTSensorMQTT. This script streams data from selected I2C environmental sensors.
- RTUVCCamMQTT. UVC-based USB webcams can be used to generate MJPEG video streams using this script.
- RTPiCamMQTT. A special version of RTUVCCamMQTT that has been modified to use the Raspberry Pi's camera instead of a USB webcam.
- RTAudioMQTT. Audio can be captured from Alsa sound devices using this script.
- RTMediaViewMQTT. This script can be used to view and output the audio and video streams generated by the other scripts.
- RTSensorViewMQTT. This script captures the output from RTSensorMQTT and displays the data on a series of graphs.
- RTDecodedSpeechMQTT. This script will display and speak the output from RTSRServer, part of RTAutomation.
- RTArduinoRelay. This is an Arduino sketch that allows four relays (or any device controllable by GPIO) to be controlled using RTControllerMQTT.
- RTControllerMQTT. This is a desktop Qt program that provides a GUI to control RTMQTT and RTArduinoMQTT devices.
RTUVCCamMQTT uses a video capture library RTUVCCamLib that can be used in other applications that need video capture from UVC USB webcams.
Supported sensors include:
* ADXL345 acceleration sensor.
* BMP180 pressure/temperature sensor.
* HTU21D humidity/temperature sensor.
* MCP9808 temperature sensor.
* TMP102 temperature sensor.
* TSL2651 light sensor.
Any combination of these sensors can be used - check RTSensorMQTT.py for information on how to customize the sensor set.
Prepare the system by installing the pre-requisites:
sudo apt-get install git python-dev python-pip python-smbus python-pyaudio portaudio19-dev mosquitto-clients
To use RTUVCCamMQTT, it is necessary to build RTUVCCamLib and there are some extra pre-requisites for this:
sudo apt-get install libqt4-dev
Note that older versions of pyaudio do not support stream_callback. If this error occurs when running RTAudioMQTT, enter:
sudo apt-get remove python-pyaudio
sudo pip install --upgrade pyaudio
This will ensure that the latest version is in use.
Prepare the system by installing the pre-requisites:
sudo apt-get install git python-dev python-pip python-pygame python-pyaudio mosquitto-clients
To use RTUVCCamMQTT, it is necessary to build RTUVCCamLib and there are some extra pre-requisites for this:
sudo apt-get install qt4-default
for Qt4 or:
sudo apt-get install qt5-default
for Qt5.
If it is desired to run the mosquitto MQTT broker on the desktop (there has to be one broker somewhere), add:
sudo apt-get install mosquitto
This can be done using pip:
sudo pip install --upgrade pip
sudo pip install paho-mqtt
Clone the repo:
cd ~/
git clone git://github.com/richards-tech/RTMQTT.git
If RTUVCCamMQTT is required, the underlying library needs to be built and installed. This can be done by entering:
cd ~/RTMQTT/RTUVCCamLib
qmake
make -j4
sudo make install
python setup.py build
sudo python setup.py install
If RTControllerMQTT is required:
cd ~/RTMQTT/RTControllerMQTT
qmake
make -j4
sudo make install
Each of the scripts has its own set of command line parameters and details can be obtained by entering:
cd ~/RTMQTT/RTxxxMQTT
python RTxxxMQTT.py -h
This will display the command line parameters and their default settings.
To run RTSensorMQTT for example:
cd ~/RTMQTT/RTSensorMQTT
python RTSensorMQTT.py -b broker_address
The default broker address is localhost so if the broker is running on the same machine then the -b option can be omitted. The broker_address can be either an IP address or hostname if the network can resolve the hostname to an IP address.
Note that RTUVCCamMQTT defaults to a windowed mode and displays a preview of the video stream. If running in console mode or on a Raspberry Pi, the script should be run in non-GUI mode:
cd ~/RTMQTT/RTUVCCamMQTT
python RTUVCCamMQTT.py -b broker_address -x
Running in GUI mode on a Raspberry Pi at the default 30 frames per second is not recommended.
The RTMediaViewMQTT script can be used to display video and audio streams. By default, the topics it subscribes to are the default topics for RTUVCCamMQTT and RTAudioMQTT but these can be changed using command line options. Run:
cd ~/RTMQTT/RTMediaViewMQTT
python RTMediaViewMQTT.py -b broker_address
Alternatively, if using RTPiCamMQTT to generate the video stream, enter:
cd ~/RTMQTT/RTMediaViewMQTT
python RTMediaViewMQTT.py -b broker_address -v rtpicam/video
if using the default topic for RTPiCamMQTT.
To display the sensors stream, enter:
mosquitto_sub -t rtsensor/sensors -h broker_address
This will display the JSON record containing the sensor readings. For example:
{"temperature": 22.8, "timestamp": 1451261500.87687, "humidity": 39.261383056640625, "topic": "rtsensor/sensors", "pressure": 1234.51, "deviceID": "rtsensor", "light": 8.800396825396827}
In fact all the messages streamed over MQTT use JSON coding so mosquitto_sub can be used to inspect any of the RTMQTT topics.
To display in a graphical form, use RTSensorViewMQTT:
python RTSensorViewMQTT.py -b broker_address -t sensor_topic
The -t argument is only needed if RTSensorMQTT is using a non-default deviceID. Enter:
python RTSensorViewMQTT.py -h
To see a list of arguments and default values.
This sketch allows four relays (or other devices that can be controlled by GPIO) to be controlled via MQTT using RTControllerMQTT. RTControllerMQTT can also control Insteon devices by using RTInsteonServer from the RTAutomation repo (https://github.com/richards-tech/RTAutomation). RTArduinoRelay appears as a server with an identical interface. RTControllerMQTT can be configured to control devices connected to multiple servers simultaneously.
Tested on an Arduino UNO, by default the sketch uses digital pins 2, 3, 5 and 6 for the relays. As the Arduino needs to be connected to a LAN, an Ethernet shield is required. Check the README in RTArduinoMQTT/libraries/PubSubClient for details of compatible shields (I have only tested using the Arduino Ethernet shield however).
If more than one instance of RTArduinoRelay is being used on the same LAN, the MAC address of at least one needs to be changed, as does the DEVICEID and MQTT topics as they all need to be unique. See RTArduinoMQTT/RTArduinoRelay/RTArduinoRelay.ino for more details of what has to be configured.
RTDecodedSpeech.py will display text and speak commands that have been decoded from speech by RTSRServer, part of RTAutomation (https://github.com/richards-tech/RTAutomation). It requires that espeak has been installed on the system to provide the text to speech capability.