Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0

IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO

Image for user brubell
published February 11, 2019, last edited January 22, 2025
Save Link Note Download
20
Beginner
Project guide

Python Code

This guide assumes that you've gotten your Raspberry Pi up and running, and have CircuitPython installed.

Installing Fonts

This project uses a Microsoft TrueType Font. To install it,enter the following into the terminal on your Raspberry Pi:

sudo apt-get install ttf-mscorefonts-installer

Installing CircuitPython Libraries

Since CircuitPython is running on the Raspberry Pi - installing the libraries for this guide is quick and easy.

To install the library for the displayenter the following into the terminal

sudo pip3 install adafruit-circuitpython-ssd1306

You'll also need toinstall the CircuitPython library to read data from the Analog Devices ADT7410

sudo pip3 install adafruit-circuitpython-adt7410

The code for this project is below.Save it to the Raspberry Pi asadafruit_io_adt7410.py

# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries## SPDX-License-Identifier: MIT"""'adafruit_io_adt7410.py'==================================Example of sending temperaturevalues to an Adafruit IO feed usingan ADT7410 breakout.Dependencies:    - Adafruit_Blinka        (https://github.com/adafruit/Adafruit_Blinka)    - Adafruit_CircuitPython_SSD1306        (https://github.com/adafruit/Adafruit_CircuitPython_SSD1306)    - Adafruit_CircuitPython_ADT7410        (https://github.com/adafruit/Adafruit_CircuitPython_ADT7410)"""# Import standard python modulesimport time# import Adafruit SSD1306 OLEDfrom PIL import Image, ImageDraw, ImageFontimport adafruit_ssd1306# import Adafruit IO REST clientfrom Adafruit_IO import Client# import Adafruit CircuitPython adafruit_adt7410 libraryimport adafruit_adt7410# import Adafruit Blinkaimport boardimport busioimport digitalio# Delay between sensor reads, in secondsDELAY_SECONDS = 30# Set to your Adafruit IO key.# Remember, your key is a secret,# so make sure not to publish it when you publish this code!ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY'# Set to your Adafruit IO username.# (go to https://accounts.adafruit.com to find your username)ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME'# Create an instance of the REST clientaio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)# Set up `temperature` feedpi_temperature = aio.feeds('temperature')# Set up OLEDi2c_bus = busio.I2C(board.SCL, board.SDA)oled_reset = digitalio.DigitalInOut(board.D21)disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c_bus, reset=oled_reset)# Clear display.disp.fill(0)disp.show()# Create blank image for drawing.# Make sure to create image with mode '1' for 1-bit color.width = disp.widthheight = disp.heightimage = Image.new('1', (width, height))# Get drawing object to draw on image.draw = ImageDraw.Draw(image)# `sudo apt-get install ttf-mscorefonts-installer` to get these fontsfont_big = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 16)font_small = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 12)adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)adt.high_resolution = Truetime.sleep(0.25)  # wait for sensor to boot up and get first readingwhile True:    # clear screen    draw.rectangle((0, 0, width, height), outline=0, fill=0)    # Read the temperature sensor    tempC = adt.temperature    tempC = round(tempC, 2)    # display the temperature on the OLED    draw.text((0, 0), "Temp: %0.2F *C" % tempC, font=font_big, fill=255)    # Send temperature to Adafruit IO    print('Sending temperature {0} C to Adafruit IO'.format(tempC))    draw.text((0, 16), "Sending...", font=font_small, fill=255)    disp.image(image)    disp.show()    aio.send(pi_temperature.key, tempC)    draw.text((0, 16), "Sending...Done!", font=font_small, fill=255)    disp.image(image)    disp.show()    # Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io    time.sleep(DELAY_SECONDS)
# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries## SPDX-License-Identifier: MIT"""'adafruit_io_adt7410.py'==================================Example of sending temperaturevalues to an Adafruit IO feed usingan ADT7410 breakout.Dependencies:    - Adafruit_Blinka        (https://github.com/adafruit/Adafruit_Blinka)    - Adafruit_CircuitPython_SSD1306        (https://github.com/adafruit/Adafruit_CircuitPython_SSD1306)    - Adafruit_CircuitPython_ADT7410        (https://github.com/adafruit/Adafruit_CircuitPython_ADT7410)"""# Import standard python modulesimport time# import Adafruit SSD1306 OLEDfrom PIL import Image, ImageDraw, ImageFontimport adafruit_ssd1306# import Adafruit IO REST clientfrom Adafruit_IO import Client# import Adafruit CircuitPython adafruit_adt7410 libraryimport adafruit_adt7410# import Adafruit Blinkaimport boardimport busioimport digitalio# Delay between sensor reads, in secondsDELAY_SECONDS = 30# Set to your Adafruit IO key.# Remember, your key is a secret,# so make sure not to publish it when you publish this code!ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY'# Set to your Adafruit IO username.# (go to https://accounts.adafruit.com to find your username)ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME'# Create an instance of the REST clientaio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)# Set up `temperature` feedpi_temperature = aio.feeds('temperature')# Set up OLEDi2c_bus = busio.I2C(board.SCL, board.SDA)oled_reset = digitalio.DigitalInOut(board.D21)disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c_bus, reset=oled_reset)# Clear display.disp.fill(0)disp.show()# Create blank image for drawing.# Make sure to create image with mode '1' for 1-bit color.width = disp.widthheight = disp.heightimage = Image.new('1', (width, height))# Get drawing object to draw on image.draw = ImageDraw.Draw(image)# `sudo apt-get install ttf-mscorefonts-installer` to get these fontsfont_big = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 16)font_small = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 12)adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48)adt.high_resolution = Truetime.sleep(0.25)  # wait for sensor to boot up and get first readingwhile True:    # clear screen    draw.rectangle((0, 0, width, height), outline=0, fill=0)    # Read the temperature sensor    tempC = adt.temperature    tempC = round(tempC, 2)    # display the temperature on the OLED    draw.text((0, 0), "Temp: %0.2F *C" % tempC, font=font_big, fill=255)    # Send temperature to Adafruit IO    print('Sending temperature {0} C to Adafruit IO'.format(tempC))    draw.text((0, 16), "Sending...", font=font_small, fill=255)    disp.image(image)    disp.show()    aio.send(pi_temperature.key, tempC)    draw.text((0, 16), "Sending...Done!", font=font_small, fill=255)    disp.image(image)    disp.show()    # Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io    time.sleep(DELAY_SECONDS)

Before running the code, you'll need toset theADAFRUIT_IO_KEY to the Adafruit IO Key we saved earlier andset theADAFRUIT_IO_USERNAME to your Adafruit IO Username.

When both of these variables are changed, save the file. You canrun the program by entering the following in your terminal:

python adafruit_io_adt7410.py

and Press the Enter Key.

You should see the following print out in your terminal

Sending temperature 22.26 C to Adafruit IOSending temperature 22.27 C to Adafruit IOSending temperature 22.25 C to Adafruit IOSending temperature 22.27 C to Adafruit IOSending temperature 22.28 C to Adafruit IO
Sending temperature 22.26 C to Adafruit IOSending temperature 22.27 C to Adafruit IOSending temperature 22.25 C to Adafruit IOSending temperature 22.27 C to Adafruit IOSending temperature 22.28 C to Adafruit IO

The display should display the temperature read from the ADT7410 along with the status of the data being sent to Adafruit IO.

raspberry_pi_live_feed.png

Next, we're going to check that the data has been received by Adafruit IO. We can do this by visiting the Adafruit IO Monitor page. Every 30 seconds, the page will refresh with the latest values from the ADT7410.

 

If you want to change the delay, change the variable DELAY_SECONDS to the desired delay between sensor reads. 

raspberry_pi_adi_dash.png

Navigate to the dashboard you created earlier to view the temperature gauge we added. Every 30 seconds, it'll update and display a new value.

For more information about the ADT7410 CircuitPython library,check out the latest docs!

Page last edited January 22, 2025

Text editor powered bytinymce.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp