Pass variable from main.py to lib class?
misterlisty last edited by robert-hh
/lib/logging.py from machine import RTC class LOGGING: global bdebug global rtc #not woking def logger(msg,showtime=True,level=3,topic="",bdebug=True,debuglevel=3): #error0,warning1,debug2,info3,verbose4 if bdebug and debuglevel>=level: if showtime: print(str(rtc.now())+str(rtc.now())+str(rtc.now())+"T"+str(rtc.now())+str(rtc.now())+str(rtc.now())," | ",msg) # send_msg("outbox",msg) else: print(msg) # send_msg("outbox",msg)
******** NameError: name 'rtc' is not defined ***********************
dough last edited by
A technique to pass object references from main.py to other modules is to create a 'dummy' module to pass items like rtc and i2c. To make this work you first need to create an empty file called 'shared.py'. Then you can use it as follows:
in main.py --
from machine import RTC, I2C
rtc = RCT()
i2c = I2C(0, I2C.MASTER)
shared.rtc = rtc
share.i2c = i2c
Then in the module you want to reference/use rtc or i2c, include an import to shared as follows:
Stevo52 last edited by
@misterlisty Have you got an rtc = RTC() statement in your code? You need to have that to make any calls to rtc.now (Note the upper and lower cases in your code).
utime.localtime() might also be useful to you...