forked from delorean-dev/bhonofre-estores
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bhonofreestores.ino
209 lines (179 loc) · 5.76 KB
/
bhonofreestores.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <Timing.h> //https://github.com/scargill/Timing
//MQTT
#include <PubSubClient.h> //https://www.youtube.com/watch?v=GMMH6qT8_f4
//ESP
#include <ESP8266WiFi.h> //https://www.youtube.com/watch?v=4d8joORYTIA
//Wi-Fi Manger library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>//https://github.com/tzapu/WiFiManager , https://www.youtube.com/watch?v=wWO9n5DnuLA
//OTA
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Bounce2.h> //https://github.com/thomasfredericks/Bounce-Arduino-Wiring
#define AP_TIMEOUT 60
#define SERIAL_BAUDRATE 115200
//CONFIGURAR O SERVIDOR MQTT
#define MQTT_BROKER_IP "0.0.0.0" /////////////////////////// COLOCA AQUI O IP DO TEU BROKER MQTT
#define MQTT_BROKER_PORT 1883
#define MQTT_AUTH true
#define MQTT_USERNAME "" //////////////////////////////////// COLOCA AQUI O USERNAME DO TEU BROKER MQTT
#define MQTT_PASSWORD "" //////////////////////////////////// COLOCA AQUI A PASSWORD DO TEU BROKER MQTT
#define PAYLOAD_OPEN "OPEN"
#define PAYLOAD_CLOSE "CLOSE"
#define PAYLOAD_STOP "STOP"
//OUTPUTS
#define COVER_OPEN_RELAY 04
#define COVER_CLOSE_RELAY 05
//INPUTS
#define SWITCH_OPEN 12
#define SWITCH_CLOSE 13
//CONSTANTS
const String HOSTNAME = "CoverController";
const char * OTA_PASSWORD = "otapower";
const String MQTT_LOG = "system/log";
const String MQTT_SYSTEM_CONTROL_TOPIC = "system/set";
const char *MQTT_CONTROL_TOPIC = "home/kitchen/window/set";
const char *MQTT_STATE_TOPIC = "home/kitchen/window/state";
//CONTROL FLAGS
bool OTA = false;
bool OTABegin = false;
String lastState = "-";
String lastStateNotified = "NONE";
bool stoped = false;
WiFiClient wclient;
PubSubClient client(MQTT_BROKER_IP,MQTT_BROKER_PORT,wclient);
Bounce debouncerOpen = Bounce();
Bounce debouncerClose = Bounce();
void setup() {
Serial.begin(115200);
WiFiManager wifiManager;
//reset saved settings
//wifiManager.resetSettings();
/*define o tempo limite até o portal de configuração ficar novamente inátivo,
útil para quando alteramos a password do AP*/
wifiManager.setTimeout(AP_TIMEOUT);
wifiManager.autoConnect(HOSTNAME.c_str());
client.setCallback(callback);
pinMode(COVER_OPEN_RELAY,OUTPUT);
pinMode(COVER_CLOSE_RELAY,OUTPUT);
pinMode( SWITCH_OPEN,INPUT_PULLUP);
pinMode( SWITCH_CLOSE,INPUT_PULLUP);
debouncerOpen.attach(SWITCH_OPEN);
debouncerOpen.interval(5);//DELAY
debouncerClose.attach(SWITCH_CLOSE);
debouncerClose.interval(5);//DELAY
}
void openCover(){
digitalWrite(COVER_CLOSE_RELAY,LOW);
digitalWrite(COVER_OPEN_RELAY,HIGH);
if(!lastStateNotified.equals(PAYLOAD_OPEN)){
lastStateNotified = PAYLOAD_OPEN;
client.publish(MQTT_STATE_TOPIC,PAYLOAD_OPEN,true);
Serial.println("OPEN");
}
}
void closeCover(){
digitalWrite(COVER_OPEN_RELAY,LOW);
digitalWrite(COVER_CLOSE_RELAY,HIGH);
if(!lastStateNotified.equals(PAYLOAD_CLOSE)){
lastStateNotified = PAYLOAD_CLOSE;
client.publish(MQTT_STATE_TOPIC,PAYLOAD_CLOSE,true);
Serial.println("CLOSE");
}
}
void stopCover(){
digitalWrite(COVER_OPEN_RELAY,LOW);
digitalWrite(COVER_CLOSE_RELAY,LOW);
if(!lastStateNotified.equals(PAYLOAD_STOP)){
lastStateNotified = PAYLOAD_STOP;
client.publish(MQTT_STATE_TOPIC,PAYLOAD_STOP,true);
Serial.println("STOP");
}
}
//Chamada de recepção de mensagem
void callback(char* topic, byte* payload, unsigned int length) {
String payloadStr = "";
for (int i=0; i<length; i++) {
payloadStr += (char)payload[i];
}
Serial.print("MQTT RECEIVED: ");
Serial.println(payloadStr);
String topicStr = String(topic);
if(topicStr.equals(MQTT_SYSTEM_CONTROL_TOPIC)){
if(payloadStr.equals("OTA_ON_"+String(HOSTNAME))){
OTA = true;
OTABegin = true;
}else if (payloadStr.equals("OTA_OFF_"+String(HOSTNAME))){
OTA = false;
OTABegin = false;
}else if (payloadStr.equals("REBOOT_"+String(HOSTNAME))){
ESP.restart();
}
}else if ( topicStr.equals(MQTT_CONTROL_TOPIC)){
if(payloadStr.equals(PAYLOAD_OPEN)){
openCover();
}else if (payloadStr.equals(PAYLOAD_CLOSE)){
closeCover();
}else if (payloadStr.equals(PAYLOAD_STOP)){
stopCover();
}
}
}
bool checkMqttConnection(){
if (!client.connected()) {
if (MQTT_AUTH ? client.connect(HOSTNAME.c_str(),MQTT_USERNAME, MQTT_PASSWORD) : client.connect(HOSTNAME.c_str())) {
//SUBSCRIÇÃO DE TOPICOS
Serial.println("CONNECTED ON MQTT");
client.subscribe(MQTT_SYSTEM_CONTROL_TOPIC.c_str());
client.subscribe(MQTT_CONTROL_TOPIC);
//Envia uma mensagem por MQTT para o tópico de log a informar que está ligado
client.publish(MQTT_LOG.c_str(),(String(HOSTNAME)+" CONNECTED").c_str());
}
}
return client.connected();
}
void loop() {
debouncerOpen.update();
debouncerClose.update();
if (WiFi.status() == WL_CONNECTED) {
if (checkMqttConnection()){
client.loop();
int realStateOpen = debouncerOpen.read();
int realStateClose = debouncerClose.read();
//OPEN
if(!realStateOpen){
stoped=false;
openCover();
}
//CLOSE
if(!realStateClose){
stoped=false;
closeCover();
}
//STOP
if(realStateOpen && realStateClose){
if(!stoped){
stoped=true;
stopCover();
}
}
if(OTA){
if(OTABegin){
setupOTA();
OTABegin= false;
}
ArduinoOTA.handle();
}
}
}
}
void setupOTA(){
if (WiFi.status() == WL_CONNECTED && checkMqttConnection()) {
client.publish(MQTT_LOG.c_str(),(String(HOSTNAME)+" OTA IS SETUP").c_str());
ArduinoOTA.setHostname(HOSTNAME.c_str());
ArduinoOTA.setPassword((const char *)OTA_PASSWORD);
ArduinoOTA.begin();
client.publish(MQTT_LOG.c_str(),(String(HOSTNAME)+" OTA IS READY").c_str());
}
}