Hi,
for communication between ESP8266 devices, I have so far used the simplest method, i.e. communication over the HTTP protocol using a specific link. Unfortunately, this solution (perhaps incorrectly written and modified by me) sometimes causes some problems.
- First of all, it happens that another ESP8266 module is turned off for some time, so that the HTTP request is sent to the ESP8266 module not connected at the moment - this causes very long delays and sometimes a restart of the ESP2866. I send a lot of such requests (even 4 every 500ms). Is there a solution to this problem? Perhaps imposing a smaller timeout while waiting to receive data (for example, 300ms)?
- Another problem is how I process the received content return data. For the moment, I am using the method I show below (using the GETRequestData[] array). I think this method is not good, it is inefficient, and it constantly generates heap memory clogging with received Strings of different lengths. Wouldn't it perhaps be better to return the content itself without the code using return? And what if the data is not returned correctly (use return false?)?
The code:
String GETRequestData[2] = {"", ""};
static int GETGateLampDataArray[5];
void sendGETRequest(String URL) {
GETRequestData[0] = "";
GETRequestData[1] = "";
if (WiFi.status() == WL_CONNECTED) { // WiFi connection check
WiFiClient client;
HTTPClient http;
http.begin(client, URL.c_str());
int httpResponseCode = http.GET(); // Send GET
if (httpResponseCode > 0) {
GETRequestData[0] = String(httpResponseCode);
GETRequestData[1] = http.getString();
} else {
logThis("HTTP Error (httpResponseCode): " + String(httpResponseCode));
}
http.end();
} else {
logThis("No WiFi");
}
}
// Update every 500ms
void updateDataOverHTTP() {
sendGETRequest("http://192.168.0.66/xyzData");
GETGateLampDataArray[0] = GETRequestData[1].charAt(0) - '0'; // GETGateLampDataArray[0] - dane o stanie lampek
GETGateLampDataArray[1] = GETRequestData[1].charAt(2) - '0'; // GETGateLampDataArray[1] - dane o stanie pozycji bramy
GETGateLampDataArray[2] = GETRequestData[1].charAt(4) - '0'; // GETGateLampDataArray[2] - dane o wartości enkodera
GETGateLampDataArray[3] = GETRequestData[1].charAt(6) - '0'; // GETGateLampDataArray[3] - dane o stanie blokady bramy
}
- Another issue is the question of how to receive data from another device, of which there will be many and will contain values in the form of double. On the device that has the data, I would like to use a function similar to the following. I would like to separate the data with the _ character, for example. But how to receive the later data efficiently and split it into single variables on the ESP8266 device, which will send a query and receive these return values?
The code I used always to send multiple data:
server.on("/xDataZ", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(volts) + "_" + String(wats) + "_" + String(amps) + "_" + String(hz));
});
Example test from content:
231.4_2970_11.2_49.99