forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_C008.ino
140 lines (123 loc) · 4.53 KB
/
_C008.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
//#######################################################################################################
//########################### Controller Plugin 008: Generic HTTP #######################################
//#######################################################################################################
#define CPLUGIN_008
#define CPLUGIN_ID_008 8
#define CPLUGIN_NAME_008 "Generic HTTP"
boolean CPlugin_008(byte function, struct EventStruct *event)
{
boolean success = false;
switch (function)
{
case CPLUGIN_PROTOCOL_ADD:
{
Protocol[++protocolCount].Number = CPLUGIN_ID_008;
strcpy_P(Protocol[protocolCount].Name, PSTR(CPLUGIN_NAME_008));
Protocol[protocolCount].usesMQTT = false;
Protocol[protocolCount].usesAccount = false;
Protocol[protocolCount].usesPassword = false;
Protocol[protocolCount].defaultPort = 80;
break;
}
case CPLUGIN_PROTOCOL_SEND:
{
switch (event->sensorType)
{
case SENSOR_TYPE_SINGLE: // single value sensor, used for Dallas, BH1750, etc
case SENSOR_TYPE_SWITCH:
case SENSOR_TYPE_DIMMER:
HTTPSend(event, 0, UserVar[event->BaseVarIndex]);
break;
case SENSOR_TYPE_LONG: // single LONG value, stored in two floats (rfid tags)
HTTPSend(event, 0, (unsigned long)UserVar[event->BaseVarIndex] + ((unsigned long)UserVar[event->BaseVarIndex + 1] << 16));
break;
case SENSOR_TYPE_TEMP_HUM:
case SENSOR_TYPE_TEMP_BARO:
HTTPSend(event, 0, UserVar[event->BaseVarIndex]);
unsigned long timer = millis() + Settings.MessageDelay;
while (millis() < timer)
backgroundtasks();
HTTPSend(event, 1, UserVar[event->BaseVarIndex + 1]);
break;
}
break;
}
}
return success;
}
//********************************************************************************
// Generic HTTP get request
//********************************************************************************
boolean HTTPSend(struct EventStruct *event, byte varIndex, float value)
{
char log[80];
boolean success = false;
char host[20];
sprintf_P(host, PSTR("%u.%u.%u.%u"), Settings.Controller_IP[0], Settings.Controller_IP[1], Settings.Controller_IP[2], Settings.Controller_IP[3]);
sprintf_P(log, PSTR("%s%s"), "HTTP : connecting to ", host);
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, Settings.ControllerPort))
{
connectionFailures++;
strcpy_P(log, PSTR("HTTP : connection failed"));
addLog(LOG_LEVEL_ERROR, log);
if (printToWeb)
printWebString += F("connection failed<BR>");
return false;
}
if (connectionFailures)
connectionFailures--;
if (ExtraTaskSettings.TaskDeviceValueNames[0][0] == 0)
PluginCall(PLUGIN_GET_DEVICEVALUENAMES, event, dummyString);
String url = "/";
url += Settings.MQTTpublish;
url.replace("%sysname%", Settings.Name);
url.replace("%tskname%", ExtraTaskSettings.TaskDeviceName);
url.replace("%id%", String(event->idx));
url.replace("%valname%", ExtraTaskSettings.TaskDeviceValueNames[varIndex]);
url.replace("%value%", String(value));
Serial.print("URL: ");
Serial.println(url);
url.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
if (printToWeb)
{
printWebString += log;
printWebString += "<BR>";
}
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\n');
line.toCharArray(log, 80);
addLog(LOG_LEVEL_DEBUG_MORE, log);
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
{
strcpy_P(log, PSTR("HTTP : Succes!"));
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
printWebString += F("Success<BR>");
success = true;
}
delay(1);
}
strcpy_P(log, PSTR("HTTP : closing connection"));
addLog(LOG_LEVEL_DEBUG, log);
if (printToWeb)
printWebString += F("closing connection<BR>");
client.flush();
client.stop();
}