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.
192 lines
4.5 KiB
192 lines
4.5 KiB
#include <Arduino.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <html.h>
|
|
#include <Graph.h>
|
|
#include <Sensor.h>
|
|
#include <WebSocketsServer.h>
|
|
#include <ArduinoJson.h>
|
|
#include "CpuLoad.h"
|
|
|
|
|
|
#define WEBSOCKETS_SERVER_CLIENT_MAX (20)
|
|
|
|
|
|
const char *ssid = "LabWork_1";
|
|
|
|
ESP8266WebServer server(80);
|
|
Sensor accelerometer = Sensor();
|
|
Graph graphAcc = Graph(200, 200);
|
|
|
|
long int lastDataSend = 0;
|
|
long int lastTest = 0;
|
|
bool testState = false;
|
|
|
|
int timeOfCal = 0;
|
|
bool accCalibrated = false;
|
|
int accCalRemains = 0;
|
|
int accMax1 = 0;
|
|
int accMax2 = 0;
|
|
|
|
WebSocketsServer webSocket = WebSocketsServer(81);
|
|
|
|
void handleMainPage()
|
|
{
|
|
int16_t bias = 0;
|
|
const int arr_size = 5000;
|
|
char *html_code = new char[arr_size];
|
|
memset(html_code, '\0', sizeof(char) * arr_size);
|
|
|
|
bias += getHtml(HTML_BEGIN, *html_code, arr_size, 0);
|
|
|
|
server.send(200, "text/html", html_code);
|
|
|
|
delete[] html_code;
|
|
}
|
|
|
|
void cookDataToJson(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 eatDataFromJson() {
|
|
|
|
}
|
|
|
|
int lowFreqFilter(int inputValue, int lastFilteredValue, int alpha) {
|
|
return (lastFilteredValue + alpha * (inputValue - lastFilteredValue)+100);
|
|
}
|
|
|
|
void updateSensors()
|
|
{
|
|
if ((millis() - accelerometer.lastUpdate) > (1000 / graphAcc.updateRate))
|
|
{
|
|
int filteredSignal = 0;
|
|
int sourceSignal = analogRead(A0);
|
|
accelerometer.lastValue = map(sourceSignal, 0, 1023, graphAcc.size-1, 0);
|
|
|
|
if (graphAcc.counter != 0) {
|
|
//filteredSignal = lowFreqFilter(accelerometer.lastValue, graphAcc.array[acc.graphCounter - 1], 0.5);
|
|
filteredSignal = graphAcc.array[graphAcc.counter - 1] + 0.5 * (accelerometer.lastValue - graphAcc.array[graphAcc.counter - 1]);
|
|
}
|
|
|
|
if (graphAcc.counter < graphAcc.size - 1)
|
|
{
|
|
graphAcc.array[graphAcc.counter] = filteredSignal;
|
|
graphAcc.counter++;
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i != graphAcc.counter; i++)
|
|
{
|
|
graphAcc.array[i] = graphAcc.array[i + 1];
|
|
}
|
|
graphAcc.array[graphAcc.counter] = filteredSignal;
|
|
}
|
|
|
|
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 sendDataToSocket() {
|
|
|
|
int updateRate = 0;
|
|
updateRate = 50;
|
|
|
|
if (millis() - lastDataSend > updateRate) {
|
|
String word;
|
|
cookDataToJson(word, graphAcc.array, graphAcc.size);
|
|
webSocket.broadcastTXT(word);
|
|
lastDataSend = millis();
|
|
}
|
|
}
|
|
|
|
|
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
|
switch(type) {
|
|
case WStype_TEXT:
|
|
timeOfCal = (int) *payload;
|
|
Serial.printf("[%u] get Text: %d\n", num, timeOfCal);
|
|
}
|
|
}
|
|
|
|
void selfTest(){
|
|
if (millis() - lastTest > 19) {
|
|
if (!testState) {
|
|
digitalWrite(D0, HIGH);
|
|
testState = true;
|
|
} else {
|
|
digitalWrite(D0, LOW);
|
|
testState = false;
|
|
}
|
|
lastTest = millis();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void setup(void)
|
|
{
|
|
IPAddress apIP(192, 168, 1, 1);
|
|
Serial.begin(115200);
|
|
Serial.setDebugOutput(true);
|
|
Serial.println();
|
|
|
|
WiFi.setAutoConnect(false);
|
|
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
|
|
WiFi.softAP(ssid);
|
|
IPAddress myIP = WiFi.softAPIP();
|
|
Serial.print("AP IP address: ");
|
|
Serial.println(myIP);
|
|
|
|
server.on("/", handleMainPage);
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
|
|
webSocket.begin();
|
|
webSocket.onEvent(webSocketEvent);
|
|
|
|
pinMode(A0, INPUT);
|
|
pinMode(D0, OUTPUT); //selftest pin
|
|
}
|
|
|
|
void loop(void)
|
|
{
|
|
updateCpuTime(0);
|
|
|
|
server.handleClient();
|
|
webSocket.loop();
|
|
updateSensors();
|
|
sendDataToSocket();
|
|
|
|
selfTest();
|
|
|
|
updateCpuTime(1);
|
|
printCpuLoad();
|
|
}
|