Posts: 205
Threads: 31
Joined: Apr 2017
Reputation:
2
i don't know about python for a ina219. But in C you can set the device. shunt value, max amps, max main voltage, bits and sample rates. If the shunt on the module is left in place you need to calculate the total shunt value as resistors in parallel.
The ina219 module can only handle 3.2 amps.
Code: ina219.begin();
// setting up our configuration
// default values are RANGE_32V, GAIN_8_320MV, ADC_12BIT, ADC_12BIT, CONT_SH_BUS
ina219.configure(INA219::RANGE_16V, INA219::GAIN_1_40MV, INA219::ADC_128SAMP, INA219::ADC_128SAMP, INA219::CONT_SH_BUS);
// calibrate with our values
ina219.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);
Posts: 240
Threads: 45
Joined: Mar 2018
Reputation:
1
2020-02-10, 10:03 AM
(This post was last modified: 2020-02-10, 10:25 AM by jamos.tan@gmail.com.)
(2020-02-09, 11:22 PM)jamos.tan@gmail.com Wrote: Thanks for the clarification e-sailing! Tried the modifications, but still get the ImportError: cannot import name INA219
Ok, I have now set up 2 scripts, one for python2 and one for python3.
I have also tried uninstalling the libraries by doing a sudo pip uninstall pi-ina219 and sudo pip3 uninstall pi-ina219
Python 2 script in my case looks like the following:
- uncommented the ads1115 lines as I don't have the module, and thus cannot be found
- changed the address to 40, I don't have any other i2c devices at that address
- I use a shunt to measure more amps, so I changed this to 0.01
- Changed line 45 like previously suggested
Code: #!/usr/bin/env python
# This file is part of Openplotter.
# Copyright (C) 2015 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/>.
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 ads1115 import Ads1115
# from conf_analog import Conf_analog
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.01
conf_analog=Conf_analog()
home = conf_analog.home
if len(sys.argv)>1:
if sys.argv[1]=='settings':
print home+'/.openplotter/openplotter_analog.conf'
subprocess.Popen(['leafpad',home+'/.openplotter/openplotter_analog.conf'])
exit
else:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
poll_interval = 1
rate_analog = 1
tick_analog=time.time()
ina = INA219(SHUNT_OHMS,1.0,1,0x40)
ina.configure()
try:
inaV = ina.voltage()
inaA = ina.current()/1000
inaW = inaV*inaA
except DeviceRangeError as e:
print e
while True:
tick2=time.time()
time.sleep(poll_interval*1.0/1000.0)
#GENERATE
if tick2-tick_analog > rate_analog:
tick_analog=time.time()
list_signalk_path1=[]
list_signalk1=[]
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, ('127.0.0.1', 55557))
This gives me the error:
Code: pi@openplotter:~/.openplotter/tools/ina219 $ sudo python ina219.py
Traceback (most recent call last):
File "ina219.py", line 24, in <module>
from ina219 import INA219
File "/home/pi/.openplotter/tools/ina219/ina219.py", line 24, in <module>
from ina219 import INA219
ImportError: cannot import name INA219
While testing this, I have uninstalled all pi-219 libraries and then going step by step, installing the correct version for the python script.
In this way I know there might be a possible python conflict, if such a thing exists.
While this didn't work, I figured I'd go ahead with the Python 3 script.
So, while having installed the correct library, I went on testing with the following script.
Python 3 code:
- This script doesn't include the ADS1115 lines, so didn't have to comment them out.
- Changed the address to 40
- Changed the ohms to 0.01
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,0x40)
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))
This gives me the error:
Code: pi@openplotter:~/.openplotter/tools/ina219 $ sudo python3 ina219test.py
Traceback (most recent call last):
File "ina219test.py", line 26, in <module>
from ina219 import INA219
File "/home/pi/.openplotter/tools/ina219/ina219.py", line 34
print home+'/.openplotter/openplotter_analog.conf'
^
SyntaxError: Missing parentheses in call to 'print'
As I understand, this is an information message from python 3, to inform the user there is some old python languages:
https://stackoverflow.com/questions/2544...-in-python
Weird, but lines 26 and 34 don't point to the code the error suggest, so I guess they mean this part:
Code: print home+'/.openplotter/openplotter_analog.conf'
^
So, when following stackoverflow I need the add parenthesis to the following code, but I already see them listed?
Code: op_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..')
When changing to this:
Code: op_folder = ((os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..')
sys.path.append(op_folder+'/classes')
I get the error:
Code: File "ina219test.py", line 6
sys.path.append(op_folder+'/classes')
^
SyntaxError: invalid syntax
So it seems I ave solved the op_folder issue, but not the sys.path.append.
Extra information, I have these python versions:
Code: pi@openplotter:~/.openplotter/tools/ina219 $ python3 -V
Python 3.5.3
pi@openplotter:~/.openplotter/tools/ina219 $ python -V
Python 2.7.13
Posts: 240
Threads: 45
Joined: Mar 2018
Reputation:
1
(2020-02-09, 10:25 PM)e-sailing Wrote: 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}]
Hi e-sailing… wanted to thank you again. I was getting a headache from Python, and avoided node-red some time. Decided to try it, and it works. Thanks so much!
Posts: 76
Threads: 14
Joined: Nov 2019
Reputation:
2
(2020-02-06, 06:09 PM)jamos.tan@gmail.com Wrote: (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
Just for all, here a clear deployment for this chip:
https://www.rototron.info/raspberry-pi-ina219-tutorial/
Bart
Posts: 3,038
Threads: 63
Joined: Mar 2016
Reputation:
289
Posts: 240
Threads: 45
Joined: Mar 2018
Reputation:
1
(2020-11-02, 07:44 PM)Sailoog Wrote: added support for INA260 and more to openplotter: https://forum.openmarine.net/showthread.php?tid=3055
Nice, considering the changes from ina219, will this also work for the ina219 if I select it in i2c?
So, backwards compatible?
Posts: 3,038
Threads: 63
Joined: Mar 2016
Reputation:
289
I do not think so. The ina219 needs extra parameters
|