forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.ino
178 lines (150 loc) · 5.81 KB
/
Controller.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
//********************************************************************************
// Interface for Sending to Controllers
//********************************************************************************
boolean sendData(struct EventStruct *event)
{
LoadTaskSettings(event->TaskIndex);
if (Settings.UseRules)
createRuleEvents(event->TaskIndex);
if (Settings.GlobalSync && Settings.TaskDeviceGlobalSync[event->TaskIndex])
SendUDPTaskData(0, event->TaskIndex, event->TaskIndex);
if (!Settings.TaskDeviceSendData[event->TaskIndex])
return false;
if (Settings.MessageDelay != 0)
{
uint16_t dif = millis() - lastSend;
if (dif < Settings.MessageDelay)
{
uint16_t delayms = Settings.MessageDelay - dif;
char log[30];
sprintf_P(log, PSTR("HTTP : Delay %u ms"), delayms);
addLog(LOG_LEVEL_DEBUG_MORE, log);
unsigned long timer = millis() + delayms;
while (millis() < timer)
backgroundtasks();
}
}
LoadTaskSettings(event->TaskIndex); // could have changed during background tasks.
if (Settings.Protocol)
{
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
CPlugin_ptr[ProtocolIndex](CPLUGIN_PROTOCOL_SEND, event, dummyString);
}
PluginCall(PLUGIN_EVENT_OUT, event, dummyString);
lastSend = millis();
}
/*********************************************************************************************\
* Handle incoming MQTT messages
\*********************************************************************************************/
// handle MQTT messages
void callback(char* c_topic, byte* b_payload, unsigned int length) {
char log[256];
char c_payload[256];
strncpy(c_payload,(char*)b_payload,length);
c_payload[length] = 0;
statusLED(true);
sprintf_P(log, PSTR("%s%s"), "MQTT : Topic: ", c_topic);
addLog(LOG_LEVEL_DEBUG, log);
sprintf_P(log, PSTR("%s%s"), "MQTT : Payload: ", c_payload);
addLog(LOG_LEVEL_DEBUG, log);
struct EventStruct TempEvent;
TempEvent.String1 = c_topic;
TempEvent.String2 = c_payload;
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
CPlugin_ptr[ProtocolIndex](CPLUGIN_PROTOCOL_RECV, &TempEvent, dummyString);
}
/*********************************************************************************************\
* Connect to MQTT message broker
\*********************************************************************************************/
void MQTTConnect()
{
IPAddress MQTTBrokerIP(Settings.Controller_IP);
MQTTclient.setServer(MQTTBrokerIP, Settings.ControllerPort);
MQTTclient.setCallback(callback);
// MQTT needs a unique clientname to subscribe to broker
String clientid = "ESPClient";
clientid += Settings.Unit;
String subscribeTo = "";
String LWTTopic = Settings.MQTTsubscribe;
LWTTopic.replace("/#", "/status");
LWTTopic.replace("%sysname%", Settings.Name);
for (byte x = 1; x < 3; x++)
{
String log = "";
boolean MQTTresult = false;
//boolean connect(const char* id);
//boolean connect(const char* id, const char* user, const char* pass);
//boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
//boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
if ((SecuritySettings.ControllerUser[0] != 0) && (SecuritySettings.ControllerPassword[0] != 0))
MQTTresult = MQTTclient.connect(clientid.c_str(), SecuritySettings.ControllerUser, SecuritySettings.ControllerPassword, LWTTopic.c_str(), 0, 0, "Connection Lost");
else
MQTTresult = MQTTclient.connect(clientid.c_str(), LWTTopic.c_str(), 0, 0, "Connection Lost");
if (MQTTresult)
{
log = F("MQTT : Connected to broker");
addLog(LOG_LEVEL_INFO, log);
subscribeTo = Settings.MQTTsubscribe;
subscribeTo.replace("%sysname%", Settings.Name);
MQTTclient.subscribe(subscribeTo.c_str());
log = F("Subscribed to: ");
log += subscribeTo;
addLog(LOG_LEVEL_INFO, log);
break; // end loop if succesfull
}
else
{
log = F("MQTT : Failed to connected to broker");
addLog(LOG_LEVEL_ERROR, log);
}
delay(500);
}
}
/*********************************************************************************************\
* Check connection MQTT message broker
\*********************************************************************************************/
void MQTTCheck()
{
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
if (Protocol[ProtocolIndex].usesMQTT)
if (!MQTTclient.connected())
{
String log = F("MQTT : Connection lost");
addLog(LOG_LEVEL_ERROR, log);
connectionFailures += 2;
MQTTclient.disconnect();
delay(1000);
MQTTConnect();
}
else if (connectionFailures)
connectionFailures--;
}
/*********************************************************************************************\
* Send status info to request source
\*********************************************************************************************/
void SendStatus(byte source, String status)
{
switch(source)
{
case VALUE_SOURCE_HTTP:
if (printToWeb)
printWebString += status;
break;
case VALUE_SOURCE_MQTT:
MQTTStatus(status);
break;
case VALUE_SOURCE_SERIAL:
Serial.println(status);
break;
}
}
/*********************************************************************************************\
* Send status info back to channel where request came from
\*********************************************************************************************/
void MQTTStatus(String& status)
{
String pubname = Settings.MQTTsubscribe;
pubname.replace("/#", "/status");
pubname.replace("%sysname%", Settings.Name);
MQTTclient.publish(pubname.c_str(), status.c_str(),Settings.MQTTRetainFlag);
}