Experimenting with the Microduino MicroWRT Core (II)
This article is a follow-up of the previous Experimenting with the Microduino MicroWRT Core (I), which covered the deployment of a small qMp-based mesh network with three MicroWRT Core devices. In this second part we show how to add a Microduino module that reads information from a sensor and spreads this information to all the nodes in the mesh.
Microduino code
The MicroWRT Core devices that Jason from Microduino™ kindly sent us were shipped with the MicroWRT UPIN, an extension board that allows stacking Microduino modules like the Microduino Core+, also included in the package we received.
We program one of the Microduino Core+ modules to read the value from a Freescale Semiconductor MPX4115 pressure sensor, which will provide the atmospheric pressure read. The following code is used:
void setup() {
//Set the baud rate 115200
Serial.begin(115200);
//Set digital pin 13 as OUTPUT
pinMode(13, OUTPUT);
//Set pin 13 digital output as 0V (ground)
digitalWrite(13, LOW);
//Set digital pin 12 as OUTPUT
pinMode(12, OUTPUT);
//Set pin 12 digital 12 output as 5V (Vcc)
digitalWrite(12, HIGH);
}
void loop() {
//Read the voltage from A0 analog pin
int n = analogRead(A0);
//Use a double type variable to save convert the
//read analog value to the actual voltage
double volt = n * (5.0 / 1024.0);
//Convert the read voltage to kPa according to
//MPX4115 specifications in datasheet
double press = 10*((volt)/5 + 0.095)/0.009;
//Output the pressure value via the serial port, issuing
//a command that will be received by the MicroWRT device
Serial.println("echo " + (String)press + " hPa > /tmp/arduino");
//Wait for 10 seconds to refresh data
delay(10000);
}
The code does the following:
- void setup(): the required digital pins are configured, as well as the serial output port.
- void loop(): the analog value from the sensor (i.e. an output voltage from the MPX4115 IC) is read from analog pin A0. The value is converted to the actual voltage (the volt variable) and then converted to the current atmospheric pressure (the press variable). The text "echo 12345.67 hPa > /tmp/arduino" (where 12345.67 is the actual pressure) is issued via the serial port.
MicroWRT Core + Microduino Core+ connection
Thanks to the MicroWRT UPIN expansion board, the Microduino Core+ board can be stacked on top of the MicroWRT Core, as shown in the picture.
The interaction between the MicroWRT Core and the Microduino Core+ is performed via the serial port. The Microduino module issues the "echo 12345.67 hPa > /tmp/arduino" commands, which are received by the MicroWRT Core. Therefore, every 10 seconds, the MicroWRT Core saves the pressure to the file /tmp/arduino.
The DIP switch of the MicroWRT UPIN expansion board must be properly configured so that the serial port of both the MicroWRT Core and the Microduino Core+ are directly connected. The switch on the left of the image is configured with the values "0101" and the one on the right is set to "1010".
In our case, the MicroWRT3 device is the one that has the Microduino Core+ board stacked with the atmospheric pressure sensor. To check everything is working fine, we can connect via SSH to the device and check what is being received on the serial port:
qmp@qmp-dev:~$ ssh root@10.88.99.1 root@10.88.99.1's password: BusyBox v1.23.2 (2015-10-16 21:45:02 CEST) built-in shell (ash) _______ ________ __ | |.-----.-----.-----.| | | |.----.| |_ | - || _ | -__| || | | || _|| _| |_______|| __|_____|__|__||________||__| |____| |__| W I R E L E S S F R E E D O M ----------------------------------------------------- CHAOS CALMER (Clearance, r47269) ----------------------------------------------------- * 1 1/2 oz Gin Shake with a glassful * 1/4 oz Triple Sec of broken ice and pour * 3/4 oz Lime Juice unstrained into a goblet. * 1 1/2 oz Orange Juice * 1 tsp. Grenadine Syrup ----------------------------------------------------- __ _ /\/\ _ __ / _` |/ \| '_ \ | (_| / /\/\ \ |_) | \__, \/ \/ .__/ |_| |_| quick MESH project --------------------------------------------------- qMp Clearance (3.2, master rev.9782ba8-20151026_2103) --------------------------------------------------- http://qmp.cat/projects/qmp/repository/show?branch=master&rev=9782ba8 --------------------------------------------------- root@MicroWRT3-20d0:~# cat /dev/ttyS0 echo 990.97 hPa > /tmp/arduino echo 989.89 hPa > /tmp/arduino
We can see how, every 10 seconds, the "echo 12345.67 hPa > /tmp/arduino" command is being received on the serial port. The MicroWRT Core and the Microduino Core+ boards are successfully connected. Furthermore, if we check the file at /tmp/arduino, it shows the actual pressure read:
root@MicroWRT3-20d0:~# cat /tmp/arduino
989.89 hPa
root@MicroWRT3-20d0:~#
Exporting the information using BMX6 SMS plugin
The BMX6 routing protocol can be extended with several plugins. One of them is the SMS plugin, which uses the BMX6 routing packets to transmit any information to all the nodes in the mesh network.
The API of the SMS plugin is very straightforward. It simply clones the content of one or more files from any node to all the other nodes, and all of them can do the same. Once the plugin is started, each node has two directories, /var/run/bmx6/sms/sendSms and /var/run/bmx6/sms/rcvdSms. Once we put a file in the sendSms folder of a node and tell BMX6 to use it, the file is replicated to all the nodes of the mesh.
We start by copying the file with the sensor read at /tmp/arduino to the sendSms folder of the MicroWRT3 node:
root@MicroWRT3-20d0:~# cp /tmp/arduino /var/run/bmx6/sms/sendSms/
We now tell BMX6 to syncrhonize the file named arduino in the sendSms folder to all the nodes:
root@MicroWRT3-20d0:~# bmx6 -c --syncSms arduino
And in a few milliseconds, the file is replicated to the rcvdSms folder of all the nodes. We check it on another node, for instance, MicroWRT2:
root@MicroWRT2-20d8:~# ls /var/run/bmx6/sms/rcvdSms/
MicroWRT3-20d0.51DA8FA586E073D0FB4B:arduino MicroWRT3-20d0.51DA8FA586E073D0FB4B:mdns
root@MicroWRT2-20d8:~# cat /var/run/bmx6/sms/rcvdSms/MicroWRT3-20d0.51DA8FA586E073D0FB4B\:arduino
990.97 hPa
Updating the sensor read to all the mesh periodically
The BMX6 SMS plugin detects changes in the files under the sendSms folder and synchronizes them all over the mesh network. However, we need to refresh the content of this file with the latest value, which is stored at /tmp/arduino.
We can add a line to the crontab file to copy the file every minute:
root@MicroWRT3-20d0:~# vim /etc/crontabs/root
39 * * * * /usr/sbin/libremap-agent
* * * * * /etc/qmp/bmx6health.sh >> /tmp/log/bmx6health.log
33 * * * * /etc/init.d/mdns reload > /tmp/log/mdns.log
* * * * * [ -f /tmp/arduino ] && cp /tmp/arduino /var/run/bmx6/sms/sendSms/ && bmx6 -c --syncSMS arduino
root@MicroWRT3-20d0:~# /etc/init.d/cron restart
Now, every minute, the device will check if the file at /tmp/arduino exists. If it is there, it will copy it to the sendSms folder and tell BMX6 to distribute it all over the mesh (this last part is actually needed only once).
Conclusion
We have seen that building a small qMp-based mesh network using three MicroWRT Core devices is reasonably easy and affordable. These two articles have shown how to install qMp on the devices and configure them to create a mesh network. Adding a MicroWRT UPIN expansion board has allowed us to stack Microduino board with an atmospheric pressure sensor. With a little bit of configuration, we've managed to connect them via the serial port, get the value from the sensor at the MicroWRT device and spread the information to all the nodes in the mesh.
This example can be easily replicated to create a mesh of nodes with sensors (temperature sensors, light sensors, etc.) and actuators (switches, relays, etc.).