help please class error

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: help please class error

Post by TheSilverBullet » Wed Aug 17, 2022 7:48 am

Maybe that's what you're looking for…

Code: Select all

class IntegrateSensor:
    def __init__(self, scl, sda):
        self.i2c = SoftI2C(scl=scl, sda=sda)
        self.ahtx = AHT10(self.i2c)
        self.ahtx.sensor_init()
        self.bh = BH1750(self.i2c)
       
    def get_humidity(self):
        return int(self.ahtx.read_humidity())
    
    def get_temperature(self):
        return int(self.ahtx.read_temperature())

    def get_lux(self):
        return self.bh.luminance()

igs = IntegrateSensor(machine.Pin(17), sda=machine.Pin(16))
print(igs.get_humidity())
print(igs.get_temperature())
print(igs.get_lux())

iceelon
Posts: 30
Joined: Tue Jul 12, 2022 11:42 am

Re: help please class error

Post by iceelon » Wed Aug 17, 2022 8:09 am

perfect!!!!!!!!!!!, thank you very much ...

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: help please class error

Post by karfas » Wed Aug 17, 2022 10:00 am

iceelon wrote:
Wed Aug 17, 2022 6:49 am
...
you are right there is no get_lux() instance yet it is still 0 the code below
...
I think the assignment of the set... get is fine, but
...
I seem to remember that to perform multiple inheritance ...
No. You still don't get it. You must read the values from the sensors at some point in time. With your original class, this requires a call to (e.g.) set_lux() from somewhere (either from outside of the class or from inside).

This has nothing to do with fancy inheritance concepts.
You read a sensor value into the variable self.lux. This is done by calling igs.set_lux() (from outside) or by calling self.set_lux() (from inside the class).
I strongly recommend you read some basic python tutorials, especially the parts about variables and classes. I have no idea how to explain this in a better/simpler way.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

iceelon
Posts: 30
Joined: Tue Jul 12, 2022 11:42 am

Re: help please class error

Post by iceelon » Wed Aug 17, 2022 10:09 am

thanks, I'm working on it... (learning or at least I'm trying to), thanks for your advice and I'm sorry if I've offended you... nothing could be further from the truth

Post Reply