ZSupreme
ZSupreme

Reputation: 11

I have been trying to create a csv file from python code on raspberry pi that can measure voltage, current, power, shunt voltage from ina219 sensor

I have been trying to create a csv file from a python code that is able to track the bus Voltage, bus current, power, and shunt voltage from a INA219 sensor that is connected to the raspberry pi3. This is the code that I have been working:

**#Importing libraries
import csv
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.1
def read():
    ina = INA219(SHUNT_OHMS) #Measures the ina219
    ina.configure()
def measuring_things():
    try:
         print("Bus Voltage: %.3f V" % ina.voltage())
         print("Bus Current: %.3f mA" % ina.current())
         print("Power: %.3f mW" % ina.power())
         print("Shunt voltage: %.3f mV" % ina.shunt_voltage())
    except DeviceRangeError as e:
    # Current out of device range with specified shunt resistor  
        print(e)
    
f = open("SensorData.csv", "w", newline="") #Gives your .csv file a name
rc = csv.writer(f)
rc.writerow(["Distance from Sensor"]) #Heading
fields = ['Bus Voltage', 'Bus Current', 'Power', 'Shunt Voltage'] 
          
rc.writerow([read()])
rc.writerow(measuring_things())
f.close()** 

I have changed the code around multiple times alongside created other codes and I keep getting an error from the ina.voltage, ina.current part no matter what I do. I keep getting an error saying:

============= RESTART: /home/pi/Downloads/scripts/SensorData.py =============
Traceback (most recent call last):
  File "/home/pi/Downloads/scripts/SensorData.py", line 34, in <module>
    rc.writerow(measuring_things())
  File "/home/pi/Downloads/scripts/SensorData.py", line 16, in measuring_things
    print("Bus Voltage: %.3f V" % ina.voltage())
NameError: name 'ina' is not defined

I am getting irritating errors, they could be minor but I just feel stuck. My goal is to create a cvs file that is similar to this information :

============== RESTART: /home/pi/Downloads/scripts/Autogain.py ==============
Bus Voltage: 0.876 V
Bus Current: -0.098 mA
Power: 0.000 mW
Shunt voltage: -0.010 mV

This output I attached was done using python on the Raspberry pi 3 (with the INA219 sensor connected to it) by itself and not in hand converting it into a csv file. The Rapberry system reads and is able to sense the INA219 sensor so I am not sure what is going on.

Upvotes: 0

Views: 162

Answers (1)

smcrowley
smcrowley

Reputation: 491

The "ina" variable is local, so the measuring_things() method doesn't have access to it

If your goal is to have a program that measures every so often and records those, file stuff goes inside the method that gets called on a timer.

import csv
from ina219 import INA219
from ina219 import DeviceRangeError
import threading # for the timer
SHUNT_OHMS = 0.1


def measure_things():
    threading.Timer(5.0, printit).start() #run method every 5 seconds
    ina = INA219(SHUNT_OHMS)
    ina.configure()
    newRow=[]
    newRow.append("Bus Voltage: %.3f V" % ina.voltage())
    try:
        newRow.append("Bus Current: %.3f mA" % ina.current())
        newRow.append("Power: %.3f mW" % ina.power())
        newRow.append("Shunt voltage: %.3f mV" % ina.shunt_voltage())
    except DeviceRangeError as e:
        # Current out of device range with specified shunt resistor
        print(e)
    # save row to csv file
    with open(r'name', 'a') as f:
        writer = csv.writer(f)
        writer.writerow(newRow)


measure_things()
#continue with other code if there is any

If your goal is to just measure once, remove threading

Upvotes: 2

Related Questions