-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathair-humidifier.ino
92 lines (62 loc) · 1.81 KB
/
air-humidifier.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
#include <WiFi.h>
#include <PubSubClient.h>
const char* SSID = "FIAP-IBM";
const char* PASSWORD = "Challenge@23!";
const char* BROKER_MQTT = "46.17.108.113";
const int BROKER_PORT = 1883;
const char* TOPICO_PUBLISH = "/TEF/lamp118/attrs";
const char* ID_MQTT = "fiware_118";
const int ledPin = 26 ; // Pino G14 para o LED
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Conecta-se à rede Wi-Fi
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando ao WiFi...");
}
Serial.println("Conectado ao WiFi");
// Conecta-se ao broker MQTT
client.setServer(BROKER_MQTT, BROKER_PORT);
client.setCallback(callback);
}
void callback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.print("Mensagem recebida no tópico: ");
Serial.println(topic);
Serial.print("Conteúdo da mensagem: ");
Serial.println(message);
if (String(topic) == TOPICO_PUBLISH) {
if (message == "s|on") {
digitalWrite(ledPin, HIGH); // Liga o LED
Serial.println("LED ligado");
} else if (message == "s|off") {
digitalWrite(ledPin, LOW); // Desliga o LED
Serial.println("LED desligado");
}
}
}
void reconnect() {
while (!client.connected()) {
Serial.println("Conectando ao MQTT Broker...");
if (client.connect(ID_MQTT)) {
Serial.println("Conectado ao MQTT Broker");
client.subscribe(TOPICO_PUBLISH);
} else {
Serial.println("Falha na conexão ao MQTT Broker. Tentando novamente em 5 segundos...");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}