forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_C002.ino
215 lines (196 loc) · 7.71 KB
/
_C002.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
210
211
212
213
214
//#######################################################################################################
//########################### Controller Plugin 002: Domoticz MQTT ######################################
//#######################################################################################################
#define CPLUGIN_002
#define CPLUGIN_ID_002 2
#define CPLUGIN_NAME_002 "Domoticz MQTT"
boolean CPlugin_002(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_002;
Protocol[protocolCount].usesMQTT = true;
Protocol[protocolCount].usesAccount = true;
Protocol[protocolCount].usesPassword = true;
Protocol[protocolCount].defaultPort = 1883;
break;
}
case CPLUGIN_GET_DEVICENAME:
{
string = F(CPLUGIN_NAME_002);
break;
}
case CPLUGIN_PROTOCOL_TEMPLATE:
{
strcpy_P(Settings.MQTTsubscribe, PSTR("domoticz/out"));
strcpy_P(Settings.MQTTpublish, PSTR("domoticz/in"));
break;
}
case CPLUGIN_PROTOCOL_RECV:
{
char json[512];
json[0] = 0;
event->String2.toCharArray(json, 512);
//Serial.println(event->String2);
StaticJsonBuffer<512> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (root.success())
{
long idx = root["idx"];
float nvalue = root["nvalue"];
long nvaluealt = root["nvalue"];
const char* name = root["name"];
const char* svalue = root["svalue"];
const char* svalue1 = root["svalue1"];
const char* svalue2 = root["svalue2"];
const char* svalue3 = root["svalue3"];
const char* switchtype = root["switchType"];
if (nvalue == 0)
nvalue = nvaluealt;
for (byte x = 0; x < TASKS_MAX; x++)
{
if (Settings.TaskDeviceID[x] == idx)
{
if (Settings.TaskDeviceNumber[x] == 1) // temps solution, if input switch, update state
{
String action = F("inputSwitchState,");
action += x;
action += ",";
action += nvalue;
struct EventStruct TempEvent;
parseCommandString(&TempEvent, action);
PluginCall(PLUGIN_WRITE, &TempEvent, action);
}
if (Settings.TaskDeviceNumber[x] == 29) // temp solution, if plugin 029, set gpio
{
String action = "";
int baseVar = x * VARS_PER_TASK;
struct EventStruct TempEvent;
if (strcasecmp_P(switchtype, PSTR("dimmer")) == 0)
{
int pwmValue = UserVar[baseVar];
action = F("pwm,");
action += Settings.TaskDevicePin1[x];
action += ",";
switch ((int)nvalue)
{
case 0:
pwmValue = 0;
break;
case 1:
pwmValue = UserVar[baseVar];
break;
case 2:
pwmValue = 10 * atol(svalue1);
UserVar[baseVar] = pwmValue;
break;
}
action += pwmValue;
}
else
{
UserVar[baseVar] = nvalue;
action = F("gpio,");
action += Settings.TaskDevicePin1[x];
action += ",";
action += nvalue;
}
parseCommandString(&TempEvent, action);
PluginCall(PLUGIN_WRITE, &TempEvent, action);
}
}
}
}
break;
}
case CPLUGIN_PROTOCOL_SEND:
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["idx"] = event->idx;
String values;
char str[80];
switch (event->sensorType)
{
case SENSOR_TYPE_SINGLE: // single value sensor, used for Dallas, BH1750, etc
root["nvalue"] = 0;
values = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
values.toCharArray(str, 80);
root["svalue"] = str;
break;
case SENSOR_TYPE_LONG: // single LONG value, stored in two floats (rfid tags)
root["nvalue"] = 0;
values = (unsigned long)UserVar[event->BaseVarIndex] + ((unsigned long)UserVar[event->BaseVarIndex + 1] << 16);
values.toCharArray(str, 80);
root["svalue"] = str;
break;
case SENSOR_TYPE_TEMP_HUM: // temp + hum + hum_stat, used for DHT11
root["nvalue"] = 0;
values = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
values += ";";
values += toString(UserVar[event->BaseVarIndex + 1],ExtraTaskSettings.TaskDeviceValueDecimals[1]);
values += ";0";
values.toCharArray(str, 80);
root["svalue"] = str;
break;
case SENSOR_TYPE_TEMP_BARO: // temp + hum + hum_stat + bar + bar_fore, used for BMP085
root["nvalue"] = 0;
values = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
values += ";0;0;";
values += toString(UserVar[event->BaseVarIndex + 1],ExtraTaskSettings.TaskDeviceValueDecimals[1]);
values += ";0";
values.toCharArray(str, 80);
root["svalue"] = str;
break;
case SENSOR_TYPE_TEMP_HUM_BARO: // temp + hum + hum_stat + bar + bar_fore, used for BME280
root["nvalue"] = 0;
values = toString(UserVar[event->BaseVarIndex],ExtraTaskSettings.TaskDeviceValueDecimals[0]);
values += ";";
values += toString(UserVar[event->BaseVarIndex + 1],ExtraTaskSettings.TaskDeviceValueDecimals[1]);
values += ";0;";
values += toString(UserVar[event->BaseVarIndex + 2],ExtraTaskSettings.TaskDeviceValueDecimals[2]);
values += ";0";
values.toCharArray(str, 80);
root["svalue"] = str;
break;
case SENSOR_TYPE_SWITCH:
root["command"] = "switchlight";
if (UserVar[event->BaseVarIndex] == 0)
root["switchcmd"] = "Off";
else
root["switchcmd"] = "On";
break;
case SENSOR_TYPE_DIMMER:
root["command"] = "switchlight";
if (UserVar[event->BaseVarIndex] == 0)
root["switchcmd"] = "Off";
else
root["Set%20Level"] = UserVar[event->BaseVarIndex];
break;
}
char json[256];
root.printTo(json, sizeof(json));
String log = F("MQTT : ");
log += json;
addLog(LOG_LEVEL_DEBUG, json);
String pubname = Settings.MQTTpublish;
pubname.replace("%sysname%", Settings.Name);
pubname.replace("%tskname%", ExtraTaskSettings.TaskDeviceName);
pubname.replace("%id%", String(event->idx));
if (!MQTTclient.publish(pubname, json))
{
log = F("MQTT publish failed");
addLog(LOG_LEVEL_DEBUG, json);
MQTTConnect();
connectionFailures++;
}
else if (connectionFailures)
connectionFailures--;
break;
}
}
return success;
}