Blog

This article describes a number of ways to connect an Arduino™, representative of a Class 0 device, to an MQTT broker to supply real world data while prototyping IoT solutions.

IoTPrototypesUsingArduino2

Using Arduinos to prototype IoT solutions

A lot of the information surrounding the use of the popular Arduino single-board microcontroller centers around its adoption by the maker community. What may not be as obvious is how effectively it can be used to prototype sensor devices in the last 100 metres of an IoT solution. Arduino-based sensors can be a very flexible and cost-effective tool in developing a solution aimed at solving business needs.

IoT devices are often constrained by cost and power considerations, so they tend to have limited amounts of memory and code space.

The Internet Engineering Task Force (IETF®) categorizes these types of devices by class, based on available resource limits:

Class Typical Resources
Class 0C0very constrained
Class 1C110 Kb RAM, 100 KB ROM/code space
Class 2C250 Kb RAM, 250 KB ROM/code space
ArduinosForIotPrototypes

Arduino UNO

The Arduino UNO, with 2KB or SRAM and 32KB of Flash, and powered by the inexpensive Atmel® ATmega328 chip, is very representative of a Class 0 device. What makes them attractive to use in prototyping an IoT business solution is that they are readily available even in small quantities for less than $27 (at the time of this writing) and provide an array of input and output pins that can be used to attach to any variety of sensors.

Depending on your business, these sensors could include any number of the following:

Environmental sensors
Vibration sensors
GPS receivers
Accelerometers
Motion detectors
Humidity sensors
Gas detectors
Flow meters
Pressure/Vacuum sensors
Custom PCB sensors/controllers
RFID
Hall Effect (magnetic) sensors
Barcodoes/QR codes
Wearable sensors
Physiological sensors: muscle activity, oxygen saturation, heart rate, ecg, eeg, blood pressure monitor.

Regardless of the type of sensor, or the kind of interaction being monitored, IoT devices all share the common requirement to take some form of measurement on a regular or triggered basis, and deliver the resultant data to the cloud to be further processed and analysed.

ArduinoNetworkDrawing

Figure 1: Arduino Network Diagram

Communicating securely to the Internet

In most businesses it is important to protect the source and flow of your data. The constraints of a Class 0 device make it difficult for them to connect securely to the Internet without the help of an intermediate proxy or gateway device.

We have taken a number of different approaches when connecting the Arduinos we have used as sensors in our IoT prototypes and proof of concept projects.

In keeping with trying to provide practical information, we’ve included sample code that shows measurements being taken of a typical type of sensor that might be used in an IoT project.  In this case a Hall Effect integrated circuit was used that provides a voltage output that is proportional to an applied magnetic field.  This type of device can be used for example to detect rotational speeds of equipment, or sense whether hoppers and gates, are open or closed.  

When it is possible to have a wired Ethernet connection at the sensor we use an Arduino connected to an Ethernet shield (see Figure #2), and use the Knolleary Arduino client for MQTT → to communicate the sensor readings to a local MQTT broker acting as a bridge running on a separate node on the LAN.

See our previous article Mosquitto as a bridge to a secure MQTT broker for more information on the bridging setup.

Figure 3 shows an Arduino sketch (WiredHallEffectSensor.ino) that takes the analog reading of the Hall Effect device every second, formats the value along with some timing information and the device ID into a JSON string and sends it to the Mosquitto MQTT bridge over the Ethernet connection provided by the shield.

ArduinoEthernetShieldFigure 2: Arduino UNO with Ethernet shield

Figure 3: WiredHallEffectSensor.ino Sketch

ArduinoEthernetRouterFigure 4: Arduino connected to OpenWRT router

When the circumstances demand a wireless connection, we’ve often connected the Arduino to a mini-router (see Figure #4) that we’ve flashed with a version of the OpenWRT operating system and run in station mode. This allows us to not only do away with the requirement for a wireless Ethernet shield, but we can run Mosquitto on it and use it to establish a SSL connection to the cloud based MQTT broker. The additional benefit of this combination is that it can be used in locations where providing mains power is difficult.

We originally thought we could send data directly over the Arduino’s USB port to a daemon running on the OpenWRT router. However we did not find the USB serial data transfer reliable enough, and have since resorted to using  a wired Ethernet shield to connect the two with a short CAT5 cable.   The direct USB connection is still used to provide power to the Arduino and allows us to implement a watch dog function on the mini-router for increased robustness.

Figure 5 shows only the portion of  the Arduino sketch (RouterHallEffectSensor.ino) which is different than the previous sketch. The Setup function needed to be changed to describe the slightly different network topology.  The Arduino Ethernet shield is connected to the RJ45 connector on the router which has an IP address of 192.168.1.1 , and the router is configured to be in station mode so it can be connected wirelessly to another subnet (192.168.2.x).

 

Figure 5: RouterHallEffectSensor.ino Sketch Fragment

The Arduino UNO is designed to automatically restart when the USB serial port is opened.  To take advantage of that we wrote a small watchdog daemon (Figure 6) to run on the OpenWRT router that uses the Mosquitto client library → to subscribe to the same MQTT topic that the Arduino is publishing to, and resetting the Arduino if too much time has passed since the last update. The setup for the Mosquitto instance running on the OpenWRT router is identical to that described in our earlier article Mosquitto as a bridge to a secure MQTT broker.

We are again using a modified router (running the OpenWRT OS version Attitude Adjustment 12.09, which we compile from source to allow us to use Mosquitto and client library 1.3.1 rather than 0.15 ). Virtually the same code could be used on a PC, running Linux or Windows, connected to the Arduino’s USB port. We chose the mini-router solution rather than an Arduino wireless Ethernet shield because it provided us with more flexibility and security (full SSL) for less cost and power requirements.

Figure 6: ArduinoWatchDog.c running on OpenWRT mini-router

Arduino controllers can be very effective for prototyping solutions to address business needs because they can rapidly demonstrate proof of concepts with real systems.

References

The Arduino Community Logo is reproduced or modified from work created and shared by Arduino and used according to terms described in the Creative Commons Attribution 3.0 license →. The Arduino name is a protected trademark of Arduino.

Leave a Reply

You must be logged in to post a comment.