-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.ino
149 lines (122 loc) · 3.69 KB
/
sketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
// Configurações de WiFi
const char *SSID = "Wokwi-GUEST";
const char *PASSWORD = "";
// 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_CONSUMO = "fiap/iot/consumoea";
// Configurações de Hardware
#define PIN_POTENTIOMETER 34 // Pino analógico do potenciômetro
#define PIN_LED 2 // Pino do LED
#define PUBLISH_DELAY 2000 // Intervalo de publicação (2 segundos)
// Variáveis globais
WiFiClient espClient;
PubSubClient MQTT(espClient);
unsigned long publishUpdate = 0;
int consumoEnergia = 0; // Valor simulado de consumo
bool ledLigado = false;
// Protótipos de funções
void initWiFi();
void initMQTT();
void callbackMQTT(char *topic, byte *payload, unsigned int length);
void reconnectMQTT();
void reconnectWiFi();
void checkWiFIAndMQTT();
void initWiFi() {
Serial.print("Conectando com a rede: ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("Conectado com sucesso: ");
Serial.println(SSID);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void initMQTT() {
MQTT.setServer(BROKER_MQTT, BROKER_PORT);
MQTT.setCallback(callbackMQTT);
}
void callbackMQTT(char *topic, byte *payload, unsigned int length) {
String msg = String((char*)payload).substring(0, length);
StaticJsonDocument<200> json;
DeserializationError error = deserializeJson(json, msg);
if (error) {
Serial.println("Erro ao processar JSON recebido.");
return;
}
if (json.containsKey("led")) {
int valor = json["led"];
if (valor == 1) {
digitalWrite(PIN_LED, HIGH);
ledLigado = true;
Serial.println("LED ligado via MQTT");
} else if (valor == 0) {
digitalWrite(PIN_LED, LOW);
ledLigado = false;
Serial.println("LED desligado via MQTT");
}
}
}
void reconnectMQTT() {
while (!MQTT.connected()) {
Serial.print("Tentando conectar ao broker MQTT: ");
Serial.println(BROKER_MQTT);
if (MQTT.connect(ID_MQTT)) {
Serial.println("Conectado ao broker MQTT!");
MQTT.subscribe(TOPIC_SUBSCRIBE_LED); // Assina o tópico para controle do LED
} else {
Serial.println("Falha na conexão. Tentando novamente em 2 segundos.");
delay(2000);
}
}
}
void reconnectWiFi() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.println("Wi-Fi conectado com sucesso!");
}
}
void checkWiFIAndMQTT() {
if (WiFi.status() != WL_CONNECTED) reconnectWiFi();
if (!MQTT.connected()) reconnectMQTT();
}
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
digitalWrite(PIN_LED, LOW); // Inicialmente o LED está desligado
initWiFi();
initMQTT();
}
void loop() {
checkWiFIAndMQTT();
MQTT.loop();
if ((millis() - publishUpdate) >= PUBLISH_DELAY) {
publishUpdate = millis();
// Simula o consumo de energia com base no valor do potenciômetro
int potValue = analogRead(PIN_POTENTIOMETER);
consumoEnergia = map(potValue, 0, 4095, 0, 100); // Converte para 0-100%
// Publica os dados de consumo e estado do LED no MQTT
StaticJsonDocument<200> doc;
doc["consumo"] = consumoEnergia;
doc["led"] = ledLigado ? "on" : "off";
char buffer[200];
serializeJson(doc, buffer);
MQTT.publish(TOPIC_PUBLISH_CONSUMO, buffer);
// Exibe no monitor serial
Serial.println(buffer);
}
}