Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (84 loc) · 2.36 KB
/
Copy pathmain.cpp
File metadata and controls
98 lines (84 loc) · 2.36 KB
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
#include <Arduino.h>
#include <PubSubClient.h>
#include <WiFi.h>
const char* SSID = "";
const char* PASSWORD = "";
const char* MQTT_SERVER = "broker.hivemq.com";
const char* MQTT_TOPIC = "teste_casa_douglas";
int MQTT_PORT = 1883;
int LED = 13;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
digitalWrite(LED,LOW);
WiFi.begin(SSID, PASSWORD);
Serial.println("Connected!");
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
connectmqtt();
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected())
{
reconnect();
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
if ((char)payload[0] == 'O' && (char)payload[1] == 'N') //on
{
digitalWrite(LED, HIGH);
Serial.println("on");
client.publish(topic, "LED turned ON");
}
else if ((char)payload[0] == 'O' && (char)payload[1] == 'F' && (char)payload[2] == 'F') //off
{
digitalWrite(LED, LOW);
Serial.println(" off");
client.publish(topic, "LED turned OFF");
}
Serial.println();
}
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect("ESP32_clientID")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(MQTT_TOPIC, "Nodemcu connected to MQTT");
// ... and resubscribe
client.subscribe(MQTT_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void connectmqtt()
{
client.connect("ESP32_clientID"); // ESP will connect to mqtt broker with clientID
{
Serial.println("connected to MQTT");
// Once connected, publish an announcement...
// ... and resubscribe
client.subscribe(MQTT_TOPIC); //topic=Demo
client.publish(MQTT_TOPIC, "connected to MQTT");
if (!client.connected())
{
reconnect();
}
}
}