Page 1 of 2
help please class error
Posted: Tue Aug 16, 2022 12:14 pm
by iceelon
hello again, I think I've messed up in a way ....
You will see a few days ago I started to use three libraries (classes bmp280, bh1750, spg30) for a routine control of an irrigation system...
when i just declare a class it works perfectly...
the problem is when i initialize the others to pass to the wrapper class...
It tells me that the temperature instance is not declared in the bh1750... and it is like this...
I haven't worked with phyton for a long time and the truth is I find myself a bit lost...
Code: Select all
class IntegrateSensor :
def __init__(self):
self.humidity=0
self.temperature =0
self.lux=0
def set_humidity(self):
self.humidity=int(ahtx.read_humidity()) (this ath10 class)
def set_temperature(self):
self.temperature=int(ahtx.read_temperature())(this ath10 class)
def set_lux(self):
self.lux=int(BH.luminance()) (this bh class bh1750)
def get_humidity(self):
return self.humidity
def get_temperature(self):
return self.temperature
def get_lux(self):
return self.lux
this print (set_lux()) is none

thank
Re: help please class error
Posted: Tue Aug 16, 2022 12:35 pm
by TheSilverBullet
You need to create an instance first.
Try:
Code: Select all
igs = IntegrateSensor()
igs.set_lux()
Re: help please class error
Posted: Tue Aug 16, 2022 1:48 pm
by iceelon
ummm... not ...
code ;
Code: Select all
from machine import SoftI2C, Pin
from aht10 import AHT10
import time
import sys
from bh1750 import BH1750
i2c = SoftI2C(scl=machine.Pin(17), sda=machine.Pin(16))
# class aht10 in file ath10.py
ahtx = AHT10(i2c)
ahtx.sensor_init()
ahtx.is_calibration_enabled()
#class bh1750 in file bh1750.py
BH = BH1750(i2c)
class IntegrateSensor :
def __init__(self):
self.humidity=0
self.temperature =0
self.lux=0.0
def set_humidity(self):
self.humidity=int(ahtx.read_humidity())
def set_temperature(self):
self.temperature=int(ahtx.read_temperature())
def set_lux(self):
self.lux=BH.luminance()
def get_humidity(self):
return self.humidity
def get_temperature(self):
return self.temperature
def get_lux(self):
return self.lux
igs =IntegrateSensor ()
print (igs.lux)
print (igs.get_humidity())
print (igs.get_temperature())
print( "this not class" )
print(ahtx.read_humidity())
print(ahtx.read_temperature())
print(BH.luminance())
result:
Code: Select all
MicroPython v1.19.1 on 2022-06-18; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
0.0 (lux of class)
0 (humidity class)
0 (temperature class)
#### this not class
37.37612 (humidity not class)
28.80363 (temperature not class)
11.66667 (lux not class)
>>>
Re: help please class error
Posted: Tue Aug 16, 2022 6:07 pm
by karfas
Why do you think that self.humidity has any value when it isn't set by a call to get_humidity first?
Re: help please class error
Posted: Tue Aug 16, 2022 8:03 pm
by iceelon
because simply calling them, they return values, as I show at the end
Re: help please class error
Posted: Tue Aug 16, 2022 9:25 pm
by karfas
Don't see this in the code.
Your get_xxx calls return the value of an instance variable (e.g. self.lux). However, this self.lux is set only in __init__() and set_lux().
As set_lux() never gets called (in your comparison of with/without class), get_lux() happily returns the value set during __init__().
Re: help please class error
Posted: Tue Aug 16, 2022 11:19 pm
by iceelon
the really strange thing is that eliminating the second class, for example bh1750 works, if I limit myself to capturing the set as it shows the result of the created class and the correct result, everything is perfect, perhaps passing as parameters in the __init__ but for what I observe it is a problem of the interpreter when working with several classes and instances...
Re: help please class error
Posted: Tue Aug 16, 2022 11:41 pm
by karfas
There is no problem with the interpreter.
YOU simply don't understand how classes work and YOU don't read or understand the answers you got here.
It's all there:
TheSilverBullet wrote: ↑Tue Aug 16, 2022 12:35 pm
You need to create an instance first.
Try:
Code: Select all
igs = IntegrateSensor()
igs.set_lux()
Where the hell is the call to set_lux() in your code ???
Re: help please class error
Posted: Wed Aug 17, 2022 2:56 am
by jimmo
karfas wrote: ↑Tue Aug 16, 2022 11:41 pm
YOU simply don't understand how classes work and YOU don't read or understand the answers you got here.
karfas, I appreciate your help responding to posts here, but please remember to keep your responses polite and friendly.
iceelon wrote: ↑Tue Aug 16, 2022 11:19 pm
for what I observe it is a problem of the interpreter when working with several classes and instances...
iceelon, I'm a little bit unsure what you're actually trying to do with your IntegrateSensor class so it's difficult to give specific advice.
That said, karfas is right, there's nothing wrong with the interpreter here, the code is behaving as expected. Instances of IntegrateSensor need to have set_humidity and set_temperature etc called on them before calling get_humidity etc will do anything.
Maybe you want something like this
Code: Select all
class IntegrateSensor :
def __init__(self):
self.humidity=0
self.temperature =0
self.lux=0.0
def update(self, bh, ahtx):
self.humidity=int(ahtx.read_humidity())
self.temperature=int(ahtx.read_temperature())
self.lux=BH.luminance()
def get_humidity(self):
return self.humidity
def get_temperature(self):
return self.temperature
def get_lux(self):
return self.lux
igs =IntegrateSensor()
# Call this whenever you want to get new sensor data.
igs.update(BH, ahtx)
# Use get_lux, etc whenever you want to access the current data.
print(igs.get_lux())
Re: help please class error
Posted: Wed Aug 17, 2022 6:49 am
by iceelon
fence...
First of all thanks karfas, and if I understand what you say but .....
let's go by parts, I think my request was not understood, or my English is so "bad" that ...
the first thing I try to do;
It turns out that I have several classes to handle "sensors" and the truth is that combining them in a single class is easier to use...
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 what I don't understand is when I try to capture from two different classes it doesn't work
I seem to remember that to perform multiple inheritance it was necessary to tell the containing class the classes it would inherit from, in this case they would be BH and ahtx
like
Code: Select all
class IntegrateSensor ([Object],[...nObject] :
by not "referring" the classes to the containing class, it only "inherits" one, the advantage is that it does not need to be "refreshed" as shown by jimmo, it is a solution but you would have to call update every time it was necessary to read the sensors
As karfas, if I have read you and you are right, it was missing .... but (not to excuse myself) I already provided that solution ...
Jimmo ...As for karfa's answers, I don't feel offended or bothered, ... at some point we have all gotten out of the pot because we thought we were not being heard ...