#include "WiFi.h"
#include "DHTesp.h"
#include "Wire.h"
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "AdafruitIO_WiFi.h"
//Pinos
#define LED_PIN 02
#define espConectado 14
#define wifiConectado 12
#define serverAtivo 13
#define DHTPIN 15
#define LDR 32
#define IO_USERNAME "marinascmoura"
#define IO_KEY "aio_jRwq73FUA4HWCpdAiyptiGjxpIN5"
//DHT
DHTesp dhtSensor;
//OLED
#define Largura 128 // largura em pixel
#define Comprimento 64 // comprimento em pixel
Adafruit_SSD1306 display(Largura, Comprimento, &Wire, -1);
//WIFI
#define ssid "Wokwi-GUEST"
#define password ""
int status = WL_IDLE_STATUS;
//MQTT IO ADAFRUIT - CONFIG
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define IO_USERNAME "marinascmoura"
#define IO_KEY "aio_jRwq73FUA4HWCpdAiyptiGjxpIN5"
//Servidor & Client
WiFiServer server(80);
int estadoServidor;
String mensagem;
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, IO_USERNAME, IO_KEY);
//AdafruitIO_Feed *digital = io.feed("saida");
Adafruit_MQTT_Subscribe saida = Adafruit_MQTT_Subscribe(&mqtt,IO_USERNAME "/feeds/saida");
//VariaveisGerais
int valorldr;
int valorlumi;
void setup() {
//Inicializa terminais
Serial.begin(115200);
// while(!Serial);
// Serial.print("Conectando MQTT");
// io.connect();
// saida->onMessage(handleMessage);
// while(io.status() < AIO_CONNECTED){
// Serial.print(".");
// delay(500);
// }
//Inicializa dispositivos
dhtSensor.setup(DHTPIN, DHTesp::DHT22);
pinMode(LED_PIN, OUTPUT);
pinMode(espConectado, OUTPUT);
pinMode(wifiConectado, OUTPUT);
pinMode(serverAtivo, OUTPUT);
pinMode(LDR, INPUT);
digitalWrite(espConectado, LOW);
digitalWrite(wifiConectado, LOW);
digitalWrite(serverAtivo, LOW);
digitalWrite(LED_PIN, LOW);
delay(100);
Serial.println("Configuração pronta");
digitalWrite(espConectado, HIGH);
//inicializa WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.println("Conectando: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado");
digitalWrite(wifiConectado, HIGH);
//Informações da rede
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Mac address: ");
Serial.println(WiFi.macAddress());
Serial.print("Subnet: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP());
//Inicialização do Server e MQTT Subscriber
server.begin();
delay(1000);
mensagem = "";
mqtt.subscribe(&saida);
delay(10);
//inicializa o OLED
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
delay(10);
}
void loop() {
//Conexão portal MQTT
//io.run();
MQTT_connect();
WiFiClient client = server.available();
//DHT & LDR
valorldr = analogRead(LDR);
valorlumi = map(valorldr, 4063, 32, 0, 100);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("temp: " + String(data.temperature, 2) + "oC");
Serial.println("Humidade: " + String(data.humidity, 1) + "%");
Serial.println("valor lumi: " + String(valorlumi) + "%");
Serial.println("---");
delay(500);
//MQTT Subscribe
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
if (subscription == &saida)
{
//Serial.println(strcmp((char *)saida.lastread, "1"));
if (strcmp((char *)saida.lastread, "L") == 0)
{
digitalWrite(LED_PIN, HIGH);
}
if (strcmp((char *)saida.lastread, "D") == 0)
{
digitalWrite(LED_PIN, LOW);
}
}
}
if(! mqtt.ping())
{
mqtt.disconnect();
}
//OLED
display.clearDisplay();
display.setCursor(0, 2);
display.println("Rede: ");
display.setCursor(33, 2);
display.print(ssid);
display.setCursor(0, 10);
display.println("IP:");
display.setCursor(35, 10);
display.print(WiFi.localIP());
display.setCursor(0, 17);
display.println("---------------------");
display.setCursor(0, 23);
display.println("temp: ");
display.setCursor(32, 23);
display.print(data.temperature);
display.setCursor(63, 23);
display.print("C");
display.setCursor(0, 32);
display.println("umidade: ");
display.setCursor(50, 32);
display.print(data.humidity);
display.setCursor(88, 32);
display.print("%");
display.setCursor(0, 41);
display.println("lumi: ");
display.setCursor(32, 41);
display.print(valorlumi);
display.setCursor(50, 41);
display.print("%");
display.display();
delay(250);
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Conectando... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Reconectando em 5s...");
mqtt.disconnect();
delay(5000);
retries--;
if (retries == 0) {
while (1);
}
}
Serial.println("Conectado!");
digitalWrite(serverAtivo, HIGH);
}