Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lennonjesus/9f492808cd1f2c8ed1009a88e81b8291 to your computer and use it in GitHub Desktop.
Save lennonjesus/9f492808cd1f2c8ed1009a88e81b8291 to your computer and use it in GitHub Desktop.
#include <DHT.h> // Including library for dht
#include <ESP8266WiFi.h>
String THINGSPEAK_API_KEY = "THINGSPEAK_API_KEY";
const char *ssid = "WIFI-SSID";
const char *pass = "WIFI-PASS";
const char *server = "api.thingspeak.com";
#define DHTPIN D3 //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
dht.begin();
WiFi.begin(ssid, pass);
Serial.print("Conectando em ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Conectado em ");
Serial.println(ssid);
}
void loop() {
int groundHumidity = analogRead(A0); // read the input on analog pin 0
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) {
String postStr = THINGSPEAK_API_KEY;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += groundHumidity;
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: " + THINGSPEAK_API_KEY + "\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);
Serial.print("Umidade do solo: ");
if (groundHumidity >= 0 && groundHumidity < 400) {
Serial.print("Umido (");
}
if (groundHumidity > 400 && groundHumidity < 800) {
Serial.print("Moderado (");
}
if (groundHumidity > 800 && groundHumidity <= 1024) {
Serial.print("Seco (");
}
Serial.print(groundHumidity);
Serial.println(")");
Serial.print("Temperatura: ");
Serial.print(t);
Serial.println("º C");
Serial.print("Umidade do ar: ");
Serial.print(h);
Serial.println("%");
Serial.println("Dados enviados.");
}
client.stop();
Serial.print("Aguardando próxima leitura");
for (int idx = 0; idx < 15; idx++) {
Serial.print(".");
delay(1000);
}
Serial.println("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment