Batman - Tales from our IoT journey - Part 2

 This blog post is written by Anssi Soinu

This blog post is written by Anssi Soinu

Blog series
Part 1, Introduction
- Part 2, Batman
- Part 3, Batmobile
- Part 4, Batcave
- Part 5, Conclusions

This is the second blog post in our IoT journey series. In this post we will go through the IoT device code-named "Batman".


Why do we really need a device?


I think it is good to start with a question: "Why do we really need a device?". Personally I like to always understand why we are doing something and what are we aiming to solve.

Let's recap a bit from the introductory blog post. The challenge behind the LED pylon is that it operates with batteries. If the battery runs out, the pylons lights are off. In order to make sure that the lights are not off - a person needs to regularly visit the LED pylon and measure the state of the battery

So, the problem we are solving with a device is that we're reducing the need for physical visits at the LED pylon. The purpose of the device is to measure the state of the battery and send it to our cloud back-end. 


What is the simplest way to try out the idea in practice?


Personally I'm a fan of trying out things in practice, so the next natural question in our case was "What is the simplest way to try out the idea in practice?". Basically we had to solve two things: measure the state of a battery and send the measured battery state to cloud back-end.

We decided to start by solving the challenge of measuring the state of a battery. A battery has a certain voltage when it is full and a certain voltage when it is empty, so to measure the state of a battery you need to measure its voltage.

We ended up ordering Arduino Uno development boards, voltage sensors, breadboards, test batteries and a bunch of jumper wires. Below you can see pictures of the first version of the device. 

First version of batman.JPG


Hardware alone wasn't enough to solve the problem, software was also needed


In the first version of Batman the required code was all about showing the measured battery state (voltage) in the Arduino IDE serial monitor. To achieve this we found a great blog post from here. Really good blog post that explains how to measure the voltage of a battery. We wrote some similar code:

If you are not familiar with writing code for Arduino - the code is written in C language and basically has two main functions: setup and loop. The setup is executed once when the device is started and the loop is executed constantly when the device is running. 

In our case we initialise the Arduino A0 pin for the battery voltage measurement and the serial for writing the measured battery voltage in the setup function. Then in the loop function we are constantly measuring the battery voltage and writing it to the serial. 

The actual measurement is done in three steps. First we read the input from the Arduino Uno A0 pin. Then we calculate the voltage out (vout) and as a last step we calculate the voltage in (vin). The voltage in and out calculations are done based on the facts how the voltage sensor is built. The voltage sensor is in practice a voltage divider which has two resistors (R1=30k Ω and R2=7.5k Ω).

And below you can see the end result in the Arduino serial monitor.


Then it was on to the second challenge - sending the measured battery state to the cloud back-end! 


After solving the first challenge, it was time to solve the next one - send the measured battery state to the cloud back-end. To be able to do that we ended up ordering more hardware components.

We ordered Arduino GSM shields and made a M2M ("Machine to Machine") subscription agreement with one of the network operators in Finland. With a M2M subscription you will get a SIM card which is not the same kind of a SIM card which you get for your mobile phone. It is designed to work in a bit more extreme environments than a normal mobile phone. The reason why we are using GSM over for example Wi-FI is that in the environment where Batman should operate, the only network available is GSM. 

Below you can see pictures of the second version of Batman - device with a sending capability.  


To be able to send the measured battery state to our cloud back-end, we had to also improve our current device software. We had to implement logic for: 

  • opening a network connection, 
  • creating the battery state message and
  • sending the battery state update HTTP request to our cloud back-end

In the code capture below you can see how the device code looks after adding the sending functionality. 


You might recognise that we have removed from the code the usage of the serial monitor: in the setup function we now open the GSM connection instead of initialising the serial monitor and in the loop function we send the measured battery state to our cloud back-end instead of printing it to the serial monitor. 

To be able to send the measured battery state from the device to our cloud back-end, we created the following APIs to our cloud back-end:

  • PUT{batteryId}/state and
  • GET{batteryId}

The PUT API will insert/update the battery state information in the cloud back-end. The GET API makes it possible to retrieve battery information from the cloud back-end. 

We will dive into details of our cloud back-end in a later blog post. 


The back-end does not receive battery states?! I thought we had everything in place...


So, in theory everything was supposed to be working - the device had all the required hardware and software. Everything was in place as we expected. We were ready to ship the first battery state to the cloud back-end but after switching the device on, nothing happened. Our cloud back-end did not receive any battery states.

We decided to go back a bit and switched the serial monitor on to get some information about what was going on in our device.  After some time we found out that the Arduino GSM shield was not on at all. We basically expected the GSM shield to start automatically when the device is powered on. We found out that the GSM shield has a power button which has to be pressed to turn it on. After switching the power on in the Arduino GSM shield, our back-end started to receive battery states correctly. 

You can see the Arduino GSM shield power button in the picture below. 

At this phase we had proven that it is possible to measure a battery voltage with a device and it is possible to send the measured battery voltage to a cloud back-end. Our try it out in practice was a success.  

We had still some things to do before the device could be used in a LED pylon for measuring the LED pylon battery state. We had to:

  • implement the device to take power from the measured battery,
  • analyse/measure how much the device itself consumes battery,
  • optimise the device battery consumption, 
  • implement a custom Arduino shield for the battery voltage sensor,
  • remove interference from the battery voltage measurement
  • do the device casing,
  • start the GSM Arduino shield automatically when the device is powered on,
  • make sure that the device is powered on automatically when the LED pylon batteries are changed, 
  • make sure that the device can operate in cold and warm environments,
  • make sure that the device can operate in moist environments,
  • do "long running tests" for the device and
  • install the device to an actual LED pylon

To keep this blog post a bit shorter we decided not to go into details of the previous mentioned improvements/actions since most of them could even be their own blog posts. We will try to write out separate blog posts to share for example how did we optimised the device battery consumption or how did we powered on the Arduino GSM shield automatically when the device was powered on. 


We have open sourced the Batman!


Although we do not go through all of the details in this blog post, you can get the complete device source codes from AradoLabs Github. Below is also a shopping list for buying all the required hardware components in order to build the device by yourself.

Have fun on building your own Batman or a similar device by yourself! 


Shopping list for building the Batman
- 1 Arduino Uno development board
- 1 Arduino GSM Shield
- 1 voltage sensor (this is the one we used)
- 1 breadboard
- 5 jumper wires
- 1 9V battery
- 1 M2M (SIM card) agreement with a local network operator