-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathController.ino
More file actions
169 lines (145 loc) · 5.37 KB
/
Controller.ino
File metadata and controls
169 lines (145 loc) · 5.37 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
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
//********************************************************************************
// 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();
}
/*********************************************************************************************\
* 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_SERIAL:
Serial.println(status);
break;
}
}
#if FEATURE_MQTT
/*********************************************************************************************\
* Handle incoming MQTT messages
\*********************************************************************************************/
// handle MQTT messages
void callback(char* c_topic, byte* b_payload, unsigned int length) {
char c_payload[384];
strncpy(c_payload,(char*)b_payload,length);
c_payload[length] = 0;
statusLED(true);
String log;
log=F("MQTT : Topic: ");
log+=c_topic;
addLog(LOG_LEVEL_DEBUG, log);
log=F("MQTT : Payload: ");
log+=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 = F("ESPClient");
clientid += Settings.Unit;
String subscribeTo = "";
String LWTTopic = Settings.MQTTsubscribe;
LWTTopic.replace(F("/#"), F("/status"));
LWTTopic.replace(F("%sysname%"), Settings.Name);
String log = "";
boolean MQTTresult = false;
String msg = F("Connection Lost");
if ((SecuritySettings.ControllerUser[0] != 0) && (SecuritySettings.ControllerPassword[0] != 0))
MQTTresult = MQTTclient.connect(clientid.c_str(), SecuritySettings.ControllerUser, SecuritySettings.ControllerPassword, LWTTopic.c_str(), 0, 0, msg.c_str());
else
MQTTresult = MQTTclient.connect(clientid.c_str(), LWTTopic.c_str(), 0, 0, msg.c_str());
if (MQTTresult)
{
log = F("MQTT : Connected to broker");
addLog(LOG_LEVEL_INFO, log);
subscribeTo = Settings.MQTTsubscribe;
subscribeTo.replace(F("%sysname%"), Settings.Name);
MQTTclient.subscribe(subscribeTo.c_str());
log = F("Subscribed to: ");
log += subscribeTo;
addLog(LOG_LEVEL_INFO, log);
}
else
{
log = F("MQTT : Failed to connected to broker");
addLog(LOG_LEVEL_ERROR, log);
}
}
/*********************************************************************************************\
* Check connection MQTT message broker
\*********************************************************************************************/
void MQTTCheck()
{
byte ProtocolIndex = getProtocolIndex(Settings.Protocol);
if (Protocol[ProtocolIndex].usesMQTT)
if (!MQTTclient.connected())
{
if (millis() - lastMQTTReconnectAttempt > 60000) {
// Reconnect attempts once per minute
String log = F("MQTT : Connection lost");
addLog(LOG_LEVEL_ERROR, log);
connectionFailures++;
MQTTConnect();
lastMQTTReconnectAttempt = millis();
}
}
else if (connectionFailures)
connectionFailures--;
}
/*********************************************************************************************\
* Send status info back to channel where request came from
\*********************************************************************************************/
void MQTTStatus(String& status)
{
String pubname = Settings.MQTTsubscribe;
pubname.replace(F("/#"), F("/status"));
pubname.replace(F("%sysname%"), Settings.Name);
MQTTclient.publish(pubname.c_str(), status.c_str(),Settings.MQTTRetainFlag);
}
#endif