A simple current sensor visualization app

INA219_RPi_VBNet

So what do you do when you want to buy a uCurrent Gold but it’s not in stock? Search for a (temporary) alternative. So that’s what I did and the screenshot above shows what this alternative produces 🙂

An Adafruit INA219 Breakout Board, a Raspberry Pi, a Python script for doing the i2c to the INA219 and MQTT on the RPi, an MQTT broker and a VB.Net app on my Windows machine to collect the data and visualize it with a Chart control. (BTW, the uCurrent Gold has been ordered just a few hours ago – it’s in stock again!)

Most of the time was lost on trying to get the INA219 to work with a Devantech USB-ISS but I didn’t manage to get it working for some strange reason. I even used a BugLogic to analyze the i2c traffic – all I saw were NAK‘s and the USB-ISS I2C_TEST command always returned 0 too; weird.

After I decided to drop the USB-ISS for this and moved to a Raspberry Pi everything went much better. Enabling i2c on the RPi, Python (pip), installing mosquitto, INA219 Python library and I was almost ready – but still not knowing whether this would actually produce something useful…?

The example Python script only needed to be changed a bit:

#!/usr/bin/python

from Subfact_ina219 import INA219
import time
import mosquitto

print 'Starting ...'

def on_connect(self, rc, res):
  print 'Connected'

#On recipt of a message create a pynotification and show it
def on_message(self, data, msg):
  print("%s, %s" % (msg.topic, msg.payload))

client = mosquitto.Mosquitto("ina219", clean_session=True)
#define the callbacks
client.on_message = on_message
client.on_connect = on_connect

client.connect("192.168.10.179", 1883)

ina = INA219(0x41)
client.subscribe("inacommand", 2)

while True:
  try:
    value = ina.getCurrent_mA()
    client.publish("inadata", "%.5f" % value  , 0)

  except KeyboardInterrupt:
    print ' Exiting...Keyboard interrupt'
    break
    
  except:
    print 'unexpected error'

And I added a function to set the INA219 to max 16V and 400 mA and made that the default.

One item left: a tool to collect and visualize the measurements done by the INA219. Whenever I need a quick & dirty app with a form and some buttons I prefer VB.Net – it’s the easiest & quickest way to do small tasks like this. 130 lines of VB code is all it takes… I can expand this app with a lot of ‘extras’  like triggers, setting the INA219 to one of its 3 calibration modes, whether it should publish shunt-, bus- or current measurements and so on. Communicating with the INA219 connected to the RPi is simple, cause it’s done with MQTT 😉 But for now I think this app will suffice.

2-3 hours later I had the app finished for some first results as can be seen in the screenshot @ the top of this post. Apparently the VB.Net receives almost 2800 samples in a period of 5 seconds; that’s about 560 samples/second. Not bad… the screenshot is taken while the INA219 was measuring the current flowing through a 4.7kΩ resistor @ 5V.

Now it’s time to hook up a ESP8266 to the INA219 and start sampling! Don’t know when that will be though …

 

Bookmark the permalink.

3 Responses to A simple current sensor visualization app

  1. Harald Klein says:

    we should use the same lua script so that we can compare results. I was trying to measure today, but the scope impedance introduced errors (set to 50, as with 1M I got no sane measurement). Will take to someone with clue and try again tomorrow.

  2. Harald says:

    I got feedback on the eevblog forums, that made me rethink my test setup and I realised that the serial GND connection might interfere here (DUT should be floating). I’ll remeasure without USB serial tomorrow.

Leave a Reply

Your email address will not be published. Required fields are marked *