Managing power consumption in the field is one of the many challenges connected products face. On a lab bench, you can easily monitor for energy spikes and usage patterns with a tool such as a Joulescope but in the field, you can't afford this luxury. Deployed devices often use a fuel gauge or battery monitor IC to measure energy usage.
The recently announced Mojo is a Coulomb counter/fuel gauge companion for the Notecard. This add-on board allows Notecard users to remotely measure energy consumption by placing the Mojo between their battery and their hardware. In Notecard firmware versions (>7.5.2), the Notecard will automatically detect the Mojo over I2C and collect readings for energy consumption in a Notefile called _log.qo.
This allows us to quickly throw together a demo to log the energy consumption of an application, using a Google Sheets route. By configuring this route on Notehub, you can log any measurements taken by Mojo into an easy-to-read Google Sheet.
Connecting Mojo to a NotecarrierUsing Mojo features on a Notecarrier is simple; just connect up either Stemma QT / Qwiic connector to the respective connector on your Notecarrier (any Notecarrier with a Stemma QT / Qwiic will work!). The important part is making sure to connect the battery to the BAT
and the Notecarrier to the LOAD
JST connectors. This allows the current to flow across the Mojo, giving it energy-measuring features!
As the Mojo is a Coulomb measuring IC, you can use it to measure any sort of power source. This could be a supercapacitor, solar panel or even a kinetic charger like a generator! Most commonly it will be used for measuring batteries; we'll look at an example in this project.
Configuring Notecard with Mojo to monitor a batteryTo configure a Notecard to use a Mojo, you should first either charge your battery to full or drain it to empty. In this tutorial, we'll use a 2000mAh LiPo battery with a nominal voltage of 3.7V. Converting mAh to Coulombs works out to an ideal energy of 7200 C (2000 * 3.6). This is unlikely to be an exact figure but for this guide, we'll assume it's the ideal energy capacity of the battery.
We can use this value to calculate the percentage capacity of the battery remaining. For example, at 50% remaining capacity, we will have seen 1000mAh pass through the Mojo. Run the following Notecard command, to reset the counter:
> {"req":"card.power","reset":true}
Typically you'll want your host microcontroller to do this to calibrate the Mojo but we can log energy usage entirely without a host (to keep it simple).
Next, you'll need to run the following card request:
> {"req":"card.power","minutes":5}
The minutes
parameter is used to set the logging interval for the Mojo, e.g. it will write to _log.qo
every 5 minutes.
A typical response might look like this:
> {"req":"card.power"}
{
"temperature": 20.111718750000023,
"voltage": 4.202880859375,
"milliamp_hours": -2.6556192
}
If you're charging the battery, you'll see the value for mAh decreasing, hinting that charge is flowing into the battery. Note - If you're using a Notecarrier and are connected over USB, you will likely be charging the battery.
To read more about how the Mojo works and to take a look at the technical details, check out the LTC2959 Datasheet. Not all functionality is exposed to the Notecard, as we've chosen to abstract some of the features to make it easier to use! If you want to directly interact with the Mojo, you can connect to it over I2C.
Configure Notecard to sync _log.qoLet's connect our example to a Notehub project called com.blue.abucknall:mojo_demo (choose your own project name!):
> {"req":"hub.set", "product":"com.blue.abucknall:mojo_demo"}
For this example, we'll configure a Notecard to use continuous mode and sync our outbound Notes every 5 mins to push _log.qo
readings to Notehub.
> {
"req": "hub.set",
"mode": "continuous",
"outbound": 5,
"duration": 240,
"sync": true
}
We can do a final reset on the Mojo to calibrate a fully charged battery:
> {"req":"card.power","reset":true}
Now we can start looking at routing this _log.qo
Notefile into Google Sheets where we can plot the data and see what our battery has been doing!
Head across to Notehub and create a new project.
Now you need to enable syncing of system logs by heading to environment variables and adding a new key _log
to with the value all.
If you now head over to events, you should see some incoming events that look like the following:
Next, let's set up a route to get these Notefiles into Google Sheets.
Go to Routes
and create a new General HTTP/HTTPS Request/Response
route. We'll add some of the prerequisite configuration information and then return once we have a URL to point the route at.
The important drop downs are Filters
which we should configure as above, making sure to include the _log.qo Notefile.
We can also make the data easier to process in Google Sheets by using a JSONata Expression to inject the data into the HTTP request payload.
{
"date": $fromMillis($number($formatBase(when, 10)) * 1000),
"milliamp_hours": $number(body.milliamp_hours),
"voltage": $number(body.voltage)
}
Temporarily disable the route, we'll come back once we've set up Google Sheets.
Setting up Google Sheets for routing NotefilesOpen Google Sheets and create a new document. Head to Extensions
and click through to Apps Script
.
In code.gs paste the following functions:
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
function doPost(e) {
var params = JSON.stringify(e.postData.contents)
params = JSON.parse(params)
var body = JSON.parse(e.postData.contents)
var date = body.date;
var milliamp_hours = body.milliamp_hours;
var voltage = body.voltage;
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Math.max(sheet.getLastRow(),1);
sheet.insertRowAfter(lastRow);
sheet.getRange(lastRow + 1, 1).setValue(date);
sheet.getRange(lastRow + 1, 2).setValue(milliamp_hours);
sheet.getRange(lastRow + 1, 3).setValue(voltage);
SpreadsheetApp.flush();
return ContentService.createTextOutput(JSON.stringify(body))
}
The doPost
function receives an incoming HTTP request, parses the JSON payload, and writes the date, milliamp_hours, and voltage in the next free row of the spreadsheet.
Please be aware that this will be a public (available to anyone) Google Sheet endpoint and as such may pose a security concern. There are methods to secure this but are outside of the scope of this tutorial.
You then need to deploy the apps script as a New Deployment
by clicking the Deploy button in the top right corner. Once you've named it and ensured that it is a Web App
, you should be presented with the following screen:
This is the URL we want to copy back into Notehub. Copy the URL and head back to the Route
page where we were configuring our Google Sheet Route.
Paste this into the configuration of the Google Sheet route and toggle it to enabled.
Visualising Mojo energy consumptionEvery 5 minutes, you should see a new log created into your Google sheet along with the timestamp of the event. I've used this to (roughly) track the battery life of my device and plot a line graph to see the charge and discharge rates. Check it out for yourself!
Here you can see the battery percentage approximated by the cumulative mAh measured out of the battery's total capacity. In this chart, you can see the battery was initially empty and was charged up to 50% before discharging, being unplugged for a while and then charged again.
Disclaimer!
This is NOT an accurate mechanism to measure battery life, there are considerable factors that will impact the performance of a battery, that Mojo cannot accommodate including losses to temperature and humidity.
Mojo can be used to measure energy across it (both charging and discharging) but has no concept of a full or empty battery. It's a great way to determine energy consumption over time, identify potential power spikes and problems as well as get some insight into how your device is behaving in the field.
Conclusion (Tips & Tricks)This project should help you get started on your IoT prototyping journey to easily log your project's energy consumption in the field. Google Sheets is a quick and easy tool to visualise and share data, allowing you to better understand the longevity and life expectancy of your hardware.
You can use Mojo to also monitor for solar charging or any other battery charging mechanisms. You should be aware though that Mojo can only measure charging or discharging that occurs between the battery and your device. Most battery chemistries experience losses due to external factors such as temperature and humidity, you won't be able to measure this with Mojo. As such you may need to adjust/calibrate your charge limits (full/empty) occasionally to ensure you are accurately measuring your battery's capacity.
Comments