From b68c95b3ec9de251d2b722ea25cfd7f720675b79 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 8 Feb 2019 16:01:37 -0500 Subject: [PATCH] add ADT7410 Adafruit IO Guide files --- adafruitio-adt7410/adafruit_io_adt7410.py | 95 ++++++++++++ adafruitio-adt7410/adafruitio_adt7410.ino | 174 ++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 adafruitio-adt7410/adafruit_io_adt7410.py create mode 100644 adafruitio-adt7410/adafruitio_adt7410.ino diff --git a/adafruitio-adt7410/adafruit_io_adt7410.py b/adafruitio-adt7410/adafruit_io_adt7410.py new file mode 100644 index 000000000..79b9be52d --- /dev/null +++ b/adafruitio-adt7410/adafruit_io_adt7410.py @@ -0,0 +1,95 @@ +""" +'adafruit_io_adt7410.py' +================================== +Example of sending temperature +values to an Adafruit IO feed using +an ADT7410 + +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 modules +import time + +# import Adafruit SSD1306 OLED +from PIL import Image, ImageDraw, ImageFont +import adafruit_ssd1306 + +# import Adafruit IO REST client +from Adafruit_IO import Client + +# import Adafruit CircuitPython adafruit_adt7410 library +import adafruit_adt7410 + +# import Adafruit Blinka +import board +import busio +import digitalio + +# 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 client +aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY) + +# Set up `temperature` feed +pi_temperature = aio.feeds('temperature') + +# Set up OLED +i2c_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.width +height = disp.height +image = 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 fonts +font_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 = True +time.sleep(0.25) # wait for sensor to boot up and get first reading + +while 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 5 seconds to avoid timeout from adafruit io + time.sleep(5) diff --git a/adafruitio-adt7410/adafruitio_adt7410.ino b/adafruitio-adt7410/adafruitio_adt7410.ino new file mode 100644 index 000000000..22298497f --- /dev/null +++ b/adafruitio-adt7410/adafruitio_adt7410.ino @@ -0,0 +1,174 @@ +// Adafruit IO ADT7410 Example +// +// Adafruit invests time and resources providing this open source code. +// Please support Adafruit and open source hardware by purchasing +// products from Adafruit! +// +// Written by Ladyada and Brent Rubell for Adafruit Industries +// Copyright (c) 2019 Adafruit Industries +// Licensed under the MIT license. +// +// All text above must be included in any redistribution. + +/************************ Adafruit IO Config *******************************/ + +// visit io.adafruit.com if you need to create an account, +// or if you need your Adafruit IO key. +#define IO_USERNAME "YOUR_IO_USERNAME" +#define IO_KEY "YOUR_IO_KEY" + +/******************************* WIFI **************************************/ + +// the AdafruitIO_WiFi client will work with the following boards: +// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471 +// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821 +// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405 +// - Feather M0 WiFi -> https://www.adafruit.com/products/3010 +// - Feather WICED -> https://www.adafruit.com/products/3056 + +#define WIFI_SSID "WIFI_NAME" +#define WIFI_PASS "WIFI_PASS" + +// comment out the following two lines if you are using fona or ethernet +#include "AdafruitIO_WiFi.h" +AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); + +/************************** Configuration ***********************************/ + +// time between sending data to adafruit io, in minutes +#define MESSAGE_WAIT_SEC (15 * 60) +/************************ Example Starts Here *******************************/ +#include +// adt7410 sensor +#include "Adafruit_ADT7410.h" +// featherwing oled +#include +#include +#include + +// Create the OLED display object +Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); + +// Create the ADT7410 temperature sensor object +Adafruit_ADT7410 tempsensor = Adafruit_ADT7410(); + +// set up the 'temperature' feed +AdafruitIO_Feed *huzzah_temperature = io.feed("temperature"); + +void setup() +{ + + // start the serial connection + Serial.begin(115200); + + // wait for serial monitor to open + while (!Serial) + ; + + Serial.println("Adafruit IO ADT7410 + OLED"); + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 + Serial.println("OLED begun"); + + // Show image buffer on the display hardware. + // Since the buffer is intialized with an Adafruit splashscreen + // internally, this will display the splashscreen. + display.display(); + delay(1000); + + // Clear the buffer. + display.clearDisplay(); + display.display(); + + // Make sure the sensor is found, you can also pass in a different i2c + // address with tempsensor.begin(0x49) for example + if (!tempsensor.begin()) + { + Serial.println("Couldn't find ADT7410!"); + while (1) + ; + } + + // sensor takes 250 ms to get first readings + delay(250); + + // connect to io.adafruit.com + Serial.print("Connecting to Adafruit IO"); + io.connect(); + + display.clearDisplay(); + display.display(); + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0, 0); + display.print("Connecting to IO..."); + display.display(); + + // wait for a connection + while (io.status() < AIO_CONNECTED) + { + Serial.print("."); + delay(500); + } + + // we are connected + Serial.println(); + Serial.println(io.statusText()); +} + +void loop() +{ + + // io.run(); is required for all sketches. + // it should always be present at the top of your loop + // function. it keeps the client connected to + // io.adafruit.com, and processes any incoming data. + io.run(); + + // Read and print out the temperature, then convert to *F + float c = tempsensor.readTempC(); + + Serial.print("Temp: "); + Serial.print(c); + Serial.println("C"); + + // clear the display + display.clearDisplay(); + display.display(); + + // print to display + display.setTextColor(WHITE); + display.setTextSize(2); + display.setCursor(0, 0); + display.print("Temp:"); + display.print(c); + + // send data to adafruit io + display.setCursor(15, 20); + display.setTextSize(1); + display.print("Sending..."); + display.display(); + Serial.println("Sending to Adafruit IO"); + delay(1000); + huzzah_temperature->save(c, 0, 0, 0, 2); + + // sent to IO + display.clearDisplay(); + display.display(); + display.setTextSize(2); + display.setCursor(0, 0); + display.print("Temp:"); + display.print(c); + display.setTextSize(1); + display.setCursor(15, 20); + display.print("Sending...Done!"); + display.display(); + + Serial.print("Waiting "); + Serial.print(MESSAGE_WAIT_SEC); + Serial.println(" seconds"); + // wait 15 minutes between sends + for (int i = 0; i < MESSAGE_WAIT_SEC; i++) + { + delay(1000); + } +} \ No newline at end of file