diff --git a/src/main.cpp b/src/main.cpp index cd9bbda..db6cfe7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,9 @@ #include "CpuLoad.h" +#define WEBSOCKETS_SERVER_CLIENT_MAX (20) + + const char *ssid = "LabWork_1"; ESP8266WebServer server(80); @@ -25,7 +28,7 @@ int accMax2 = 0; WebSocketsServer webSocket = WebSocketsServer(81); -void handleAccelerometer() +void handleMainPage() { int16_t bias = 0; const int arr_size = 5000; @@ -33,16 +36,13 @@ void handleAccelerometer() memset(html_code, '\0', sizeof(char) * arr_size); bias += getHtml(HTML_BEGIN, *html_code, arr_size, 0); - // bias += getHtml(STYLE, *html_code, arr_size, bias); - // bias += getHtml(SCRIPT, *html_code, arr_size, bias); - // bias += getHtml(HTML_END, *html_code, arr_size, bias); server.send(200, "text/html", html_code); delete[] html_code; } -void cookAccelerometer(String &in, int *in_array, int array_size) { +void cookDataToJson(String &in, int *in_array, int array_size) { String *output = ∈ int *array = in_array; DynamicJsonBuffer jsonBuffer; @@ -58,7 +58,7 @@ int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) { return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100); } -void updateAccelerometer() +void updateSensors() { if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate)) { @@ -109,14 +109,14 @@ void handleNotFound() server.send(404, "text/plain", message); } -void sendAccelerometerData() { +void sendDataToSocket() { int updateRate = 0; updateRate = 50; if (millis() - lastDataSend > updateRate) { String word; - cookAccelerometer(word, graphAcc.array, graphAcc.size); + cookDataToJson(word, graphAcc.array, graphAcc.size); webSocket.broadcastTXT(word); lastDataSend = millis(); } @@ -147,10 +147,7 @@ void setup(void) Serial.print("AP IP address: "); Serial.println(myIP); - server.on("/accelerometer", handleAccelerometer); - server.on("/", []() { - server.send(200, "text/html", "Main Page"); - }); + server.on("/", handleMainPage); server.onNotFound(handleNotFound); server.begin(); @@ -168,8 +165,8 @@ void loop(void) server.handleClient(); webSocket.loop(); - updateAccelerometer(); - sendAccelerometerData(); + updateSensors(); + sendDataToSocket(); updateCpuTime(1); printCpuLoad();