From 573b934c076e95ddbb3cbe4d0ae4909f780e9842 Mon Sep 17 00:00:00 2001 From: Nikita Romanenko Date: Wed, 13 Dec 2017 10:12:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=B4=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=20=D0=BF=D0=BE=D0=B4=20WebSocket?= =?UTF-8?q?'=D1=8B=20-=20=D1=81=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=20=D1=81=D0=BA=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D0=B2=D0=B0=D0=B5=D1=82=20=D1=81=D1=82=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=86=D1=83,=20=D0=B7=D0=B0=D1=82=D0=B5=D0=BC=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87=D0=B0=D0=B5=D1=82=D1=81?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=20=D1=81=D0=BE=D0=BA=D0=B5=D1=82=D1=83=20?= =?UTF-8?q?=D0=BA=20ESP.=20=D0=94=D0=B0=D0=BB=D0=B5=D0=B5=20ESP=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=87=D0=B8=D0=BD=D0=B0=D0=B5=D1=82=20=D0=BA=D0=B0=D0=B6?= =?UTF-8?q?=D0=B4=D1=8B=D0=B9=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B2=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D1=81=D1=8B=D0=BB=D0=B0=D1=82=D1=8C=20=D0=BC=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=B8=D0=B2=20=D1=81=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=BC=D0=B8=20=D0=BD=D0=B0=20=D0=BA=D0=BB=D0=B8=D0=B5?= =?UTF-8?q?=D0=BD=D1=82,=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D1=8F=20JSON-=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .vscode/launch.json | 4 +- html/acc.html | 32 +++++++++---- src/html.h | 30 ++++++++++--- src/main.cpp | 107 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 135 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index f39f7c9..7cf35aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .pioenvs .piolibdeps .vscode/c_cpp_properties.json +lib diff --git a/.vscode/launch.json b/.vscode/launch.json index a0f79a7..e66e479 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,8 +6,8 @@ "request": "launch", "cwd": "${workspaceRoot}", "name": "PlatformIO Debugger", - "target": "c:/Users/itsmy_000/Desktop/VSCode Workplace/node_mcu_001/.pioenvs/nodemcuv2/firmware.elf", - "gdbpath": "C:/Users/itsmy_000/.platformio/penv/Scripts/piodebuggdb", + "target": "c:/Users/itsmy/Desktop/VSCodeWorkspace/nodemcu-lab-work-1/.pioenvs/nodemcuv2/firmware.elf", + "gdbpath": "C:/Users/itsmy/.platformio/penv/Scripts/piodebuggdb", "autorun": [ "source .pioinit" ] } ] diff --git a/html/acc.html b/html/acc.html index 2064a2e..509432b 100644 --- a/html/acc.html +++ b/html/acc.html @@ -37,20 +37,36 @@
diff --git a/src/html.h b/src/html.h index 83038a2..fae25fb 100644 --- a/src/html.h +++ b/src/html.h @@ -41,7 +41,6 @@ const char style[] PROGMEM = { }\n\ \n\ \n\ - \n\ \n\ \n\ \n\ @@ -57,14 +56,33 @@ const char style[] PROGMEM = { const char script_begin[] PROGMEM = { "\n\ + "}; -const char script_end[] PROGMEM = { - "context.stroke();\n\ - \n"}; +const char script_end[] PROGMEM = {""}; const char html_end[] PROGMEM = { "
\n\ diff --git a/src/main.cpp b/src/main.cpp index 27dda38..edeab6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,34 +1,38 @@ #include #include -#include +//#include #include -#include +//#include #include #include #include +#include +#include const char *ssid = "NodeMCU"; ESP8266WebServer server(80); - - Sensor accelerometer = Sensor(); - Graph graphAcc = Graph(200, 50); +int lastDataSend = 0; + +WebSocketsServer webSocket = WebSocketsServer(81); + void handleAccelerometer() -{ +{ int16_t bias = 0; const int arr_size = 10000; char *html_code = new char[arr_size]; - memset(html_code, '\0', sizeof(char)*arr_size); + 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_BEGIN, *html_code, arr_size, bias); - for (int i = 0; i != graphAcc.graphSize - 1; i++) { - bias += snprintf((html_code+bias), arr_size - bias, "context.lineTo(%i*2, %i);\n", i, graphAcc.graphArray[i]); - } + // for (int i = 0; i != graphAcc.graphSize - 1; i++) + // { + // bias += snprintf((html_code + bias), arr_size - bias, "context.lineTo(%i*2, %i);\n", i, graphAcc.graphArray[i]); + // } bias += getHtml(SCRIPT_END, *html_code, arr_size, bias); //bias += snprintf((html_code+bias), arr_size - bias, "
Current accelerometer: %i", acc); bias += getHtml(HTML_END, *html_code, arr_size, bias); @@ -38,20 +42,38 @@ void handleAccelerometer() delete[] html_code; } +void cookAccelerometer(String &in, int *in_array, int array_size) { + String *output = ∈ + int *array = in_array; + DynamicJsonBuffer jsonBuffer; + JsonObject& root = jsonBuffer.createObject(); + JsonArray& data = root.createNestedArray("data"); + for (int i = 0; i != array_size; i++) { + data.add(*(array + i)); + } + root.printTo(*output); +} + void updateAccelerometer() { if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.graphUpdateRate)) { accelerometer.lastValue = map(analogRead(A0), 512, 768, 200, 0); - if (graphAcc.graphCounter < graphAcc.graphSize - 1) { + if (graphAcc.graphCounter < graphAcc.graphSize - 1) + { graphAcc.graphArray[graphAcc.graphCounter] = accelerometer.lastValue; graphAcc.graphCounter++; - } else { - for(int i = 0; i != graphAcc.graphCounter; i++) { + } + else + { + for (int i = 0; i != graphAcc.graphCounter; i++) + { graphAcc.graphArray[i] = graphAcc.graphArray[i + 1]; - } - graphAcc.graphArray[graphAcc.graphCounter] = accelerometer.lastValue; + } + graphAcc.graphArray[graphAcc.graphCounter] = accelerometer.lastValue; } + + accelerometer.lastUpdate = millis(); } } @@ -73,28 +95,67 @@ void handleNotFound() server.send(404, "text/plain", message); } +void sendAccelerometerData() { + if (millis() - lastDataSend > 100) { + String word; + cookAccelerometer(word, graphAcc.graphArray, graphAcc.graphSize); + webSocket.broadcastTXT(word); + lastDataSend = millis(); + } +} + +//** WEBSOCKET TEST **// +void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) +{ + switch (type) + { + case WStype_DISCONNECTED: + Serial.printf("[%u] Disconnected!\n", num); + break; + case WStype_CONNECTED: + { + IPAddress ip = webSocket.remoteIP(num); + Serial.println("Client connected!"); + Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload); + } + break; + } +} +//** WEBSOCKET TEST END**// + void setup(void) { + Serial.begin(115200); + Serial.setDebugOutput(true); + Serial.println(); + pinMode(A0, INPUT); - Serial.begin(115200); WiFi.softAP(ssid); - IPAddress myIP = WiFi.softAPIP(); - Serial.print("AP IP address: "); - Serial.println(myIP); - + IPAddress myIP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(myIP); + server.on("/accelerometer", handleAccelerometer); server.on("/", []() { - server.send(200, "text/plain", "http://192.168.4.1/accelerometer"); + server.send(200, "text/html", "Main Page"); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); + + //** WEBSOCKET TEST **// + + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + //** WEBSOCKET TEST END**// } void loop(void) -{ - updateAccelerometer(); +{ server.handleClient(); + webSocket.loop(); + updateAccelerometer(); + sendAccelerometerData(); } \ No newline at end of file