/ adafruitio-adt7410 / adafruitio-adt7410.ino
adafruitio-adt7410.ino
  1  // SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  // Adafruit IO ADT7410 Example
  6  //
  7  // Adafruit invests time and resources providing this open source code.
  8  // Please support Adafruit and open source hardware by purchasing
  9  // products from Adafruit!
 10  //
 11  // Written by Ladyada and Brent Rubell for Adafruit Industries
 12  // Copyright (c) 2019 Adafruit Industries
 13  // Licensed under the MIT license.
 14  //
 15  // All text above must be included in any redistribution.
 16  
 17  /************************ Adafruit IO Config *******************************/
 18  
 19  // visit io.adafruit.com if you need to create an account,
 20  // or if you need your Adafruit IO key.
 21  #define IO_USERNAME "YOUR_IO_USERNAME"
 22  #define IO_KEY "YOUR_IO_KEY"
 23  
 24  /******************************* WIFI **************************************/
 25  
 26  // the AdafruitIO_WiFi client will work with the following boards:
 27  //   - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
 28  //   - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
 29  //   - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
 30  //   - Feather M0 WiFi -> https://www.adafruit.com/products/3010
 31  //   - Feather WICED -> https://www.adafruit.com/products/3056
 32  
 33  #define WIFI_SSID "WIFI_NAME"
 34  #define WIFI_PASS "WIFI_PASS"
 35  
 36  // comment out the following two lines if you are using fona or ethernet
 37  #include "AdafruitIO_WiFi.h"
 38  AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
 39  
 40  /************************** Configuration ***********************************/
 41  
 42  // time between sending data to adafruit io, in minutes
 43  #define MESSAGE_WAIT_SEC (15 * 60)
 44  /************************ Example Starts Here *******************************/
 45  #include <Wire.h>
 46  // adt7410 sensor
 47  #include "Adafruit_ADT7410.h"
 48  // featherwing oled
 49  #include <Adafruit_GFX.h>
 50  #include <Adafruit_SSD1306.h>
 51  #include <SPI.h>
 52  
 53  // Create the OLED display object
 54  Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);
 55  
 56  // Create the ADT7410 temperature sensor object
 57  Adafruit_ADT7410 tempsensor = Adafruit_ADT7410();
 58  
 59  // set up the 'temperature' feed
 60  AdafruitIO_Feed *huzzah_temperature = io.feed("temperature");
 61  
 62  void setup()
 63  {
 64  
 65    // start the serial connection
 66    Serial.begin(115200);
 67  
 68    // wait for serial monitor to open
 69    while (!Serial)
 70      ;
 71  
 72    Serial.println("Adafruit IO ADT7410 + OLED");
 73    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
 74    Serial.println("OLED begun");
 75  
 76    // Show image buffer on the display hardware.
 77    // Since the buffer is intialized with an Adafruit splashscreen
 78    // internally, this will display the splashscreen.
 79    display.display();
 80    delay(1000);
 81  
 82    // Clear the buffer.
 83    display.clearDisplay();
 84    display.display();
 85  
 86    // Make sure the sensor is found, you can also pass in a different i2c
 87    // address with tempsensor.begin(0x49) for example
 88    if (!tempsensor.begin())
 89    {
 90      Serial.println("Couldn't find ADT7410!");
 91      while (1)
 92        ;
 93    }
 94  
 95    // sensor takes 250 ms to get first readings
 96    delay(250);
 97  
 98    // connect to io.adafruit.com
 99    Serial.print("Connecting to Adafruit IO");
100    io.connect();
101  
102    display.clearDisplay();
103    display.display();
104    display.setTextSize(1);
105    display.setTextColor(WHITE);
106    display.setCursor(0, 0);
107    display.print("Connecting to IO...");
108    display.display();
109  
110    // wait for a connection
111    while (io.status() < AIO_CONNECTED)
112    {
113      Serial.print(".");
114      delay(500);
115    }
116  
117    // we are connected
118    Serial.println();
119    Serial.println(io.statusText());
120  }
121  
122  void loop()
123  {
124  
125    // io.run(); is required for all sketches.
126    // it should always be present at the top of your loop
127    // function. it keeps the client connected to
128    // io.adafruit.com, and processes any incoming data.
129    io.run();
130  
131    // Read and print out the temperature, then convert to *F
132    float c = tempsensor.readTempC();
133  
134    Serial.print("Temp: ");
135    Serial.print(c);
136    Serial.println("C");
137  
138    // clear the display
139    display.clearDisplay();
140    display.display();
141  
142    // print to display
143    display.setTextColor(WHITE);
144    display.setTextSize(2);
145    display.setCursor(0, 0);
146    display.print("Temp:");
147    display.print(c);
148  
149    // send data to adafruit io
150    display.setCursor(15, 20);
151    display.setTextSize(1);
152    display.print("Sending...");
153    display.display();
154    Serial.println("Sending to Adafruit IO");
155    delay(1000);
156    huzzah_temperature->save(c, 0, 0, 0, 2);
157  
158    // sent to IO
159    display.clearDisplay();
160    display.display();
161    display.setTextSize(2);
162    display.setCursor(0, 0);
163    display.print("Temp:");
164    display.print(c);
165    display.setTextSize(1);
166    display.setCursor(15, 20);
167    display.print("Sending...Done!");
168    display.display();
169  
170    Serial.print("Waiting ");
171    Serial.print(MESSAGE_WAIT_SEC);
172    Serial.println(" seconds");
173    // wait 15 minutes between sends
174    for (int i = 0; i < MESSAGE_WAIT_SEC; i++)
175    {
176      delay(1000);
177    }
178  }