Visualising the battery's state of charge using Datacake

If you have been following the previous tutorials, you should now be able to collect sensor data, monitor the battery’s state of charge, and send this information to The Things Network (TTN).

Visualising this information on Datacake will allow us to check sensor data (in this case temperature) at a glance, and also tell us when to recharge the battery.

The sketch we uploaded in the previous tutorial Battery power - connecting and charging the Li Po battery, uploaded two pieces of information to TTN.


payload[0] = tempVariable; // Send temperature by LoRa.
payload[1] = cellPercent; // Send state of charge by LoRa.

Debug tab temp only

Now login to your Datacake account, choose your board and click on the ‘Debug’ tab. You should see this. 

At the moment, we can only see temperature data. This is because we only set up the Payload Decoder to extract temperature information. We now need to add the battery’s state of charge.

Still in your Datacake account, click on the ‘Configuration’ tab. Newer versions of the Datacake app will also require you to click on the ‘Product and Hardware’ link in the left-hand navigation. Now scroll down to the ‘Payload Decoder’ section.

Payload decoder temp

Now add these few lines of JavaScript.


{
"field": "SOC",
"value": bytes[1]
},


Payload decoder temp and soc

Your completed script should look like this.

Scroll down and click on the ‘Save’ button.

You can now check that your modified code is working. Scroll just below the ‘Payload Decoder’ to the ‘Try Decoder’ section. 

Try decoder

In the box labelled ‘Payload’, type in some dummy values that you might expect from your sensor.

You could try ’10 11’. These are the hexadecimal values for temperature and state of charge, as received from your sensor. Click the blue ‘Try Decoder’ button. You should see this if all is working.

So, the temperature is reported as being 16 degrees and the start of charge is at 17 percent.

Add field blank

We now need to configure Datacake to show our state of charge value in the visualisation. Click on the ‘Configuration’ tab and this time select ‘Fields’ in the left-hand menu.

If you see a ‘Suggested Fields’ section. Then click the blue ‘Create Field’ button. Otherwise, click the blue ‘Add Field’ button.

You will see a form like this.

Add field text

We only need to complete three boxes to display state of charge as a percentage. From the ‘Type’ box, select integer.

In the name box, type ‘Soc’.

This will automatically complete the identifier box with ‘SOC’. 

Add field completed

Now click the blue ‘Add Field’ button. If you return to the ‘Fields’ section of the configuration tab, you should see the new SOC field added.

You can now include this field in your Datacake dashboard.

Empty dashboard

Here are the instructions for setting up a state of charge visualisation in your Datacake dashboard (some of the following images are for Board 4 as I already had the screenshots).

Click on the dashboard tab in your Datacake account, then toggle on the edit button on the extreme right of the top navigation bar. 

Add widget

Now click on the blue ‘Add Widget’ button.

This will launch a pop-up window.

Add value widget

Scroll down the list of widgets and click on the ‘Value’ widget (it is the last one). 

This will launch another pop-up window.

Widget state of charge

By the way, the second box in the title section which defaults to German, is because Datacake is a German company.

Click the blue ‘Save’ button. Now select the ‘Data’ tab. 

Widget state of charge field

In the ‘Field’ box, select the ‘SOC’ field (or whatever you have called your state of charge field when you originally created it).

The current state of charge should appear in the window at the top of the pop-up box. 

The state of charge of the battery is being reported as 4%. Time to charge the battery perhaps.

Widget state of charge gauge

Now click the ‘Gauge’ tab and select a ‘Gauge Type’. I chose ‘Circular’.

We can now add some colours. Click the ‘Add’ button near the foot of the window. As this will be a percentage value, the range will go from 0 to 100. So, for the first range I left the value at 0 and selected the colour blue.



The colours are in hexadecimal. The colour code starts with a #. Colours are represented on screen as varying proportions of red, green and blue. The first two digits represent the amount of red colour, the next two the green colour, and the last two digits represent the colour blue. For each colour, it’s absence is represented by 00, and the maximum possible value is ff. So, my blue colour is #0000ff.

Click the ‘Add’ button again to specify the colour of the maximum percentage of 100. I have coloured this red, so the colour is #ff0000.

Now click ‘Save’ and close the pop-up window. This will return you to the dashboard.

SOC field added

Here you can see that we have added a graphical field, which tells you when the battery was recharged. The graph was added by clicking the blue ‘Add Widget’ button again and selecting the ‘Chart’ widget. In the ‘Data’ tab of the pop-up window, I then selected ‘SOC’ again.

In the next image you will see that the temperature Value and Chart widgets have also been added. 

All widgets added

For each widget added, you will see three icons in the top right-hand corner. These allow you to edit the widget, clone it or delete it. You can also click within the widget and drag it to a new location if you wish to reorder your widgets.

Near the top of the page, you will see two buttons, Desktop and Mobile. Up until now, you have been editing the desktop version of your Dashboard. Click on ‘Mobile’ and then select ‘Create from desktop’ after clicking the ‘More’ tab. You can then rearrange your widgets for mobile visitors. Your mobile layout will not affect your desktop layout.

When you are done, toggle off the edit version of your dashboard to save your work. Now check the layouts on your mobile and desktop devices.



Next, we will see how long the battery will last. Click here for the next steps.

© Copyright 2025 Dr Dulcamara - All Rights Reserved