-
Notifications
You must be signed in to change notification settings - Fork 0
/
internet_comunication.ino
128 lines (90 loc) · 4.36 KB
/
internet_comunication.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
//internet_comunication.ino
#include <HTTPSRedirect.h>
void sendNodeStateUpdate(String fireEventName){sendNodeStateUpdate(fireEventName.c_str());}
void sendNodeStateUpdate(const char* fireEventName)
{
if (!UPLOADING_DATA_MODULE_ENABLED)
return;
if(MAIN_DEBUG) DEBUG_OUTPUT.println(" F:sendNodeStateUpdate(String fireEventName) :" + (String)fireEventName);
showServiceMessage(fireEventName);
flowTemp_global = waterFlowDisplay_global; //Debugging ##
//prepare data to send
String mainTemp = (lastTemp_global != ERROR_TEMP_VALUE_MINUS_127) ? String(lastTemp_global) : "";
String flowTemp = (flowTemp_global != ERROR_TEMP_VALUE_MINUS_127) ? String(flowTemp_global) : "";
String pipeTemp = (pipeTemp_global != ERROR_TEMP_VALUE_MINUS_127) ? String(pipeTemp_global) : "";
String uriParams = "&temp=" + mainTemp + "&flowTemp=" + flowTemp + "&pipeTemp=" + pipeTemp;
String waterFlow = "";
if(waterFlowSensorCount_ISR_global != 0)
{
waterFlow = String(convertWaterFlowSensorImpulsesToLitres(waterFlowSensorCount_ISR_global));
resetflowCount();
}
String current = (lastCurrentMeasurment_global != 0) ? String(lastCurrentMeasurment_global) : "";
uriParams += "&waterFlow=" + waterFlow + "&event=" + fireEventName + "¤t=" + current; // + "&" + "tGlobal" + "=" + t2 + "&" + "tDiff" + "=" + td ;
String heatingControl = getTempControleStyleStringName();
String heatingRelay = (isBoilerHeatingOn()) ? "ON" : "OFF";
uriParams += "&heatingControl_RelayState=" + heatingControl + "&heatingSpiral_RelayState=" + heatingRelay;
//append node identification
char buffer[50];
IPAddress ip = WiFi.localIP();
sprintf(buffer, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
uriParams = "chipId=" + String(ESP.getChipId()) + "&ip=" + URLEncode(buffer) + "&nodeName=" + URLEncode(NODE_NAME) + uriParams;
uriParams += "&objectAskingForResponse=" + URLEncode(getObjectAskingForResponse().c_str()) + "&timeStamp=" + URLEncode(getNowTimeDateString().c_str());
if(!UPLOADING_DATA_MODULE_ENABLED) { DEBUG_OUTPUT.println("Not sending data over WiFi: UPLOADING_DATA_MODULE: DISABLED"); return; }
String postDataToSent = getSystemStateInfo() + ((systemStateInfo_global != "") ? "systemStateInfo=" + systemStateInfo_global : "");
showServiceMessage("Sending");
bool responseOk = postDataToServer(postDataToSent, uriParams);
showServiceMessage("Data sent!");
flowTemp_global++;
lastUpdateTime_global = millis();
}
////////////////////////////////////////////////////////
//POST call from VIKTOR
////////////////////////////////////////////////////////
bool postDataToServer(String postDataToSent, String params)
{
if(MAIN_DEBUG) DEBUG_OUTPUT.println(" F:postDataToServer(String postDataToSent, String params) : \npostDataToSent: \n" + postDataToSent + "\nparams: \n" + params);
const String host = DATA_SERVER_HOST_ADDRESS;
//String url = (String)"/macros/s/AKfycbzXFTXj3rW4Pu2mhSL8t-LgXHQETRlJ7tneod7cGjQu45nvlWc-/exec" + "?" + params; //viktor text file
String url = DATA_SERVER_SCRIPT_URL;
url += + "?" + params;
HTTPSRedirect client;
if(INTERNET_COMMUNICATION_DEBUG) DEBUG_OUTPUT.println((String)"Sending request: " + host + url);
if(INTERNET_COMMUNICATION_DEBUG) DEBUG_OUTPUT.println((String)"Adding POST Data: " + postDataToSent);
//disableInterupts();
if (!client.POST(host, url, postDataToSent))
{
DEBUG_OUTPUT.println("Sending POST error occured");
client.stop();
return false;
}
else if(isSomebodyAskingForResponse())
{
responseText_global = client.readStringUntil('\n');
if(INTERNET_COMMUNICATION_DEBUG) DEBUG_OUTPUT.println((String)"Response text: " + responseText_global);
//delay(100);
}
client.stop();
//enableInterupts();
return true;
}
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
String URLEncode(const char* msg)
{
const char *hex = "0123456789abcdef";
String encodedMsg = "";
while(*msg != '\0') {
if( ('a' <= *msg && *msg <= 'z')
|| ('A' <= *msg && *msg <= 'Z')
|| ('0' <= *msg && *msg <= '9') ) {
encodedMsg += *msg;
} else {
encodedMsg += '%';
encodedMsg += hex[*msg >> 4];
encodedMsg += hex[*msg & 15];
}
msg++;
}
return encodedMsg;
}