-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoConnectWithMQTT.ino
More file actions
110 lines (90 loc) · 2.67 KB
/
AutoConnectWithMQTT.ino
File metadata and controls
110 lines (90 loc) · 2.67 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
99
100
101
102
103
104
105
106
107
108
109
110
#include <FS.h> //this needs to be first, or it all crashes and burns...
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <PubSubClient.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#include <Button.h>
#include <ButtonEventCallback.h>
#include <PushButton.h>
#include "pin.h"
#include "device.h"
#include "config.h"
// 1 led, 1 button
#define LED 0
#define BUTTON 2
#define DEBUG_MODE 1 //1 set debug on
PushButton button = PushButton(BUTTON);
Pin led(LED);
Device device;
void callback(char* topic, byte* payload, unsigned int length);
WiFiClient espClient;
PubSubClient client(espClient);
void led_to(int pin, int value){
digitalWrite(pin, value);
Serial.println("change");
client.publish(device.publish_channel(), "change");
}
// Functions
void callback(char* topic, byte* payload, unsigned int length) {
if ((char)payload[0] == '1') {
led_to(LED, LOW);
} else {
led_to(LED, HIGH);
}
}
//callbacks functions
void pressFunc(Button& btn){
client.publish(device.publish_channel(), "Pressed");
}
void setup() {
pinMode(LED, OUTPUT);
button.onPress(pressFunc);
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000); // for debuging only
setup_config();
client.setServer(mqtt_server, atoi(mqtt_port));
client.setCallback(callback);
// --------------
device.set(device_name, device_id);
Serial.println("setup ended");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(device.publish_channel(), "connected");
Serial.println("publish to: ");
Serial.println(device.publish_channel());
// ... and resubscribe
Serial.println("subcribe to: ");
Serial.println(device.subscribe_channel());
client.subscribe(device.subscribe_channel());
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
Serial.println("re connecting");
reconnect();
}
client.loop();
button.update();
}