/ adafruitio-adt7410 / adafruit_io_adt7410.py
adafruit_io_adt7410.py
1 # SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 'adafruit_io_adt7410.py' 7 ================================== 8 Example of sending temperature 9 values to an Adafruit IO feed using 10 an ADT7410 breakout. 11 12 Dependencies: 13 - Adafruit_Blinka 14 (https://github.com/adafruit/Adafruit_Blinka) 15 - Adafruit_CircuitPython_SSD1306 16 (https://github.com/adafruit/Adafruit_CircuitPython_SSD1306) 17 - Adafruit_CircuitPython_ADT7410 18 (https://github.com/adafruit/Adafruit_CircuitPython_ADT7410) 19 """ 20 # Import standard python modules 21 import time 22 23 # import Adafruit SSD1306 OLED 24 from PIL import Image, ImageDraw, ImageFont 25 import adafruit_ssd1306 26 27 # import Adafruit IO REST client 28 from Adafruit_IO import Client 29 30 # import Adafruit CircuitPython adafruit_adt7410 library 31 import adafruit_adt7410 32 33 # import Adafruit Blinka 34 import board 35 import busio 36 import digitalio 37 38 # Delay between sensor reads, in seconds 39 DELAY_SECONDS = 30 40 41 # Set to your Adafruit IO key. 42 # Remember, your key is a secret, 43 # so make sure not to publish it when you publish this code! 44 ADAFRUIT_IO_KEY = 'ADAFRUIT_IO_KEY' 45 46 # Set to your Adafruit IO username. 47 # (go to https://accounts.adafruit.com to find your username) 48 ADAFRUIT_IO_USERNAME = 'ADAFRUIT_IO_USERNAME' 49 50 # Create an instance of the REST client 51 aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) 52 53 # Set up `temperature` feed 54 pi_temperature = aio.feeds('temperature') 55 56 # Set up OLED 57 i2c_bus = busio.I2C(board.SCL, board.SDA) 58 oled_reset = digitalio.DigitalInOut(board.D21) 59 disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c_bus, reset=oled_reset) 60 # Clear display. 61 disp.fill(0) 62 disp.show() 63 64 # Create blank image for drawing. 65 # Make sure to create image with mode '1' for 1-bit color. 66 width = disp.width 67 height = disp.height 68 image = Image.new('1', (width, height)) 69 # Get drawing object to draw on image. 70 draw = ImageDraw.Draw(image) 71 # `sudo apt-get install ttf-mscorefonts-installer` to get these fonts 72 font_big = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 16) 73 font_small = ImageFont.truetype('/usr/share/fonts/truetype/msttcorefonts/Arial_Bold.ttf', 12) 74 75 adt = adafruit_adt7410.ADT7410(i2c_bus, address=0x48) 76 adt.high_resolution = True 77 time.sleep(0.25) # wait for sensor to boot up and get first reading 78 79 while True: 80 # clear screen 81 draw.rectangle((0, 0, width, height), outline=0, fill=0) 82 83 # Read the temperature sensor 84 tempC = adt.temperature 85 tempC = round(tempC, 2) 86 87 # display the temperature on the OLED 88 draw.text((0, 0), "Temp: %0.2F *C" % tempC, font=font_big, fill=255) 89 90 # Send temperature to Adafruit IO 91 print('Sending temperature {0} C to Adafruit IO'.format(tempC)) 92 draw.text((0, 16), "Sending...", font=font_small, fill=255) 93 disp.image(image) 94 disp.show() 95 96 aio.send(pi_temperature.key, tempC) 97 draw.text((0, 16), "Sending...Done!", font=font_small, fill=255) 98 disp.image(image) 99 disp.show() 100 101 # Delay for DELAY_SECONDS seconds to avoid timeout from adafruit io 102 time.sleep(DELAY_SECONDS)