Compare commits

...

8 Commits

  1. 1
      .gitignore
  2. 73
      html/hbs.html
  3. BIN
      lib/FFT.zip
  4. 3
      platformio.ini
  5. 6
      src/CpuLoad.h
  6. 1
      src/Graph.cpp
  7. 1
      src/Graph.h
  8. 24
      src/html.h
  9. 73
      src/main.cpp

1
.gitignore

@ -6,3 +6,4 @@
.vscode/launch.json .vscode/launch.json
.vscode .vscode
.vscode/.browse.c_cpp.db* .vscode/.browse.c_cpp.db*
.lib/readme.txt

73
html/hbs.html

@ -1,73 +0,0 @@
<!DOCTYPE html>
<html>
<style>
button {
width: 190px;
height: 40px;
}
#navs {
height: 40px;
width: 400px;
position: relative;
}
footer {
height: 5%;
text-align: center;
width: 100%;
margin-top: 100px;
}
.clear {
clear: left;
}
#hbs_text {
text-align: left;
width: 400px;
text-indent: 20px;
}
</style>
<body>
<div id="main_block" align="center">
<h2>Лабораторная работа *НАЗВАНИЕ*</h2>
<h3>Высокочувствительный оптодатчик ADPD2214</h3>
<p id="hbs_text">Применяется для измерения пульса, расчета фотоплетизмограммы. Низкое энергопотребление, а также управляемое выключение
этого электроприбора позволяет использовать его в портативных устройствах.
</p>
<p id="hbs_text">Также применяется в химическом анализе. Частота обновления графика - 1 раз в двадцать секунд. Данные с сенсора снимаются
с частотой в 20Hz. </p>
<canvas id="myCanvas" width="400" height="200" style="border:1px solid #3c70ff;">
Браузер устарел!
</canvas>
<script>
var c = document.getElementById("myCanvas");
var context = c.getContext("2d");
var randomNumbers, randNumsLength;
context.moveTo(0, 0);
for (i = 0; i != 400; i += 4) {
context.lineTo(i, Math.random() * 100 + 50);
}
context.stroke();
context.font = "50px Arial";
context.fillText("122",300,170);
</script>
<div id="navs">
<button onclick="window.location.href='/acc.html?f=0'" class="button">Акселерометр</button>
</div>
<div class="clear"></div>
</div>
<footer>
Тамбовский Государственный Технический Университет 2017 г.
</footer>
</body>
</html>

BIN
lib/FFT.zip

Binary file not shown.

3
platformio.ini

@ -6,4 +6,5 @@ platform = espressif8266
board = nodemcuv2 board = nodemcuv2
framework = arduino framework = arduino
monitor_baud = 115200 monitor_baud = 115200
lib_deps = ./lib/FFT.zip, ./lib/Json.zip, ./lib/WebSockets.zip board_f_cpu = 160000000L
lib_deps = ./lib/Json.zip, ./lib/WebSockets.zip

6
src/CpuLoad.h

@ -1,4 +1,4 @@
#include <arduino.h> #include <Arduino.h>
// updateCpuTime отвечает за обновление статистики загрузки Cpu // updateCpuTime отвечает за обновление статистики загрузки Cpu
// updateCpuTime(false) - размещается в начале loop() // updateCpuTime(false) - размещается в начале loop()
@ -30,7 +30,9 @@ static void printCpuTime () {
static void printCpuLoad () { static void printCpuLoad () {
if (millis() - cpuTimeLastUpdate > 2000) { if (millis() - cpuTimeLastUpdate > 2000) {
Serial.printf("CPU Load: %ld \n", cpuLoadAvgTime/10000); int x = cpuLoadAvgTime/10000;
Serial.printf("CPU Load: %ld \n", x);
if (x > 400) ESP.restart();
cpuTimeLastUpdate = millis(); cpuTimeLastUpdate = millis();
} }
} }

1
src/Graph.cpp

@ -4,7 +4,6 @@ Graph::Graph (int graphSize, int graphUpdateRate) {
this->size = graphSize; this->size = graphSize;
this->updateRate = graphUpdateRate; // Раз в секунду this->updateRate = graphUpdateRate; // Раз в секунду
array = new int[size]; array = new int[size];
fft = new int[size/2];
source = new int[size]; source = new int[size];
counter = 0; counter = 0;
} }

1
src/Graph.h

@ -3,7 +3,6 @@ class Graph {
int size; int size;
int updateRate; int updateRate;
int * array; int * array;
int * fft;
int * source; int * source;
int counter; int counter;

24
src/html.h

@ -48,11 +48,7 @@ const char html_begin[] PROGMEM = R"(
<body> <body>
<div id="main_block" align="center"> <div id="main_block" align="center">
<h1>Лабораторная работа</h1> <h1>Диагностика</h1>
<h2>Акселерометр ADW22035Z - Cемейство ADXL103</h2>
<p id="hbs_text">Акселерометр подает на выход данные по одной оси.</p>
<p id="hbs_text">Частота обновления графика - 1 раз в секунду. Данные с сенсора снимаются
с частотой в 50Hz. </p>
<canvas id="myCanvas" width="440" height="240" style="border:0px solid #000000;"> <canvas id="myCanvas" width="440" height="240" style="border:0px solid #000000;">
Браузер устарел! Браузер устарел!
</canvas> </canvas>
@ -63,7 +59,8 @@ const char html_begin[] PROGMEM = R"(
var context = c.getContext("2d"); var context = c.getContext("2d");
var maxX = 400, maxY = 200; var maxX = 400, maxY = 200;
var biasX = 20, biasY = 20; var biasX = 20, biasY = 20;
var maxVoltage = 20;
var freq = 4/4;
function iY(y) { function iY(y) {
return maxY + biasY - y; return maxY + biasY - y;
} }
@ -74,7 +71,7 @@ const char html_begin[] PROGMEM = R"(
context.lineTo(20, iY(i)); context.lineTo(20, iY(i));
context.lineTo(15, iY(i)); context.lineTo(15, iY(i));
context.moveTo(20, iY(i)); context.moveTo(20, iY(i));
textY = i/200*3.3; textY = i/200*maxVoltage;
context.fillText(textY.toFixed(1), 0, iY(i-2.5)); context.fillText(textY.toFixed(1), 0, iY(i-2.5));
} }
context.moveTo(20, iY(0)); context.moveTo(20, iY(0));
@ -82,7 +79,7 @@ const char html_begin[] PROGMEM = R"(
context.lineTo(i, iY(0)); context.lineTo(i, iY(0));
context.lineTo(i, iY(-5)); context.lineTo(i, iY(-5));
context.moveTo(i, iY(0)); context.moveTo(i, iY(0));
textX = (i-20)/400*4 ; textX = (i-20)/400*freq ;
context.fillText(textX.toFixed(1), i-10, iY(-20)); context.fillText(textX.toFixed(1), i-10, iY(-20));
} }
context.stroke(); context.stroke();
@ -114,7 +111,7 @@ const char html_begin[] PROGMEM = R"(
<div class="clear"></div> <div class="clear"></div>
</div> </div>
<footer> <footer>
<hr \>Тамбовский Государственный Технический Университет 2017-2018 г. <hr \>DEFEND STUDIO | DEFEND.PW | 2018
</footer> </footer>
</body> </body>
</html> </html>
@ -136,15 +133,6 @@ static int getHtml(parts p, char &adr, uint16_t buf_size, uint16_t bias)
case HTML_BEGIN: case HTML_BEGIN:
html_part = html_begin; html_part = html_begin;
break; break;
// case HTML_END:
// html_part = html_end;
// break;
// case STYLE:
// html_part = style;
// break;
// case SCRIPT:
// html_part = script;
// break;
case ACC_CALIBRATE: case ACC_CALIBRATE:
html_part = acc_calibrate; html_part = acc_calibrate;
break; break;

73
src/main.cpp

@ -8,21 +8,19 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "CpuLoad.h" #include "CpuLoad.h"
#include "arduinoFFT.h"
bool fftMode = true; #define WEBSOCKETS_SERVER_CLIENT_MAX (20)
arduinoFFT FFT = arduinoFFT();
const char *ssid = "LabWork_1"; const char *ssid = "LabWork_1";
ESP8266WebServer server(80); ESP8266WebServer server(80);
Sensor accelerometer = Sensor(); Sensor accelerometer = Sensor();
Graph graphAcc = Graph(256, 50); Graph graphAcc = Graph(200, 200);
double vReal[256];
double vImag[256];
long int lastDataSend = 0; long int lastDataSend = 0;
long int lastTest = 0;
bool testState = false;
int timeOfCal = 0; int timeOfCal = 0;
bool accCalibrated = false; bool accCalibrated = false;
@ -32,7 +30,7 @@ int accMax2 = 0;
WebSocketsServer webSocket = WebSocketsServer(81); WebSocketsServer webSocket = WebSocketsServer(81);
void handleAccelerometer() void handleMainPage()
{ {
int16_t bias = 0; int16_t bias = 0;
const int arr_size = 5000; const int arr_size = 5000;
@ -40,16 +38,13 @@ void handleAccelerometer()
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(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); server.send(200, "text/html", html_code);
delete[] 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 = &in; String *output = &in;
int *array = in_array; int *array = in_array;
DynamicJsonBuffer jsonBuffer; DynamicJsonBuffer jsonBuffer;
@ -61,11 +56,15 @@ void cookAccelerometer(String &in, int *in_array, int array_size) {
root.printTo(*output); root.printTo(*output);
} }
void eatDataFromJson() {
}
int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) { int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) {
return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100); return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100);
} }
void updateAccelerometer() void updateSensors()
{ {
if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate)) if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate))
{ {
@ -81,7 +80,6 @@ void updateAccelerometer()
if (graphAcc.counter < graphAcc.size - 1) if (graphAcc.counter < graphAcc.size - 1)
{ {
graphAcc.array[graphAcc.counter] = filteredSignal; graphAcc.array[graphAcc.counter] = filteredSignal;
graphAcc.source[graphAcc.counter] = sourceSignal;
graphAcc.counter++; graphAcc.counter++;
} }
else else
@ -89,22 +87,8 @@ void updateAccelerometer()
for (int i = 0; i != graphAcc.counter; i++) for (int i = 0; i != graphAcc.counter; i++)
{ {
graphAcc.array[i] = graphAcc.array[i + 1]; graphAcc.array[i] = graphAcc.array[i + 1];
graphAcc.source[i] = graphAcc.source[i + 1];
} }
graphAcc.array[graphAcc.counter] = filteredSignal; graphAcc.array[graphAcc.counter] = filteredSignal;
graphAcc.source[graphAcc.counter] = sourceSignal;
//start fft
for (int i = 0; i < graphAcc.size; i++) {
vReal[i] = (double) graphAcc.source[i];
vImag[i] = 0;
}
FFT.Windowing(vReal, graphAcc.size, FFT_WIN_TYP_RECTANGLE, FFT_FORWARD);
FFT.Compute(vReal, vImag, graphAcc.size, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, graphAcc.size);
for (int i = 0; i < graphAcc.size/2; i++) {
graphAcc.fft[i] = map((int) vReal[i], 0, 15000, 200, 0);
}
} }
accelerometer.lastUpdate = millis(); accelerometer.lastUpdate = millis();
@ -128,17 +112,15 @@ void handleNotFound()
server.send(404, "text/plain", message); server.send(404, "text/plain", message);
} }
void sendAccelerometerData() { void sendDataToSocket() {
int updateRate = 0; int updateRate = 0;
if (fftMode) updateRate = 200; updateRate = 50;
else updateRate = graphAcc.updateRate;
if (millis() - lastDataSend > updateRate) { if (millis() - lastDataSend > updateRate) {
String word; String word;
cookAccelerometer(word, graphAcc.fft, graphAcc.size/2); cookDataToJson(word, graphAcc.array, graphAcc.size);
webSocket.broadcastTXT(word); webSocket.broadcastTXT(word);
Serial.println(word);
lastDataSend = millis(); lastDataSend = millis();
} }
} }
@ -152,6 +134,19 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length
} }
} }
void selfTest(){
if (millis() - lastTest > 19) {
if (!testState) {
digitalWrite(D0, HIGH);
testState = true;
} else {
digitalWrite(D0, LOW);
testState = false;
}
lastTest = millis();
}
}
void setup(void) void setup(void)
@ -168,10 +163,7 @@ void setup(void)
Serial.print("AP IP address: "); Serial.print("AP IP address: ");
Serial.println(myIP); Serial.println(myIP);
server.on("/accelerometer", handleAccelerometer); server.on("/", handleMainPage);
server.on("/", []() {
server.send(200, "text/html", "<a style=\"font-size:10em;2\"href=\"http://192.168.1.1/accelerometer\">Main Page</a>");
});
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.begin(); server.begin();
@ -181,6 +173,7 @@ void setup(void)
webSocket.onEvent(webSocketEvent); webSocket.onEvent(webSocketEvent);
pinMode(A0, INPUT); pinMode(A0, INPUT);
pinMode(D0, OUTPUT); //selftest pin
} }
void loop(void) void loop(void)
@ -189,8 +182,10 @@ void loop(void)
server.handleClient(); server.handleClient();
webSocket.loop(); webSocket.loop();
updateAccelerometer(); updateSensors();
sendAccelerometerData(); sendDataToSocket();
selfTest();
updateCpuTime(1); updateCpuTime(1);
printCpuLoad(); printCpuLoad();

Loading…
Cancel
Save