Created
October 28, 2020 13:30
-
-
Save lennonjesus/694a90b0350b24cb7150172a1f49e96b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ESP 32s DevKit | |
#include <WiFi.h> | |
//#include <HTTPClient.h> | |
#include <DHT.h> | |
String THINGSPEAK_API_KEY = ""; | |
const char *ssid = ""; | |
const char *pass = ""; | |
const char *server = "api.thingspeak.com"; | |
//const int APIN_EMPTY = 35; | |
const int APIN_SOIL_HUM = 33; | |
const int APIN_LDR = 34; | |
const int APIN_RAIN = 35; | |
//const int APIN_EMPTY = 36; | |
const int DPIN_RAIN = 4; | |
const int DPIN_DHT11 = 23; | |
WiFiClient client; | |
DHT dht(DPIN_DHT11, DHT11); | |
void connectWiFi() { | |
delay(100); | |
WiFi.begin(ssid, pass); | |
Serial.print("Conectando em "); | |
Serial.print(ssid); | |
int timeout = 0; | |
while (WiFi.status() != WL_CONNECTED && ++timeout <= 10) { | |
digitalWrite(BUILTIN_LED, HIGH); | |
delay(500); | |
digitalWrite(BUILTIN_LED, LOW); | |
delay(500); | |
Serial.print("."); | |
} | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println(" Conexão não estabelecida. Reiniciando..."); | |
ESP.restart(); | |
} | |
Serial.println(""); | |
Serial.print("Conectado em "); | |
Serial.print(ssid); | |
Serial.print(" com o IP "); | |
Serial.println(WiFi.localIP()); | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(DPIN_RAIN, INPUT); | |
pinMode(BUILTIN_LED,OUTPUT); | |
connectWiFi(); | |
delay(10); | |
dht.begin(); | |
} | |
void sendDataToServer(float humidity, float temperature, float soilMoisture, int luminosity, int rainIntensity, boolean isRaining) { | |
if (client.connect(server, 80)) { | |
String postStr = THINGSPEAK_API_KEY; | |
postStr +="&field1="; | |
postStr += String(temperature); | |
postStr +="&field2="; | |
postStr += String(humidity); | |
postStr +="&field3="; | |
postStr += soilMoisture; | |
postStr +="&field4="; | |
postStr += rainIntensity; | |
postStr +="&field5="; | |
postStr += isRaining; | |
postStr +="&field6="; | |
postStr += luminosity; | |
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: "); | |
// Serial.print(soilMoisture); | |
// Serial.print("% ("); | |
// | |
// if (soilMoisture <= 30) { | |
// Serial.print("Seco"); | |
// } | |
// | |
// if (soilMoisture > 30 && soilMoisture <= 50) { | |
// Serial.print("Moderado"); | |
// } | |
// | |
// if (soilMoisture > 50 && soilMoisture <= 85) { | |
// Serial.print("Umido"); | |
// } | |
// | |
// if (soilMoistureumidity > 85) { | |
// Serial.print("Encharcado"); | |
// } | |
// | |
// Serial.println(")"); | |
// | |
// Serial.print("Temperatura: "); | |
// Serial.print(temperature); | |
// Serial.println("º C"); | |
// Serial.print("Umidade do ar: "); | |
// Serial.print(humidity); | |
// Serial.println("%"); | |
delay(1000); | |
Serial.println("Dados enviados."); | |
} else { | |
Serial.println("Falha ao enviar os dados!"); | |
} | |
client.stop(); | |
} | |
void loop() { | |
delay(500); | |
int soilMoisture = 100 - (analogRead(APIN_SOIL_HUM) * 100 ) / 4095; | |
Serial.print("APIN_SOIL_HUM => "); | |
Serial.print(soilMoisture); | |
Serial.println("%"); | |
int luminosity = 100 - (analogRead(APIN_LDR) * 100) / 4095; | |
Serial.print("APIN_LDR => "); | |
Serial.print(luminosity); | |
Serial.println("%"); | |
int rainIntensity = 100 - (analogRead(APIN_RAIN) * 100) / 4095; | |
Serial.print("APIN_RAIN => "); | |
Serial.print(rainIntensity); | |
Serial.println("%"); | |
boolean isRaining = !(digitalRead(DPIN_RAIN)); | |
Serial.print("DPIN_RAIN => "); | |
Serial.println(isRaining); | |
float humidity = dht.readHumidity(); | |
Serial.print("DPIN_DHT11 H => "); | |
Serial.print(humidity); | |
Serial.println("%"); | |
float temperature = dht.readTemperature(); | |
Serial.print("DPIN_DHT11 T => "); | |
Serial.print(temperature); | |
Serial.println("ºC"); | |
if (isnan(humidity) || isnan(temperature)) { | |
digitalWrite(BUILTIN_LED, HIGH); | |
Serial.println("Falha no sensor DHT11!"); | |
delay(100); | |
digitalWrite(BUILTIN_LED, LOW); | |
delay(100); | |
return; | |
} | |
sendDataToServer(humidity, temperature, soilMoisture, luminosity, rainIntensity, isRaining); | |
Serial.println("Proxima leitura em 60 segundos"); | |
delay(60000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment