Help with Adafruit IO Temperature & Humidity Example

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
leoleoleo
 
Posts: 1
Joined: Tue Apr 06, 2021 5:23 pm

Help with Adafruit IO Temperature & Humidity Example

Post by leoleoleo »

I'm having trouble using the "Adafruit IO Temperature & Humidity Example". It always fails to initialize the TCP. The FONA seemed to be working on the FONAtest example, but moving on here it doesn't. Did I maybe set up the APN incorrectly, I am really lost. Thank you so much.
Attachments
Serial output
Serial output
Screenshot_2021-05-16_183739.png (31.15 KiB) Viewed 460 times

User avatar
obray21
 
Posts: 1
Joined: Sat Jul 10, 2021 9:54 am

Re: Help with Adafruit IO Temperature & Humidity Example

Post by obray21 »

HEY,
This is what I have got for you, u may reinstall it with the help of these procedures and then I think there will be no further issues,

CLICK the VIEW AIO KEY button to retrieve your key.

retrieving your key

A window will pop up with your Adafruit IO. Keep a copy of this in a safe place. We'll need it later.

Image of the AIO key

Creating the Feeds

First, you will need to create a feed called Temperature.

Creating the temperature feed

You will also need to create a feed called Humidity.

Creating the Humidity feed

If you need help getting started with creating feeds on Adafruit IO, check out the Adafruit IO Feed Basics guide.

Adding the Line Chart Block

Add a new Line Chart block to a new or existing dashboard. Make sure you have selected both the Temperature and Humidity feeds as the data sources for the block.

Add a new Line Chart block

When you reach the block settings, set the Hours of History setting to 24 hours, and name the block whatever you would like.

When you are finished editing the form, click Create Block to add the new block to the dashboard.

Add the new block

If you need help getting started with Dashboards on Adafruit IO, check out the Adafruit IO Dashboard Basics guide.

Next, we will look at wiring the circuit.

Wiring

You will need the following parts for this tutorial:

1x Adafruit IO compatible Feather
1x DH22 temperature & humidity sensor
1x 10k ohm resistor
4x jumper wires
We will need to connect the following pins from the Feather to the resistor and DHT22:

Feather 3V to the pin 1 of the DHT22
Feather 3V to one leg of a 10k ohm resistor, and the other leg of the resistor to the pin 2 of the DHT22
Feather pin 2 to pin 2 of the DHT22
Feather GND to pin 4 of the DHT22
Wiring guide

Next, let's look at the example sketch we will be using.

Arduino Setup

You should go through the setup guides associated with your selected set of hardware, and make sure you have internet connectivity with the device before continuing. The following links will take you to the guides for your selected platform.

Adafruit Feather HUZZAH ESP8266 Setup Guide
You will need to make sure you have at least version 2.4.1 of the Adafruit IO Arduino library installed before continuing.

Library files 1

You will also need to install the Adafruit Unified Sensor library.

Library files 2

As well as the DHT Sensor Library.

Library files 3

For this example, you will need to open the adafruitio_15_temp_humidity example in the Adafruit IO Arduino library.

open the adafruitio_15_temp_humidity example

Next, we will look at the network configuration options in the sketch.

Network Config

To configure the network settings, click on the config.h tab in the sketch. You will need to set your Adafruit IO username in the IO_USERNAME define, and your Adafruit IO key in the IO_KEY define.

You will need to set your Adafruit IO username

WiFi Config

WiFi is enabled by default in config.h so if you are using one of the supported WiFi boards, you will only need to modify the WIFI_SSID and WIFI_PASS options in the config.h tab.

modify the WIFI_SSID and WIFI_PASS options

FONA Config

If you wish to use the FONA 32u4 Feather to connect to Adafruit IO, you will need to first comment out the WiFi support in config.h

Comment out the WiFi lines if using FONA

Next, remove the comments from both of the FONA config lines in the FONA section of config.h to enable FONA support.

Uncomment both Fona config lines

Ethernet Config

If you wish to use the Ethernet Wing to connect to Adafruit IO, you will need to first comment out the WiFi support in config.h

Comment out WiFi for Ethernet

Next, remove the comments from both of the Ethernet config lines in the Ethernet section of config.h to enable Ethernet Wing support.

Uncomment both ethernet config lines

Next, we will look at how the example sketch works.

Code

The adafruitio_15_temp_humidity example uses digital pin 2 by default on all boards, and that can be modified if needed by changing the DATA_PIN define.

Copy Code
// pin connected to DH22 data line
#define DATA_PIN 2
The next chunk of code creates an instance of the DHT class, and also sets up feed instances for the temperature and humidity feeds.

Copy Code
// create DHT22 instance
DHT_Unified dht(DATA_PIN, DHT22);

// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
The setup function initializes the DHT22 sensor, and also connects your feather to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch. If you have any issues connecting, check config.h for any typos in your username or key.

Copy Code
void setup() {

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// initialize dht22
dht.begin();

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());

}
Next, we have the main loop() function. The first line of the loop function calls io.run(); this line will need to be present at the top of your loop in every sketch. It helps keep your device connected to Adafruit IO, and processes any incoming data.

Copy Code
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();
The next chunk of code inside the loop() checks the current DHT22 temperature value, and saves the value in the celsius and fahrenheit variables.

We then print both celsius and fahrenheit to the Arduino Serial Monitor, and save the fahrenheit value to the temperature feed on Adafruit IO.

Copy Code
sensors_event_t event;
dht.temperature().getEvent(&event);

float celsius = event.temperature;
float fahrenheit = (celsius * 1.8) 32;

Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");

Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");

// save fahrenheit (or celsius) to Adafruit IO
temperature->save(fahrenheit);
If you prefer to log celsius values, you can modify the call to the save() function.

Copy Code
temperature->save(celsius);
The final chunk of the loop() function requests a humidity reading from the DHT22, and prints the value to the Arduino Serial Monitor. We also save the humidity value to the humidity feed on Adafruit IO.

Copy Code
dht.humidity().getEvent(&event);

Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");

// save humidity to Adafruit IO
humidity->save(event.relative_humidity);

// wait 5 seconds (5000 milliseconds == 5 seconds)
delay(5000);

}
Upload the sketch to your board, and open the Arduino Serial Monitor. Your board should now connect to Adafruit IO.

Copy Code
Connecting to Adafruit IO....

Adafruit IO connected.
You should now see the temperature and humidity values being sent to Adafruit IO.

Copy Code
celsius: 18.30C
fahrenheit: 64.94F
humidity: 34.90%

celsius: 18.20C
fahrenheit: 64.76F
humidity: 35.40%
Check your dashboard on Adafruit IO, and you should see the line chart update with the changes in temperature and humidity.

Check your Adafruit IO dashboard for updates

Add an OLED

Add an OLED

Now that you've got a graphing weather device using IO, you can add an OLED feather so you can see network status, IP address, and the latest measurements!

Plug the OLED FeatherWing on top of your Feather, and check out our guide to get set up and test it! Once you've verified that the OLED works, you can use this new code. Use the same config.h file from the previous section, just replace the 'main' tab of code:

ATTENTION

Copy Code
// Adafruit IO Temperature & Humidity Example
// Tutorial Link: https://learn.adafruit.com/adafruit-io- ... d-humidity
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016-2017 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/************************ Example Starts Here *******************************/
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_SSD1306.h>

// oled display
Adafruit_SSD1306 oled = Adafruit_SSD1306();

// pin connected to DH22 data line
#define DATA_PIN 2

// create DHT22 instance
DHT_Unified dht(DATA_PIN, DHT22);

// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");

void setup() {
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
oled.display();

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// initialize dht22
dht.begin();

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());

// text display tests
oled.setTextSize(1);
oled.setTextColor(WHITE);
}

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();

sensors_event_t event;
dht.temperature().getEvent(&event);

float celsius = event.temperature;
float fahrenheit = (celsius * 1.8) 32;

Serial.print("celsius: ");
Serial.print(celsius);
Serial.println("C");

Serial.print("fahrenheit: ");
Serial.print(fahrenheit);
Serial.println("F");

// save fahrenheit (or celsius) to Adafruit IO
temperature->save(fahrenheit);

dht.humidity().getEvent(&event);

Serial.print("humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");

// save humidity to Adafruit IO
humidity->save(event.relative_humidity);

// print it to the OLED
oled.clearDisplay();
oled.setCursor(0,0);
oled.print("SSID: "); oled.println(WIFI_SSID);
oled.print("IP: "); oled.println(WiFi.localIP());
oled.print("Temp: "); oled.print(fahrenheit,0); oled.print(" *F ");
oled.print("Hum: "); oled.print(event.relative_humidity,0); oled.println(" %");
oled.print("IO Status: ");
aio_status_t aio_status = io.status();
Serial.print("Status: "); Serial.println(aio_status);
switch (aio_status) {
case AIO_IDLE: oled.println("IDLE"); break;
case AIO_DISCONNECTED:
case AIO_NET_DISCONNECTED: oled.println("DISCONNECT"); break;
case AIO_NET_CONNECTED:
case AIO_CONNECTED_INSECURE:
case AIO_CONNECTED: oled.println("CONNECTED"); break;
}
oled.display();

// wait 5 seconds (5000 milliseconds == 5 seconds)
delay(2000);
}.


MUCH OBLIGED.

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”