Towards Lab IoT Part One
Chemistry scale-up labs tend to have multiple pieces of equipment connected to a single reactor. These items can work indepentently, co-dependently or fully-dependently with respect to each other. Automated reactors have been in existence for a number of years. These systems can generally collect and store data for a limited number of interfaces and typically work through wired connections. Here I'm documenting the first step towards a fully-functioning internet-of-things (IoT) lab so that multiple reactors can run processes that may be dependent on one another.
The final system should allow for the following:
- unidirectional and bidirectional communication
- software-managed PID control so that one input can be controlled by another output
- alarms and failsafes - raise an alarm when out of spec and provide the ability to return to a setpoint
- continual monitor of connection and checking for disconnect (drop leads to alarm and return to failsage)
- database to store measurements
- visualization to monitor data
I settled on a few hardware and software solutions to put this into effect. Much of the MQTT and database development was inspired by several thingsmatic blog entries
Hardware Solutions
Each instrument can be controlled by a small controller. The controller should be able to interface with the instrument and publish and receive data. It should run background code that allows it to react to received data. It should also communicate wirelessly via wifi and allow ssh communication. Obvious choice is the Raspberry Pi ZeroW but we could also consider the Adafruit Feather HUZZAH ESP8266 (a little more challenging to add ssh).
Software Solutions
I chose to implement MQTT as a protocol. It's a lightweight messaging protocol that runs over TCP/IP. MQTT works by having clients publish messages through topics to a broker, which manages all the traffic. Clients can also subscribe to any topics, receiving messages from the broker as they arrive. This routes all messages through a central location as well as allowing clients to communicate with each other (via the broker). For IoT data received from multiple sources with time being a key piece of metadata a time series database (in this case InfluxDB) is a suitable choice for data storage. The data can then be displayed in real-time using grafana.
Test Sandbox
I used virtualbox to test the concept. Ultimately I created three ubuntu instances to mimic a server and two remote linux controllers.
Installing software
InfluxDB and grafana were installed to the server instance and MQTT was installed on the server as well as the two other virtual machines.
Installing InfluxDB
Add the InfluxData repository
1wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
2source /etc/lsb-release
3echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
Install and start the InfluxDB service
1sudo apt-get update && sudo apt-get install influxdb
2sudo service influxdb start
Configuration file is located under /etc/influxdb/influxdb.conf
Most settings are commented out and take the default. Apply the following changes for a simple system:
1# remove data-usage reporting
2reporting-disabled = true
3
4[http]
5 # Determines whether HTTP endpoint is enabled.
6 enabled = true
7
8 # The bind address used by the HTTP service.
9 bind-address = ":8086"
10
11 # Determines whether user authentication is enabled over HTTP/HTTPS.
12 auth-enabled = false
Launch / relaunch with config file
1influxd -config /etc/influxdb/influxdb.conf
2sudo service influxd restart -config /etc/influxdb/influxdb.conf
Testing InfluxDB
1$ influx -precision rfc3339 // starts the InfluxDB shell and shows time in rfc3339 format
2Connected to http://localhost:8086 version 1.2.0
3InfluxDB shell version: 1.2.0
4> create database demo // Creates a new database
5> use demo
6Using database demo
7> insert mysensor1 value=0.51 // Insert new datapoints
8> insert mysensor1 value=0.53 // for the measurement
9> insert mysensor1 value=0.45 // "mysensor1"
10> select * from mysensor1 // Get the whole time series
11name: mysensor1
12time value
13---- -----
142017-02-26T13:33:49.00279827Z 0.51
152017-02-26T13:33:53.045762633Z 0.53
162017-02-26T13:33:57.015427575Z 0.45
Installing grafana
Add repository
1sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
2wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
Install grafana
1sudo apt-get update
2sudo apt-get install grafana
Start service
1sudo service grafana-server start
To start at bootup:
1sudo update-rc.d grafana-server defaults
The configuration file is found at /etc/grafana/grafana.ini
and we’ll need to enable http protocol before use:
1[server]
2# Protocol (http, https, socket)
3protocol = http
4
5# The ip address to bind to, empty will bind to all interfaces
6;http_addr =
7
8# The http port to use
9http_port = 3000
Service can be relaunched with
1sudo service grafana-server restart
Testing grafana
Connect database
Point browser to http://localhost:3000/
A getting-started guide can be found at https://grafana.com/docs/guides/getting_started/
log in (default = admin/admin) and click on "add data source" select InfluxDB as the source and then set the following parameters:
1Name: InfluxDB
2HTTP URL: http://10.0.2.15:8086 (obtained from ifconfig command)
3Database name: demo
Create dashboard Dashboards can be built using "create dashboard". Add a query that reads
1Query: sensordata
2FROM default mysensor1 WHERE
3SELECT field(value) mean()
The grafana time plot should show data from the Influx database, for example:
Installing MQTT
MQTT (MQ Telemetry Protocol) is a lightweight protocol for communication in IoT devices. In order to utilize it we must install Mosquitto, a message broker that implements the MQTT protocol. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.
Mosquitto is available in the Ubuntu repositories:
1sudo apt-get install mosquitto
2sudo apt-get install mosquitto-clients # Mosquitto cmd line clients (use via bash)
Testing Mosquitto is running
1service mosquitto status
Running a test communication between two servers (command line)
In order to ensure that each VM has a separate IP address do the following:
- VirtualBox control Panel: File -> Preferences -> Network
- select NAT networks tab and create a new network called “NatNetwork”
- in each VM: Settings -> Network -> Attached to: change to “Nat Network” and select “NatNetwork” from the Name dropdown
- Restart all VMs
The following lines will create a subscriber on machine 1 which will listen for messages from machine 2 which have a topic starting with message followed by any other sub-topic. More details on how to use the protocol can be found at https://mosquitto.org/
machine 1 (ip address = 10.0.2.15)
mosquitto_sub -h 10.0.2.15 -t 'message/+' &
listens for message in background
machine 2 (ip address = 10.0.2.4)
mosquitto_pub -h 10.0.2.15 -t 'message/my_comp' -m 'Hi there'
Publish message
If all works well machine 1 should display 'Hi there' each time the mosquitto_pub command is run.