#include "Arduino.h"
#include "WiFi.h"
#include <HTTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
HTTPClient client;
char nomeWifi[] = "Wokwi-GUEST";
char senhaWifi[] = "";
char serverAddress[] = "https://api.tago.io/data";
char contentHeader[] = "application/json";
char tokenHeader[] = "65060ee2-215e-49e4-8fb1-7e8b1f2cdf70";
DHT dht(15, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define LDR_PIN 4
#define GREEN_LED_PIN 19
#define YELLOW_LED_PIN 18
#define RED_LED_PIN 5
#define BUZZER_PIN 2
int luminosity = 0;
char luminosityP[] = "";
int humi = 0;
int temp = 0;
/*
int i = 0;
long acumHumi = 0;
long acumTemp = 0;
*/
int MODO_OK = 870;
int MODO_ALERTA = 950;
int humi_OK = 50;
int humi_ALERTA = 70;
int temp_OK = 10;
int temp_ALERTA = 15;
int systemState = 0;
void setup() {
lcd.init();
lcd.clear();
lcd.backlight();
dht.begin();
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
init_wifi();
}
void Temp_Rod() {
if (temp < temp_OK) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. Baixa ");
lcd.setCursor(0, 1);
lcd.print("Temp. = ");
lcd.println(temp);
tone(BUZZER_PIN, 1000, 3000);
digitalWrite(YELLOW_LED_PIN, HIGH);
} else if (temp < temp_ALERTA) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. OK ");
lcd.setCursor(0, 1);
lcd.print("Temp. = ");
lcd.println(temp);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp. ALTA ");
lcd.setCursor(0, 1);
lcd.print("Temp. = ");
lcd.println(temp);
tone(BUZZER_PIN, 1000, 3000);
digitalWrite(RED_LED_PIN, HIGH);
}
}
void Humi_Rod() {
if (humi < humi_OK) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Umid. Baixa ");
lcd.setCursor(0, 1);
lcd.print("Umid. = ");
lcd.println(humi);
tone(BUZZER_PIN, 1000, 3000);
digitalWrite(YELLOW_LED_PIN, HIGH);
} else if (humi < humi_ALERTA) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Umid. OK ");
lcd.setCursor(0, 1);
lcd.print("Umid. = ");
lcd.println(humi);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Umid. ALTA ");
lcd.setCursor(0, 1);
lcd.print("Umid. =");
lcd.println(humi);
tone(BUZZER_PIN, 1000, 3000);
digitalWrite(RED_LED_PIN, HIGH);
}
}
void init_wifi() {
Serial.println("Conectando WiFi");
Serial.print("Wifi: ");
Serial.println(nomeWifi);
WiFi.begin(nomeWifi, senhaWifi);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("WiFi Conectado");
Serial.print("Meu IP eh: ");
Serial.println(WiFi.localIP());
}
void send_infos() {
int statusCode = 0;
char postData[500];
// Construa o JSON com os dados a serem enviados
snprintf(postData, sizeof(postData), "{\n"
"\t\"variable\": \"temperature\",\n"
"\t\"value\": %.2f,\n"
"\t\"unit\": \"ºC\"\n"
"}\n"
"{\n"
"\t\"variable\": \"humidity\",\n"
"\t\"value\": %d,\n"
"\t\"unit\": \"%\"\n"
"}\n"
"{\n"
"\t\"variable\": \"luminosity\",\n"
"\t\"value\": %d\n"
"}\n",
temp, humi, luminosity);
client.begin(serverAddress);
client.addHeader("Content-Type", contentHeader);
client.addHeader("Device-Token", tokenHeader);
statusCode = client.POST(postData);
delay(2000);
// Leia o código de status e o corpo da resposta
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.println("End of POST to TagoIO");
Serial.println();
}
void loop() {
int luminosity = analogRead(LDR_PIN);
humi = dht.readHumidity();
temp = dht.readTemperature();
Serial.print("\n LDR = ");
Serial.print(luminosity);
Serial.print("\n Temperatura C = ");
Serial.print(temp);
Serial.print("\n Humidade: ");
Serial.print(humi);
delay(2000);
if (luminosity <= MODO_OK) {
systemState = 0; // OK
char luminosityP[] = "Ambiente OK";
} else if (luminosity <= MODO_ALERTA) {
systemState = 1; // Alerta
char luminosityP[] = "Ambiente Meia Luz";
} else {
systemState = 2; // Problema
char luminosityP[] = "Ambiente Claro";
}
switch (systemState) {
case 0:
// Ambiente OK
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
noTone(BUZZER_PIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ambiente OK");
break;
case 1:
// Ambiente em Alerta
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
noTone(BUZZER_PIN);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ambiente a meia");
lcd.setCursor(0, 1);
lcd.print("Luz");
break;
case 2:
// Ambiente em Problema
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
tone(BUZZER_PIN, 1000, 3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ambiente muito ");
lcd.setCursor(0, 1);
lcd.print("CLARO");
break;
default:
// Estado desconhecido
printf("Estado desconhecido");
break;
}
Temp_Rod();
delay(1000);
Humi_Rod();
delay(1000);
send_infos(); // Envie a temperatura para o servidor TagoIO
delay(5000);
}