Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Pass variable from main.py to lib class?

    Scheduled Pinned Locked Moved Guides
    rtc
    3 Posts 3 Posters 579 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      misterlisty
      last edited by robert-hh

      I have the following... how do access or pass rtc to the lib class logging.py?
      main.py
      ..
      rtc=RTC()

      
      /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()[0])+str(rtc.now()[1])+str(rtc.now()[2])+"T"+str(rtc.now()[3])+str(rtc.now()[4])+str(rtc.now()[5])," | ",msg)
                      # send_msg("outbox",msg)
                  else:
                      print(msg)
                      # send_msg("outbox",msg)
      

      ******** NameError: name 'rtc' is not defined ***********************

      S D 2 Replies Last reply Reply Quote 0
      • S Offline
        Stevo52 @misterlisty
        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...

        Now registered on this forum as Steve Williams (due to an email address change) - stevo7452@gmail.com

        1 Reply Last reply Reply Quote 1
        • D Offline
          dough @misterlisty
          last edited by

          @misterlisty

          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
          import shared

          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:

          import shared

          shared.rtc.now()
          shared.i2c.scan()

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom