• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

AHT20 Humidity + Temperature Sensor

Scheduled Pinned Locked Moved Solved Requests
18 Posts 3 Posters 510 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.
  • R Offline
    rkorell @rkorell
    last edited by rkorell Apr 16, 2025, 10:38 AM Apr 16, 2025, 10:35 AM

    @JohnGalt
    just seen while re-reading my last posting:

    nodehelper.js (the part of the module which is communicating with the python program and so with the sensor) is calling python3 :

    exec(`python3 ./modules/MMM-BME280/bme280.py ${deviceAddr}`, (error, stdout) => {
    

    but the python script itself is referencing python 2 (see above post, source code of bme280.py, 1st line:

    #!/usr/bin/python
    

    I guess this must match and doesn’t …
    So may this is the cause of your problems.

    And if this is true it is may really hard to get this working because you may run in several incompatibilities and library-conflicts …

    Regards,
    Ralf

    1 Reply Last reply Reply Quote 0
    • J Offline
      JohnGalt @sdetweil
      last edited by Apr 16, 2025, 9:47 PM

      @sdetweil – Thanks, Sam. It looks like I intended to send bme280.py but actually copied MMM-BME280.js. I see below where Ralf did post it, so I will look at that, too.

      1 Reply Last reply Reply Quote 1
      • J Offline
        JohnGalt @rkorell
        last edited by Apr 16, 2025, 9:57 PM

        @rkorell – Hi Ralf:

        Yes, I2C is enabled and functioning. There is a test script that does return current temperature and humidity:
        Invoking “python AHT20_test.py” does launch this script (Note - I launch from the terminal using ‘python’, not ‘python3’, telling me the system is indeed defaulting to python3):

        # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
        # SPDX-License-Identifier: MIT
        
        """
        Basic `AHTx0` example test
        """
        
        import time
        import board
        import adafruit_ahtx0
        
        # Create sensor object, communicating over the board's default I2C bus
        i2c = board.I2C()  # uses board.SCL and board.SDA
        # i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
        sensor = adafruit_ahtx0.AHTx0(i2c)
        
        while True:
            print("\nTemperature: %0.1f C" % sensor.temperature)
            print("Humidity: %0.1f %%" % sensor.relative_humidity)
            time.sleep(2)
        
        1 Reply Last reply Reply Quote 0
        • J Offline
          JohnGalt @rkorell
          last edited by Apr 17, 2025, 1:01 AM

          @rkorell + @sdetweil - Thanks for the support. I think I have fixed this for myself by adapting an existing module (“MMM-Temperature”, // https://github.com/Tom-Hirschberger/MMM-Temperature).

          That module calls scripts for various sensors to capture the data. I was able to adapt an existing script for use with this sensor, so I think I am good for now.

          R 1 Reply Last reply Apr 17, 2025, 6:25 AM Reply Quote 2
          • R Offline
            rkorell @JohnGalt
            last edited by Apr 17, 2025, 6:25 AM

            @JohnGalt cool.
            congratulations.
            It’s may be worth to show the community what you have done?

            Regards,
            Ralf

            J 1 Reply Last reply Apr 17, 2025, 4:24 PM Reply Quote 0
            • J Offline
              JohnGalt @rkorell
              last edited by Apr 17, 2025, 4:24 PM

              @rkorell Yes, I can describe how I arrived at a resolution for my particular problem - which was to replace DHTxx sensors with the more accurate AHT20 temperature and humidity sensors.

              Caveat: I do not necessarily recommend this for the average person though, because I did not find Magic Mirror modules that natively support this sensor, and as a result had to make changes outside of the config.js and custom.css files. As usual, this puts me at risk of something breaking when the module is updated.

              The module being used (MMM-Temperature - found at https://github.com/Tom-Hirschberger/MMM-Temperature) utilizes python scripts to capture the data from the sensor. While the module is quite complete, with script support for many sensors, I did create a new script in order to use this particular sensor.

              The sensor is supported by Adafruit (https://learn.adafruit.com/adafruit-aht20/python-circuitpython), with instructions to install various adafruit libraries including adafruit-ahtx0 (sudo pip3 install adafruit-circuitpython-ahtx0).

              Using those libraries, your script needs to include the following instructions:

              import board
              import adafruit_ahtx0
              sensor = adafruit_ahtx0.AHTx0(board.I2C())
              

              One of the existing scripts supports I2C sensors using the Adafruit libraries, so it looked like a good candidate to use as a model. See htu21:

              #!/usr/bin/env python3
              #pip3 install adafruit-circuitpython-htu21d
              import board
              from adafruit_htu21d import HTU21D
              import json
              
              result = {}
              try:
                  # Create sensor object, communicating over the board's default I2C bus
                  i2c = board.I2C()  # uses board.SCL and board.SDA
                  sensor = HTU21D(i2c)
                  result["temperature_c"] = sensor.temperature
                  result["humidity"] = sensor.relative_humidity
                  result["temperature_f"] = (result["temperature_c"]*1.8) + 32
                  result["error"] = False
              except:
                  result["temperature_c"] = 0.0
                  result["humidity"] = 0.0
                  result["temperature_f"] = (result["temperature_c"]*1.8) + 32
                  result["error"] = True
              
              print(json.dumps(result))
              

              My resulting script is aht20:

              #!/usr/bin/env python3
              # aht20: Modeled on htu21 
              # pip3 install adafruit-circuitpython-htu21d
              # pip3 install adafruit-circuitpython-ahtx0
              import board
              import adafruit_ahtx0
              import json
              
              result = {}
              try:
                  # Create sensor object, communicating over the board's default I2C bus
                  i2c = board.I2C()  # uses board.SCL and board.SDA
                  # sensor = HTU21D(i2c)
                  sensor = adafruit_ahtx0.AHTx0(board.I2C())
                  result["temperature_c"] = sensor.temperature
                  result["humidity"] = sensor.relative_humidity
                  result["temperature_f"] = (result["temperature_c"]*1.8) + 32
                  result["error"] = False
              except:
                  result["temperature_c"] = 0.0
                  result["humidity"] = 0.0
                  result["temperature_f"] = (result["temperature_c"]*1.8) + 32
                  result["error"] = True
              
              print(json.dumps(result))
              

              This module is now displaying the temperature and humidity from the sensor directly connected it it. Now all I have to do is figure out the complicated css and get it to look like the other modules on my Magic Mirror.

              Barring any objections, I will mark this as solved.

              S R 2 Replies Last reply Apr 17, 2025, 4:34 PM Reply Quote 1
              • S Away
                sdetweil @JohnGalt
                last edited by Apr 17, 2025, 4:34 PM

                @JohnGalt css, use the developers window

                see the second link in my signature below

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                J 1 Reply Last reply Apr 17, 2025, 4:47 PM Reply Quote 0
                • R Offline
                  rkorell @JohnGalt
                  last edited by rkorell Apr 17, 2025, 4:34 PM Apr 17, 2025, 4:34 PM

                  @JohnGalt Really COOL!
                  Thanks for sharing!
                  Ralf

                  J 1 Reply Last reply Apr 17, 2025, 4:47 PM Reply Quote 0
                  • J Offline
                    JohnGalt @sdetweil
                    last edited by Apr 17, 2025, 4:47 PM

                    @sdetweil – Thanks for the reminder, I’ll take a look.

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      JohnGalt @rkorell
                      last edited by Apr 17, 2025, 4:47 PM

                      @rkorell – My pleasure.

                      1 Reply Last reply Reply Quote 1
                      • J JohnGalt has marked this topic as solved on Apr 17, 2025, 4:48 PM
                      • 1
                      • 2
                      • 2 / 2
                      2 / 2
                      • First post
                        13/18
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy