#include "DHTesp.h"
#include "ESP32Servo.h"
#include "WiFi.h"
#include "HTTPClient.h"
#include "UrlEncode.h"
//DHT========
const int DHT_PIN = 15; // Pino do DHT22
DHTesp dhtSensor;
//Gas========
const int MQ2_PIN = 34; // Pino analógico do MQ-2 (GPIO34)
//Bia aqui
float ppmGas;
/*
//Photoresist========
//#define digital_In 13
int analog_In = 35; //pra fzr a conv pra lux
const float GAMMA = 0.7;
//constante da curva de resposta do sensor
const float RL10 = 50;
//valor tipico da resistencia a 10 lux
int photoValue;*/
int luxPin = 12;
int luxValue;
int interruptPin = 19;
const int servo1Pin = 16; // Pino para o servo 1
const int servo2Pin = 17; // Pino para o servo 2
volatile int val = 0;
unsigned long startTime;
//unsigned long wpTime;
volatile unsigned long interruptTime;
volatile unsigned long lastInterruptTime = 0;
volatile bool interrupt = false;
volatile bool outOfInterrupt = true;
String phoneNumber = "5571984274277";
String apiKey = "9111783";
//VARIÁVEIS RN ---------------
float lifeChance = 80;
bool wrong = false;
//---------------
Servo myServo1; // Cria um objeto Servo para o primeiro servo
Servo myServo2; // Cria um objeto Servo para o segundo servo
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//Pinos dos sensores
pinMode(MQ2_PIN, INPUT);
//pinMode(digital_In, INPUT);
//pinMode(analog_In, INPUT);
pinMode(luxPin, INPUT);
//Pino da interrupção
attachInterrupt(interruptPin, interruptFunction, CHANGE);
//Servo motores
myServo1.attach(servo1Pin); // Associa o servo 1 ao pino 16
myServo2.attach(servo2Pin); // Associa o servo 2 ao pino 17
//CONECTA AO WI_FI ---------------
Serial.print("Conectando-se ao Wi-Fi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Conectado!");
//---------------
}
void loop() {
int angle = 0;
Serial.println("Iniciou contagem de 1s");
//inicia contagem de tempo
startTime = millis();
float tempValue;
float humidityValue;
if ((interrupt == false) && (outOfInterrupt == true)){
while ((millis() - startTime <= 1000) && (interrupt == false) && (outOfInterrupt == true)){
//LÊ VALORES DOS SENSORES ---------------
//Leitura valores DHT22
TempAndHumidity data = dhtSensor.getTempAndHumidity();
tempValue = data.temperature;
humidityValue = data.humidity;
//Leitura valores Gas
int gasValue = analogRead(MQ2_PIN);
ppmGas = (map(gasValue, 0, 4095, 0.1, 300)) + 0.1;
//Leitura valores Photoresist
//valor analógico
luxValue = analogRead(luxPin);
/*
float voltage = analogValue / 4096. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
photoValue = map(lux, 0, 100000, 150, 1100);
*/
//valor digital
//int lux_d = digitalRead(digital_In);
//---------------
//SERVOS ATIVOS SEM INTERRUPÇÃO ---------------
for(angle = 0; angle <= 180; angle++) {
myServo1.write(angle); // Ajusta o ângulo do servo 1
myServo2.write(angle); // Ajusta o ângulo do servo 2 (opcional, se você quiser que os dois se movam juntos)
//servoTime = millis();
if ((interrupt == false) && (wrong == false)){
if (((millis() - startTime == 500) || (millis() - startTime == 1000)) && (wrong == false)){
//PRINTA VALORES DOS SENSORES ---------------
Serial.print("Analog - Lux: ");
Serial.println(luxValue);
/*
if (lux_d == 0){
Serial.println("Digital: Light");
}else {
Serial.println("Digital: Dark");
}*/
Serial.println("Temp: " + String(tempValue, 2) + " °C");
Serial.println("Hum: " + String(humidityValue, 1) + " %");
Serial.println("MQ-2: " + String(ppmGas));
Serial.println(" ");
Serial.println("----------------------------------");
//---------------
if (millis() - startTime >= 1000){
startTime = millis();
//TESTAR CHANCE DE VIDA ---------------
//---------------
if (lifeChance >= 80){
wrong = true;
lifeChance = 0;
sendMessage("Temp: " + String(tempValue, 2) + " °C");
} else if ((lifeChance >= 50) && (lifeChance <80)){
//condição moderada
} else if (lifeChance <50){
//ambiente hostil
}
}
}
}
}
// Segundo loop vai de 180 até 0 para o servo 1
for(angle = 180; angle >= 0; angle--) {
myServo1.write(angle); // Ajusta o ângulo do servo 1
myServo2.write(angle); // Ajusta o ângulo do servo 2 (opcional)
//servoTime = millis();
if ((interrupt == false) && (wrong == false)){
if (((millis() - startTime == 500) || (millis() - startTime == 1000)) && (wrong == false)){
//PRINTA VALORES DOS SENSORES ---------------
Serial.print("Analog - Lux: ");
Serial.println(luxValue);
/*
if (lux_d == 0){
Serial.println("Digital: Light");
}else {
Serial.println("Digital: Dark");
}*/
Serial.println("Temp: " + String(tempValue, 2) + " °C");
Serial.println("Hum: " + String(humidityValue, 1) + " %");
Serial.println("MQ-2: " + String(ppmGas));
Serial.println(" ");
Serial.println("----------------------------------");
//---------------
if (millis() - startTime >= 1000){
startTime = millis();
//TESTAR CHANCE DE VIDA ---------------
//---------------
if (lifeChance >= 80){
wrong = true;
lifeChance = 0;
sendMessage("Temp: " + String(tempValue, 2) + " °C");
} else if ((lifeChance >= 50) && (lifeChance <80)){
//condição moderada
} else if (lifeChance <50){
//ambiente hostil
}
}
}
}
}
}
}
if (interrupt == true){
val++;
if(val == 1){
Serial.println("Interrupt");
interrupt = false;
myServo1.write(90);
myServo2.write(90);
outOfInterrupt = false;
//sendMessage("Temp: " + String(tempValue, 2) + " °C");
//sendMessage("Temperatura:"+String(data.temperature,2)+"C" + "\n" + "Humidade:"+String(data.humidity,1)+"%")
}
if(val == 2){
Serial.println("Saiu da interrupt");
interrupt = false;
outOfInterrupt = true;
val = 0;
Serial.println("Iniciou contagem de 1s");
startTime = millis();
}
}
delay(2000);
}
void interruptFunction(){
interruptTime = millis();
if (interruptTime - lastInterruptTime > 200){
interrupt = true;
}
lastInterruptTime = interruptTime;
}
void sendMessage(String message){
// Data to send with HTTP POST
//String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&text=" + "This+is+a+test" + "&apikey=" + apiKey;
String url = "https://api.callmebot.com/whatsapp.php?phone=557184274277&text=" + urlEncode(message) + "&apikey=9111783";
HTTPClient http;
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200) {
Serial.print("Message sent successfully");
} else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
startTime = millis();
wrong = false;
// Free resources
http.end();
}