forked from defend/nodemcu-lab-1
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.1 KiB
152 lines
4.1 KiB
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
//#include <WiFiClient.h>
|
|
#include <ESP8266WebServer.h>
|
|
//#include <ESP8266mDNS.h>
|
|
#include <html.h>
|
|
#include <Graph.h>
|
|
#include <Sensor.h>
|
|
#include <WebSocketsServer.h>
|
|
#include <ArduinoJson.h>
|
|
#include "CpuLoad.h"
|
|
|
|
const char *ssid = "LabWork_1";
|
|
|
|
ESP8266WebServer server(80);
|
|
Sensor accelerometer = Sensor();
|
|
Graph graphAcc = Graph(200, 50);
|
|
|
|
long 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);
|
|
|
|
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) {
|
|
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);
|
|
}
|
|
|
|
int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) {
|
|
return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100);
|
|
}
|
|
|
|
// TODO: необходимо перекомпоновать функцию updateAccelerometer()
|
|
void updateAccelerometer()
|
|
{
|
|
if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.graphUpdateRate))
|
|
{
|
|
int filteredAcc = 0;
|
|
accelerometer.lastValue = map(analogRead(A0), 368, 768, 200, 0);
|
|
|
|
if (graphAcc.graphCounter != 0) {
|
|
//filteredAcc = lowFreqFilter(accelerometer.lastValue, graphAcc.graphArray[graphAcc.graphCounter - 1], 0.5);
|
|
filteredAcc = graphAcc.graphArray[graphAcc.graphCounter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.graphArray[graphAcc.graphCounter - 1]);
|
|
}
|
|
|
|
if (graphAcc.graphCounter < graphAcc.graphSize - 1)
|
|
{
|
|
graphAcc.graphArray[graphAcc.graphCounter] = filteredAcc;
|
|
graphAcc.graphCounter++;
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i != graphAcc.graphCounter; i++)
|
|
{
|
|
graphAcc.graphArray[i] = graphAcc.graphArray[i + 1];
|
|
}
|
|
graphAcc.graphArray[graphAcc.graphCounter] = filteredAcc;
|
|
}
|
|
|
|
|
|
accelerometer.lastUpdate = millis();
|
|
}
|
|
}
|
|
|
|
|
|
void handleNotFound()
|
|
{
|
|
String message = "File Not Found\n\n";
|
|
message += "URI: ";
|
|
message += server.uri();
|
|
message += "\nMethod: ";
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
message += "\nArguments: ";
|
|
message += server.args();
|
|
message += "\n";
|
|
for (uint8_t i = 0; i < server.args(); i++)
|
|
{
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
}
|
|
server.send(404, "text/plain", message);
|
|
}
|
|
|
|
void sendAccelerometerData() {
|
|
if (millis() - lastDataSend > 50) {
|
|
String word;
|
|
cookAccelerometer(word, graphAcc.graphArray, graphAcc.graphSize);
|
|
webSocket.broadcastTXT(word);
|
|
lastDataSend = millis();
|
|
}
|
|
}
|
|
|
|
|
|
void setup(void)
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.setDebugOutput(true);
|
|
Serial.println();
|
|
|
|
pinMode(A0, INPUT);
|
|
WiFi.setAutoConnect(false);
|
|
WiFi.softAP(ssid);
|
|
IPAddress myIP = WiFi.softAPIP();
|
|
Serial.print("AP IP address: ");
|
|
Serial.println(myIP);
|
|
|
|
server.on("/accelerometer", handleAccelerometer);
|
|
server.on("/", []() {
|
|
server.send(200, "text/html", "<a href=\"http://192.168.4.1/accelerometer\">Main Page</a>");
|
|
});
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
|
|
webSocket.begin();
|
|
}
|
|
|
|
void loop(void)
|
|
{
|
|
updateCpuTime(0);
|
|
|
|
server.handleClient();
|
|
webSocket.loop();
|
|
updateAccelerometer();
|
|
sendAccelerometerData();
|
|
|
|
updateCpuTime(1);
|
|
printCpuLoad();
|
|
}
|