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