Search code examples
python-3.xclassadafruit-circuitpython

Storing and reading data from module class


I am trying to use one module to read data from a sensor board, put that data into a class, than print that data in another module. I'm not sure if I am following best practices here or if there is a better way to do this. Currently I am getting the following error and I'm not sure how to correct.

Traceback (most recent call last):
  File "app/conductor.py", line 15, in <module>
    accel_x, accel_y, accel_z = nineDOF.Acceleration()
TypeError: cannot unpack non-iterable NoneType object

conductor.py:

import time
import datetime
import dht11
import max31865
import lsm9ds1

while True:
    externalReading = max31865.temp()
    print("External Temp: {0:0.3f}C".format(externalReading))

    internalTemp, internalHumidity = dht11.readings()
    print("Internal Temp 1: {:.3f}C  Humidity: {}% ".format(internalTemp, internalHumidity))

    nineDOF = lsm9ds1.get()
    accel_x, accel_y, accel_z = nineDOF.Acceleration()
    mag_x, mag_y, mag_z = nineDOF.Magnetometer()
    gyro_x, gyro_y, gyro_z = nineDOF.Gyroscope()
    internalTemp2 = nineDOF.temperature()
    print(
        "Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
            accel_x, accel_y, accel_z
        )
    )
    print(
        "Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(mag_x, mag_y, mag_z)
    )
    print(
        "Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
            gyro_x, gyro_y, gyro_z
        )
    )
    print("Internal Temp 2: {0:0.3f}C".format(internalTemp2))

lsm9ds1.py:

import board
import busio
import adafruit_lsm9ds1

# SPI connection:
from digitalio import DigitalInOut, Direction

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
csag = DigitalInOut(board.D5)
csag.direction = Direction.OUTPUT
csag.value = True
csm = DigitalInOut(board.D6)
csm.direction = Direction.OUTPUT
csm.value = True
sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)


class Readings:
    def __init__(self):
        pass

    def Acceleration(self):
        x, y, z = sensor.acceleration

    def Magnetometer(self):
        x, y, z = sensor.magnetic

    def Gyroscope(self):
        x, y, z = sensor.gyro

    def temperature(self):
        x = sensor.temperature


def get():
    return Readings()


Solution

  • Your immediate issue is the methods in the Readings class have no return values and Python's default return value when there is no explicit return statement is None. There are a few languages where the last value in a function is also the return value - R, Perl and Bourne Shell come to mind - this may be the source of confusion here. There's a discussion on this in return, return None, and no return at all?

    The implicit None return value from the Acceleration method cannot be unpacked into a three element tuple. If you add a return x, y, z at the end of that method it should work.

    In terms of class and program structure it depends what your aim is.