#include <WiFi.h>
#include <ArduinoJson.h>
#include <DHTesp.h>
#include <PubSubClient.h>
// Configurações de WiFi
const char *SSID = "Wokwi-GUEST";
const char *PASSWORD = ""; // Substitua pelo sua senha
// Configurações de MQTT
const char *BROKER_MQTT = "broker.hivemq.com";
const int BROKER_PORT = 1883;
const char *ID_MQTT = "esp32_mqtt";
const char *TOPIC_SUBSCRIBE_LED = "fiap/iot/led";
const char *TOPIC_PUBLISH_TEMP_HUMI = "fiap/iot/temphumi";
const int pinRed = 15;
const int pinGreen = 2;
const int pinBlue = 4;
const int buttonPin = 13;
const int buzzerPin = 5;
int currentColor = 1; // 1 = vermelho, 2 = verde, etc.
bool ledOn = true;
unsigned long buttonPressTime = 0;
bool buttonPressed = false;
bool longPressHandled = false;
const unsigned long debounceDelay = 50;
unsigned long lastDebounceTime = 0;
void updateLed();
void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(pinBlue, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
Serial.begin(115200);
updateLed();
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && !buttonPressed) {
if (millis() - lastDebounceTime > debounceDelay) {
buttonPressed = true;
buttonPressTime = millis();
longPressHandled = false;
lastDebounceTime = millis();
}
}
if (buttonState == LOW && buttonPressed) {
if (!longPressHandled && millis() - buttonPressTime > 1000) {
ledOn = !ledOn;
updateLed();
longPressHandled = true;
}
}
if (buttonState == HIGH && buttonPressed) {
if (millis() - lastDebounceTime > debounceDelay) {
buttonPressed = false;
lastDebounceTime = millis();
if (!longPressHandled && ledOn) {
currentColor++;
if (currentColor > 7) currentColor = 1;
updateLed();
}
}
}
}
void updateLed() {
if (!ledOn) {
analogWrite(pinRed, 0);
analogWrite(pinGreen, 0);
analogWrite(pinBlue, 0);
noTone(buzzerPin);
Serial.println("LED desligado");
return;
}
switch (currentColor) {
case 1:
analogWrite(pinRed, 255);
analogWrite(pinGreen, 0);
analogWrite(pinBlue, 0);
tone(buzzerPin, 500);
Serial.println("Problema Mecânico");
break;
case 2:
analogWrite(pinRed, 0);
analogWrite(pinGreen, 255);
analogWrite(pinBlue, 0);
tone(buzzerPin, 1000);
Serial.println("Documentação Pendente");
break;
case 3:
analogWrite(pinRed, 0);
analogWrite(pinGreen, 0);
analogWrite(pinBlue, 255);
tone(buzzerPin, 1500);
Serial.println("Problema Elétrico");
break;
case 4:
analogWrite(pinRed, 255);
analogWrite(pinGreen, 255);
analogWrite(pinBlue, 0);
tone(buzzerPin, 2000);
Serial.println("Problema Estético");
break;
case 5:
analogWrite(pinRed, 255);
analogWrite(pinGreen, 50);
analogWrite(pinBlue, 0);
tone(buzzerPin, 2500);
Serial.println("Problema Segurança");
break;
case 6:
analogWrite(pinRed, 150);
analogWrite(pinGreen, 0);
analogWrite(pinBlue, 0);
tone(buzzerPin, 3000);
Serial.println("Problemas Múltiplos");
break;
case 7:
analogWrite(pinRed, 130);
analogWrite(pinGreen, 0);
analogWrite(pinBlue, 255);
tone(buzzerPin, 3500);
Serial.println("Motocicleta Pronta para Uso");
break;
}
}