#if defined(ESP32)
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#define DEVICE "ESP32"
#elif defined(ESP8266)
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti;
#define DEVICE "ESP8266"
#endif
#include <InfluxDbClient.h>
#include <InfluxDbCloud.h>
// WiFi AP SSID
#define WIFI_SSID "Wokwi-GUEST"
// WiFi password
#define WIFI_PASSWORD ""
#define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com"
#define INFLUXDB_TOKEN "x17ikEXhDzjcFkWDNT1WAhNSw_SCOaTfcbm4Lzqsb5hujbaJ0rQo_cPmRBNo2P6MiJ9bMYJ0P-kQ8adfoGafzQ=="
#define INFLUXDB_ORG "350ed6bb3f49c50a"
#define INFLUXDB_BUCKET "Aplicacao01"
// Time zone info
#define TZ_INFO "UTC-3"
// Declare InfluxDB client instance with preconfigured InfluxCloud certificate
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
// Declare Data point
Point sensor("wifi_status");
//Adiciona bibliotecas
#include "DHTesp.h"
//Definição de pinos
#define button 2
#define red 23
#define blue 21
#define green 22
#define potentiometer 14
#define buzzer 15
#define photoresistor 27
const int DHT_PIN = 32;
bool sysState = true;
bool ledON = true;
float temperature;
void IRAM_ATTR Ext_INT1_ISR(){
sysState = !sysState;
}
DHTesp dhtSensor;
void setup() {
//Definição PinModes
pinMode(potentiometer, INPUT);
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(button, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(photoresistor, INPUT);
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(button), Ext_INT1_ISR, RISING);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
WiFi.mode(WIFI_STA);
wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to wifi");
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Accurate time is necessary for certificate validation and writing in batches
// We use the NTP servers in your area as provided by: https://www.pool.ntp.org/zone/
// Syncing progress and the time will be printed to Serial.
timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov");
// Check server connection
if (client.validateConnection()) {
Serial.print("Connected to InfluxDB: ");
Serial.println(client.getServerUrl());
} else {
Serial.print("InfluxDB connection failed: ");
Serial.println(client.getLastErrorMessage());
}
sensor.addTag("device", DEVICE);
sensor.addTag("SSID", WiFi.SSID());
}
float read_temp(){
float Temp = dhtSensor.getTemperature();
return Temp;
}
void led_color(){
int pot = analogRead(potentiometer);
if(pot >= 0 && pot < 1366){
analogWrite(red,4095-pot);
analogWrite(green,pot);
analogWrite(blue,0);
}else if(pot >= 1366 && pot < 2732){
analogWrite(red,pot);
analogWrite(green,pot);
analogWrite(blue,pot);
}else if(pot >= 2732){
analogWrite(red,0);
analogWrite(green,4095-pot);
analogWrite(blue,pot);
}
}
void out_temperature(float temperature){
if(temperature > 30 || temperature < 0){
Serial.println("Perigo! Desligar!");
tone(buzzer,262);
} else {
tone(buzzer,0);
}
}
void read_photores(){
if(!digitalRead(photoresistor) || temperature > 30 || temperature < 0){
turnoff_led();
}
else
ledON = true;
}
void turnoff_led(){
analogWrite(red,0);
analogWrite(green,0);
analogWrite(blue,0);
ledON = false;
}
void enviar_bd(){
// Clear fields for reusing the point. Tags will remain the same as set above.
sensor.clearFields();
// Store measured value into point
// Report RSSI of currently connected network
sensor.addField("temperature", temperature);
sensor.addField("sistema", sysState);
sensor.addField("estado_led", ledON);
// Print what are we exactly writing
Serial.print("Writing: ");
Serial.println(sensor.toLineProtocol());
// Check WiFi connection and reconnect if needed
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi connection lost");
}
// Write point
if (!client.writePoint(sensor)) {
Serial.print("InfluxDB write failed: ");
Serial.println(client.getLastErrorMessage());
}
Serial.println("Waiting 1 second");
delay(1000);
}
void loop() {
enviar_bd();
if (sysState){
led_color();
read_photores();
temperature = read_temp();
out_temperature(temperature);
} else {
turnoff_led();
tone(15,0);
delay(50);
}
if(ledON){
led_color();
Serial.println("Led ligado");
}
else{
Serial.println("Led desligado");
}
}