first commit
This commit is contained in:
96
ESP8266_TEMP_SLEEP_2/ESP8266_TEMP_SLEEP_2.ino
Executable file
96
ESP8266_TEMP_SLEEP_2/ESP8266_TEMP_SLEEP_2.ino
Executable file
@@ -0,0 +1,96 @@
|
||||
|
||||
/* DHT22 temperature and humidity data are uploaded to thingspeak using
|
||||
* WeMOS ESP8266 MCU.
|
||||
*
|
||||
* Details: http://embedded-lab.com/blog/iot-temperature-and-humidity-logger/
|
||||
*/
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <DHT.h>
|
||||
//#include "Math.h"
|
||||
#define DHTTYPE DHT22
|
||||
#define DHTPIN 0 // DHT22 data pin connects to D4
|
||||
|
||||
// Time to sleep (in seconds):
|
||||
const int sleepTimeS = 60;
|
||||
|
||||
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
|
||||
|
||||
|
||||
float prevTemp = 0;
|
||||
const char* server = "api.thingspeak.com";
|
||||
String apiKey ="API Key here";
|
||||
const char* MY_SSID = "Livebox-37cc";
|
||||
const char* MY_PWD = "8A6060920A8A86896F770F2C47";
|
||||
int sent = 0;
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
dht.begin();
|
||||
connectWifi();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
float t1,humidity;
|
||||
//char buffer[10];
|
||||
humidity = dht.readHumidity(); // Read humidity (percent)
|
||||
t1 = dht.readTemperature(true); // Read temperature as Fahrenheit
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(humidity) || isnan(t1)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Serial.print(String(sent)+" Temperature: ");
|
||||
Serial.println(t1);
|
||||
Serial.print(String(sent)+" Humidity: ");
|
||||
Serial.println(humidity);
|
||||
sendTeperatureTS(t1, humidity);
|
||||
ESP.deepSleep(sleepTimeS*1000000, WAKE_RF_DEFAULT); // Sleep for 60 seconds
|
||||
}
|
||||
|
||||
void connectWifi()
|
||||
{
|
||||
Serial.print("Connecting to "+*MY_SSID);
|
||||
WiFi.begin(MY_SSID, MY_PWD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("Connected");
|
||||
Serial.println("");
|
||||
}//end connect
|
||||
|
||||
void sendTeperatureTS(float temp1, float temp2)
|
||||
{
|
||||
WiFiClient client;
|
||||
|
||||
if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
|
||||
Serial.println("WiFi Client connected ");
|
||||
|
||||
String postStr = apiKey;
|
||||
postStr += "&field1=";
|
||||
postStr += String(temp1);
|
||||
postStr += "&field2=";
|
||||
postStr += String(temp2);
|
||||
postStr += "\r\n\r\n";
|
||||
|
||||
client.print("POST /update HTTP/1.1\n");
|
||||
client.print("Host: api.thingspeak.com\n");
|
||||
client.print("Connection: close\n");
|
||||
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
|
||||
client.print("Content-Type: application/x-www-form-urlencoded\n");
|
||||
client.print("Content-Length: ");
|
||||
client.print(postStr.length());
|
||||
client.print("\n\n");
|
||||
client.print(postStr);
|
||||
delay(1000);
|
||||
|
||||
}//end if
|
||||
sent++;
|
||||
client.stop();
|
||||
}//end send
|
||||
Reference in New Issue
Block a user