AM2302 (DTH22) on Adafruit Feather HUZZAH reporting via Wi-Fi (step 4)

Next we’ll connect the Feather to Wi-Fi and report the temperature and humidity data to a cloud MySQL database server through a web page.

Using the MAC address for the Feather you found in step 2, register the Feather on Devicenet.

Paste the is sketch and make necessary edits (indicated with //TODO):

// // Import required libraries #include "ESP8266WiFi.h" #include "DHT.h" // WiFi parameters const char* ssid = "put your SSID between these quotes"; // TODO const char* password = "put your wi-fi password between these quotes"; // TODO // Data Pin #define DHTPIN 2 // Ensure the data wire (yellow) for the DHT22 is on pin 2 // Use DHT22 sensor #define DHTTYPE DHT22 //for AM2302 // Initialize DHT sensor DHT dht(DHTPIN, DHTTYPE); // Host const char* host = "put the web host between these quotes"; // TODO void setup() { // Start Serial Serial.begin(115200); delay(10); // Init DHT dht.begin(); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { Serial.print("Connecting to "); Serial.println(host); // Use WiFiClient class to create TCP connections WiFiClient client; const int httpPort = 80; if (!client.connect(host, httpPort)) { Serial.println("connection failed"); return; } // Reading temperature and humidity float h = dht.readHumidity(); // Read temperature as Celsius float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); // This will send the request to the server // TODO // /iot?sensor=0 <-- this should be changed to reflect the page and sensor ID // it is passing sensor, temp, and humidity as URL GET parameters // the iot page is parsing these parameters into variables and writing them to the database client.print(String("GET /iot?sensor=0&temp=") + String(f) + "&humidity=" + String(h) + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(10); Serial.print("data written to db"); Serial.println(); Serial.println("closing connection"); // Repeat every 15 minutes delay(900000); // TODO - change this to reflect how frequently you want the data written to the database }

At this point, you should visit the website, click on your sensor ID and see data on the web site. It sends the first data as soon as it’s powered on and (in this case) every ~15 minutes.

You can open the serial monitor (magnifying glass), set the baud to 115200, and see the serial print output. Press the reset button on your Feather. Each time it is reset it will write to the serial monitor and the database.