This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
INA219 directly on i2c of Pi without ESP8266
#1
I decided to order an INA219 board and to connect it directly to the pi, and not use an ESP8266 or Arduino to send the values to the Pi, but connect it directly to I2c / SDA and SDL. Hardware works and when I run the following script I get the readings:

Code:
#!/usr/bin/env python

import logging
from ina219 import INA219

SHUNT_OHMS = 0.01
MAX_EXPECTED_AMPS = 10


def read():
    ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS, log_level=logging.INFO)
    ina.configure(ina.RANGE_16V, ina.GAIN_AUTO)

    print("Bus Voltage    : %.3f V" % ina.voltage())
    print("Bus Current    : %.3f mA" % ina.current())
    print("Supply Voltage : %.3f V" % ina.supply_voltage())
    print("Shunt voltage  : %.3f mV" % ina.shunt_voltage())
    print("Power          : %.3f mW" % ina.power())


if __name__ == "__main__":
    read()

Readings:


Code:
2020-02-06 17:25:28,198 - INFO - INA219 gain set to 0.16V
2020-02-06 17:25:28,200 - INFO - INA219 calibrate called with: bus max volts: 16V, max shunt volts: 0.16V, max expected amps: 10.000A
2020-02-06 17:25:28,201 - INFO - INA219 max possible current: 16.000A
2020-02-06 17:25:28,201 - INFO - INA219 max expected current: 10.000A
2020-02-06 17:25:28,202 - INFO - INA219 current LSB: 6.250e-05 A/bit
2020-02-06 17:25:28,202 - INFO - INA219 power LSB: 1.250e-03 W/bit
2020-02-06 17:25:28,203 - INFO - INA219 max current before overflow: 2.0480A
2020-02-06 17:25:28,204 - INFO - INA219 max shunt voltage before overflow: 20.4800mV
2020-02-06 17:25:28,205 - INFO - INA219 calibration: 0xfffe (65534)
Bus Voltage    : 12.464 V
Bus Current    : 108.003 mA
Supply Voltage : 12.453 V
Shunt voltage  : 1.140 mV
Power          : 1542.547 mW


So, the board works.

I can't however for the life of me get these values into SignalK.  The current available tools scripts for INA219 seem to be designed for usage with the ESP8266. What script do I use to use the INA219 directly on i2C of the pi?

Hope someone can help.

Regards,

Jamos
Reply
#2
This is a good candidate to be added to openplotter-i2c app. Do you know what is the most used module?
Reply
#3
(2020-02-06, 06:05 PM)Sailoog Wrote: This is a good candidate to be added to openplotter-i2c app. Do you know what is the most used module?

It is the one from this tutorial: https://www.youtube.com/watch?v=BgShCD7xT_A
Reply
#4
OK, I see. It seems that addresses can be defined but default is 40. the problem is that some extra settings are needed:

SHUNT_OHMS = 0.01
MAX_EXPECTED_AMPS = 10

I should add changes in i2c app to allow extra settings but it will be useful for future sensors. I will try to do something soon.
Reply
#5
Thanks!

The default values for this is:

SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 0.3

But I changed them too 0.01 and 10 because of an extra shunt to measure more current in my setup:

SHUNT_OHMS = 0.01
MAX_EXPECTED_AMPS = 10
Reply
#6
By the way, maybe slightly off topic, but do you ever receive the error "ImportError: cannot import name INA219"

I am having this error with the original INA219.py script downloadable from https://github.com/sailoog/openplotter/b..._ina219.py

Hope you can assist.
Reply
#7
buff I did not remember we had already done this Smile

forget that script, that is for OP1 and pyhon2

For OP2 you have to install ina219 module for python3:

sudo pip3 install pi-ina219
Reply
#8
(2020-02-07, 12:37 PM)Sailoog Wrote: buff I did not remember we had already done this Smile

forget that script, that is for OP1 and pyhon2

For OP2 you have to install ina219 module for python3:

sudo pip3 install pi-ina219

Haha! No worries, happens to the best of us.  Tongue

I am running 1.2.0 alpha, and installed both:

sudo pip install pi-ina219
sudo pip3 install pi-ina219

When trying the corresponding commands I get:

Python command:


Code:
pi@openplotter:~/.openplotter/tools/ina219 $ sudo python ina219.py
Traceback (most recent call last):
  File "ina219.py", line 3, in <module>
    from ina219 import INA219
  File "/home/pi/.openplotter/tools/ina219/ina219.py", line 3, in <module>
    from ina219 import INA219
ImportError: cannot import name INA219

Python3 command:


Code:
pi@openplotter:~/.openplotter/tools/ina219 $ python3 ina219.py
  File "ina219.py", line 19
    print home+'/.openplotter/openplotter_analog.conf'
             ^
SyntaxError: Missing parentheses in call to 'print'

Can I still make it work in 1.2.0. alpha? I can't upgrade that easily to 2.  Sad Have it running on a Pi 4 with 4GB to test, but my main system is still 1.2.0 for a while.

Hope you can help!
Reply
#9
The code example for python3  openplotter 2.0 is:
Code:
#!/usr/bin/env python3

# This file is part of Openplotter.
# Copyright (C) 2020 by sailoog <https://github.com/sailoog/openplotter>
#                     e-sailing <https://github.com/e-sailing/openplotter>
# Openplotter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
# Openplotter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Openplotter. If not, see <http://www.gnu.org/licenses/>.

# This tool should receive data from ina219
# convert the data and send it to the signal k server
# You need to install pi-ina219 (sudo pip3 install pi-ina219)

import socket, time, math, csv, datetime, subprocess, sys, os

op_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..')
sys.path.append(op_folder+'/classes')
from ina219 import INA219
from ina219 import DeviceRangeError

SHUNT_OHMS = 0.1

if len(sys.argv)>1:
    if sys.argv[1]=='settings':
        pass
else:

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    poll_interval = 1000

    ina = INA219(SHUNT_OHMS,1.0,1,0x41)
    ina.configure()

    try:
        inaV = ina.voltage()
        inaA = ina.current()/1000
        inaW = inaV*inaA
    except DeviceRangeError as e:
        print(e)
    
    while True:
        time.sleep(poll_interval*1.0/1000.0)

        try:
            inaV = inaV*0.8 +ina.voltage()*0.2
            inaA = inaA*0.8 +ina.current()/1000*0.2
            inaW = inaV*inaA
        except DeviceRangeError as e:
            print(e)

        SignalK = '{"updates": [{"$source": "OPsensors.I2C.ina219","values":[ '
        Erg=''
        Erg += '{"path": "electrical.batteries.rpi.current","value":'+str(inaA)+'},'
        Erg += '{"path": "electrical.batteries.rpi.voltage","value":'+str(inaV)+'},'
        Erg += '{"path": "electrical.batteries.rpi.power","value":'+str(inaW)+'},'            
        SignalK +=Erg[0:-1]+']}]}\n'
        sock.sendto(SignalK.encode(), ('127.0.0.1', 20220))
Please remember we set i2c address to 0x41 because the standard 0x40 could be used by other devices (HTU21D).
And you have to setup a signalk connection on port 20220.

For 1.2:
I think the library has changed. You have to change line 45 /home/pi/.config/openplotter/tools/analog_ina219/analog_ina219.py to

Code:
    ina = INA219(SHUNT_OHMS,1.0,1,0x41)



There is also a chance to read ina219 by node-red (advantage less cpu power consumption)
cd .signalk/red
npm install ina219
git clone https://github.com/easybotics/node-red-c...ina219.git
cd node-red-contrib-easybotics-ina219
npm install
sudo npm link
This link must be moved or copied to .signalk/red/node_modules
cp /usr/lib/node_modules/node-red-contrib-easybotics-ina219-sensor /home/pi/.signalk/red/node_modules


Code:
[{"id":"9dd53318.b65b58","type":"ina-sensor","z":"413e23b8.c0b474","name":"","handle":"cd4c02a9.2f45b","x":510,"y":120,"wires":[["4ac9724a.23dc94"],["f3918cff.9c8ab8"]]},{"id":"5d9fe981.6e7708","type":"signalk-send-pathvalue","z":"413e23b8.c0b474","name":"","source":"","x":1030,"y":120,"wires":[]},{"id":"f3918cff.9c8ab8","type":"function","z":"413e23b8.c0b474","name":"current&power","func":"msg.topic = \"electrical.batteries.rpi.current\";\nmsg.payload = Number(msg.payload)/1000;\nnode.send(msg);\nmsg.topic = \"electrical.batteries.rpi.power\";\nmsg.payload = Number(flow.get('voltage'))*Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":180,"wires":[["5d9fe981.6e7708"]]},{"id":"4ac9724a.23dc94","type":"function","z":"413e23b8.c0b474","name":"voltage","func":"msg.topic = \"electrical.batteries.rpi.voltage\";\nflow.set('voltage', msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":120,"wires":[["5d9fe981.6e7708"]]},{"id":"cd4c02a9.2f45b","type":"ina-sensor-manager","z":"","address":"0x41","delay":"1000","ohms":"0.01","customResistor":false}]
Reply
#10
Thanks for the clarification e-sailing! Tried the modifications, but still get the ImportError: cannot import name INA219
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)