Matt McNamara
Published

Adafruit Feather HUZZAH with ESP8266 WiFi

Feather HUZZAH connected to an AM2302 (DHT22) temperature sensor uploading data to ThingSpeak IoT

BeginnerFull instructions provided1 hour4,085
Adafruit Feather HUZZAH with ESP8266 WiFi

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1

Software apps and online services

ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Schematic

Pin 1 (power) to 3.3V - Pin 2 (signal) to Pin 2 - Pin 4 (ground) to Ground

Huzzah Datasheet

AM2302 Sensor

Code

C Source code for programming the ESP8266 and AM2302 sensor

C/C++
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ThingSpeak.h>

#define DHTPIN 2                                    // what digital pin we're connected to
const char* ssid = "XXXXXX";                        // Wireless SID
const char* password = "XXXXXXXXXX";                // Wireless Passcode

ESP8266WebServer server(80);

const int led = 13;

// #define DHTTYPE DHT11                            // DHT 11
#define DHTTYPE DHT22                               // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21                             // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.

DHT dht(DHTPIN, DHTTYPE);

WiFiClient  client;
unsigned int myChannelNumber = XXXXXX;           // Channel Number from                                                   // ThingSpeak IoT
const char * myWriteAPIKey = "InsertAPIKey";     // Write API Key
                                                 // fom ThingSpeak IoT
void handleRoot() 
    {
    digitalWrite(led, 1);
    delay(1000);
    server.send(200, "text/plain", "hello from esp8266!");
    digitalWrite(led, 0);
    delay(1000);
    }

void handleNotFound()
    {
    digitalWrite(led, 1);
    String message = "File Not Found\n\n";
    message += "URI: ";
    message += server.uri();
    message += "\nMethod: ";
    message += (server.method() == HTTP_GET)?"GET":"POST";
    message += "\nArguments: ";
    message += server.args();
    message += "\n";
   
    for (uint8_t i=0; i<server.args(); i++)
        {
        message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
        }
    
    server.send(404, "text/plain", message);
    digitalWrite(led, 0);
    }

void setup(void)
    {
    Serial.println("AM2302 test!");
    dht.begin();

    pinMode(led, OUTPUT);
    digitalWrite(led, 0);
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    ThingSpeak.begin(client);
    Serial.println("");

  // Wait for connection
    while (WiFi.status() != WL_CONNECTED) 
        {
        delay(500);
        Serial.print(".");
        }
  
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());

    if (MDNS.begin("esp8266")) 
        {
        Serial.println("MDNS responder started");
        }

    server.on("/", handleRoot);

    server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

    server.onNotFound(handleNotFound);

    server.begin();
    Serial.println("HTTP server started");
    }

void loop(void)
    {
    server.handleClient();
     // Wait 60 seconds between measurements.
    delay(60000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) 
        {
        Serial.println("Failed to read from DHT sensor!");
        return;
        }

    float hif = dht.computeHeatIndex(f, h);         // Compute heat index in Fahrenheit (the default)
    float hic = dht.computeHeatIndex(t, h, false);  // Compute heat index in Celsius (isFahreheit = false)

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    
    ThingSpeak.setField(1, t);
    ThingSpeak.setField(2, f);
    ThingSpeak.setField(3, h);
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    }

Credits

Matt McNamara

Matt McNamara

2 projects • 1 follower

Comments