Code

Now that we’ve got the feed set up, we’ll want to automatically send data from our "thing". Let's show how to do that with both a Raspberry Pi and Python as well as a Feather ESP8266 and C++. For our sensor, we’ll use the ADT7410 from Analog Devices, which is a high precision temperature sensor with an I2C interface.

Linux + Python Example

Lets start with a Raspberry Pi with Python. This assumes we’ve already set up the Pi’s operating system and WiFi. We wire up the sensor with I2C and run the Python script on this page.

# SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
'adafruit_io_adt7410.py'
==================================
Example of sending temperature
values to an Adafruit IO feed using
an 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 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

# Delay between sensor reads, in seconds
DELAY_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 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 DELAY_SECONDS seconds to avoid timeout from adafruit io
    time.sleep(DELAY_SECONDS)

As you can see, this is very simple and straightforward - we only have to enter in our Username and AIO Key. The code will connect to the feed, initialize the sensor, and then execute a loop, sending the temperature every 30 seconds.

Meanwhile, over at the Adafruit IO website, we can monitor what data is coming in. This is very handy if you’re having an issue - you can see the connected IP addresses and what feeds they are writing to. You can also see errors.

Perhaps a typo snuck into your code, it may appear here!

Microcontroller + Arduino Example

Likewise, here’s an example using the ESP8266 Feather and an ADT7410 wired up on a breadboard. With the ESP Feather, we’re stuck with a microcontroller rather than a full computer, so we don’t use Python, instead we’ll stick to “Arduino” C++. The tradeoff is that we have much lower power usage, and faster boot, but we also have to be more careful with memory and link management. Check out the Arduino code on this guide.

// SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// 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 <Wire.h>
// adt7410 sensor
#include "Adafruit_ADT7410.h"
// featherwing oled
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>

// 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);
  }
}

Like the Python code, it’s pretty simple - we need our Username and AIO Key. We also need the WiFi credentials since there’s no operating system to do that for us. Like the Pi script, we connect, initialize the sensor and send temperature data every 10 seconds.

And at Adafruit IO, we have the same monitored data:

Once we’re sure that the monitor is properly indicating what data is coming in, we can check our feeds. Aha! We’ve got our temperature data feeds from both the Pi and Feather.

For more information on the basics of using Adafruit IO with temperature and humidity sensors, check out this learn guide!

This guide was first published on Mar 14, 2019. It was last updated on Mar 19, 2024.

This page (Sending Data From Your Thing) was last updated on Mar 18, 2024.

Text editor powered by tinymce.