Commit 0db1b7df authored by louie.bridges's avatar louie.bridges

Upload New File

parent db8f1fd1
//Internet of Things Project Code
//This code will be used to connect the ESP8266 microcontroller to the internet
//and then gather the humidity and temperature data from the DHT11 sensor
//and display it on the cloud service ThingSpeak
//Libraries
#include <ESP8266WiFi.h>
#include <DHT.h>
//Network access SSID and password
WiFiClient client;
const char *ssid = "BTHub6-FHZM";
const char *password = "exQ4EDgfVT7R";
//ThingSpeak API key and server address
const char* server ="api.thingspeak.com";
String thinkspeakAPIKey = "QC30MAFXUWR6V8IH";
//Define what pin the DHT11 is connected on the microcontroller
#define DHTPIN 3
DHT dht11(DHTPIN, DHT11);
void setup() {
// put your setup code here, to run once:
//Connecting the ESP8266 to the internet
Serial.begin(115200);
dht11.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.println("WiFi connected");
}
void loop() {
// put your main code here, to run repeatedly:
// Read humidity and temperature
float temp = dht11.readTemperature();
float humidity = dht11.readHumidity();
if (client.connect(server, 80)) {
//Posts humidity and temperature to fields 1 and 2 on thingspeak
String postData = thinkspeakAPIKey;
postData +="&field1=";
postData += String(temp);
postData +="&field2=";
postData += String(humidity);
postData += "\r\n\r\n";
//Connect to thingspeak and send data via the API key
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+thinkspeakAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postData.length());
client.print("\n\n");
client.print(postData);
// Prints temperature and humitity to the serial monitor.
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("Humidity: ");
Serial.print(humidity);
}
client.stop();
// Thingspeak needs 15 seconds to update so I have set a delay for 30 seconds.
delay(10000);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment