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
#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


Messages In This Thread
RE: INA219 directly on i2c of Pi without ESP8266 - by e-sailing - 2020-02-09, 10:25 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)