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.
iceelon
Posts: 30
Joined: Tue Jul 12, 2022 11:42 am

help please class error

Post by iceelon » Tue Aug 16, 2022 12:14 pm

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

TheSilverBullet
Posts: 50
Joined: Thu Jul 07, 2022 7:40 am

Re: help please class error

Post by TheSilverBullet » Tue Aug 16, 2022 12:35 pm

You need to create an instance first.
Try:

Code: Select all

igs = IntegrateSensor()
igs.set_lux()

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

Re: help please class error

Post by iceelon » Tue Aug 16, 2022 1:48 pm

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)
>>> 

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

Re: help please class error

Post by karfas » Tue Aug 16, 2022 6:07 pm

Why do you think that self.humidity has any value when it isn't set by a call to get_humidity first?
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 » Tue Aug 16, 2022 8:03 pm

because simply calling them, they return values, as I show at the end

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

Re: help please class error

Post by karfas » Tue Aug 16, 2022 9:25 pm

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__().
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 » Tue Aug 16, 2022 11:19 pm

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...

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

Re: help please class error

Post by karfas » Tue Aug 16, 2022 11:41 pm

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 ???
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: help please class error

Post by jimmo » Wed Aug 17, 2022 2:56 am

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())

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

Re: help please class error

Post by iceelon » Wed Aug 17, 2022 6:49 am

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 ...

Post Reply