Today I will talk about you how the Internet of Things can be used in the transportation sector, and I will show you how to monitor temperature in cooling trucks using a device that is smart, connected, and very cost effective.
When shipping fresh groceries, it’s critical to keep the right temperature, and especially in tropical climates like here in Brazil, a problem with the cooling system can quickly turn the goods useless.
In this video I will show you how a cooling truck can be equipped with a smart and connected device (e.g. via a standard 3G router) to publish the temperature on an MQTT broker, which can be subscribed to by tools for analytics and alarms, and I will use the 2lemetry cloud to do some simple analytics.
I will use a really cool chip called the ESP8266, and it’s a complete WiFi module that can be controlled by a microcontroller like an Arduino. It actually has a microcontroller of its own, but to program it takes some more advanced tools, so I will not show this here, but using that approach, you can actually end up with a smart and connected temperature sensor for less than $5. To simplify the programming, I will use a separate Arduino microcontroller, but the complete cost for this setup is still below $10, and it all easily fits into a normal matchbox.
In addition to the Arduino and the ESP8266, you will need a temperature sensor, and to connect everything, you need a breadboard and 8 jumper wires. I recommend that you upgrade the firmware of the ESP8266 (http://blog.electrodragon.com/cloud-updating-your-wi07c-esp8266-now, and I use 0.922), so that it support slower speeds (default is 9600) to allow using the normal Arduino I/O pins for serial communication.
To setup the hardware, you start by disconnecting everything from its power source, and then connect the Arduino VCC to the VCC on both the sensor and WiFi module, and also to the WiFi chip enable (CH_PD) pin. Connect the Arduino GND to the GND on both the sensor and the WiFi. Then connect Arduino pin A0 to the sensor S (signal) pin, the Arduino pin 2 to the WiFi transmit (TXD) pin, and the Arduino pin 3 to WiFi receive (RXD) pin.
This is the Arduino code (Sketch) to read the temperature in the truck and send it over WiFi to the server…
SoftwareSerial SoftSerial(2, 3); // RX, TX
const int tempPin = 0;
char tempChars = "";
int value = analogRead(tempPin);
float temp = log((10240000.0 / value) - 10000.0);
temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp)) * temp);
temp -= 273.15;
dtostrf(temp, 4, 2, tempChars);
String cmd = "GET /publish?temp=" + String(tempChars) + " HTTP/1.0";
SoftSerial.println(cmd.length() + 4);
…and after the initial include of the software serial library (on line 1), a variable is created for it (on line 3). In the setup, the software serial communication is initiated (on line 9), and then the WiFi module is queried for an IP address (on line 10). If there isn’t an IP address (checked on line 11, which means that it’s not connected to the access point), we connect to the access point (on lines 13 and 15). The eternal loop starts by getting the temperature from the sensor (on lines 22-26), and then the IP connection is started (on line 27). We check for any connection errors (on line 29), and create the HTTP request (on line 30). We define the length of the request (on lines 31-32), and if the WiFi module is ready for the request (checked on line 34), we send the request (on lines 36-37). Lastly, we close the connection (on line 40), and wait so that the temperature is measured every 10 seconds.
As you can see, I’m just sending a HTTP GET request with the temperature, but when there’s a MQTT library available (there are some things in the works, e.g. https://github.com/soynerdito/ESP8266-MQTT), I would rather use it directly from the device. For now I’m just using a simple PHP page on a server to do the MQTT publishing, and here is the server code that publishes the temperature to the 2lemetry MQTT broker…
$temp = $_GET["temp"];
$mqtt = new phpMQTT("q.m2m.io", 1883, "TruckTemp");
if($mqtt->connect(true, NULL, "USERNAME", "MD5PASSWORD"))
…and after the include of the MQTT library (on line 2, you can download it from https://github.com/bluerhinos/phpMQTT), I get the temperature parameter value (on line 3). Then I create the MQTT object (on line 4) and connect to the MQTT broker (on lines 5). If the connection is successful, I publish the temperature to the topic (on line 7), and close the MQTT connection (on line 8).
You sign up for a 2lemetry account (on http://app.m2m.io) to get the info to insert in the code (username/email, password, and domain), and please note that you need to MD5-hash the password (on a Mac you just open the terminal and enter the command “md5 –s password”, where you insert your password as the last parameter). When you are logged in, you can select the PAST menu option and enter the fields with “Arduino” (stuff), “TruckTemp” (thing), and an underscore (attributes). When you hit enter, you will see the published data, and if you select the first value, you will get a chart that looks similar to this. It would be as easy to subscribe to the MQTT topic and check that the temperature is in a valid range, and if not, trigger an alarm, like notifying the driver and maybe also a service technician.
That’s how the Internet of Things can be used in the transportation sector.