Skip to content

Instantly share code, notes, and snippets.

@lennonjesus
Created October 16, 2020 17:06
Show Gist options
  • Save lennonjesus/05d07cf5eae5ff5b71a22095b2706091 to your computer and use it in GitHub Desktop.
Save lennonjesus/05d07cf5eae5ff5b71a22095b2706091 to your computer and use it in GitHub Desktop.
#include <DHT.h>
#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
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 sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
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 += String(voltage);
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("Luminosidade: ");
Serial.print(voltage);
Serial.println("v");
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment