Added sensors as classes

This commit is contained in:
Tim Korjakow 2021-11-21 20:40:23 +01:00
parent 92a2cec272
commit 0c954b745b
11 changed files with 39308 additions and 1575 deletions

View File

@ -1 +1 @@
REACT_APP_BACKEND_URL="ws://localhost:8080"
REACT_APP_BACKEND_URL="ws://192.168.4.1/ws"

37411
frontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,7 @@
{
"name": "testbench-frontent",
"version": "0.1.0",
"type": "module",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -8,14 +8,18 @@
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[platformio]
data_dir=frontend/build
[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_speed = 115200
board_build.filesystem = littlefs
lib_deps =
ottowinter/ESPAsyncWebServer-esphome@^2.1.0
ottowinter/ESPAsyncWebServer-esphome@^2.1.0
adafruit/Adafruit BMP3XX Library@^2.1.1
adafruit/Adafruit BusIO@^1.9.3
Wire
SPI
Wire
SPI

41
src/AP_setup.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include <ESP8266mDNS.h>
#include <LittleFS.h>
#include "AP_setup.h"
const char* domain_name = "prüfstand";
const char* AP_NamePrefix = "Prüfstand ";
void startMDNS() {
if (!MDNS.begin(domain_name, WiFi.softAPIP())) {
Serial.println("[ERROR] MDNS responder did not setup");
while (1) {
delay(1000);
}
} else {
Serial.println("[INFO] MDNS setup is successful!");
}
}
String getAPName() {
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
return AP_NamePrefix + macID;
}
void initAccessPoint() {
Serial.println("\n[INFO] Configuring access point");
WiFi.mode(WIFI_AP);
WiFi.softAP(getAPName().c_str(), NULL);
startMDNS();
MDNS.addService("http", "tcp", 80);
Serial.print("[INFO] Started access point at IP ");
Serial.println(WiFi.softAPIP());
}

8
src/AP_setup.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef _AP_SETUP_H
#define _AP_SETUP_H
extern void startMDNS();
extern String getAPName();
extern void initAccessPoint();
#endif

View File

@ -1,33 +1,117 @@
#include <Arduino.h>
#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include <ESP8266mDNS.h>
#include <LittleFS.h>
double ohm_to_celsius(float measurement, int temp_range_step)
{
int lower_bin_edge = (int) measurement % temp_range_step;
return 0.0;
#include "AP_setup.h"
#include "sensor.h"
AsyncWebServer server(80);
AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws
FluidPressureSensor pressure_before_radiator_sensor(A0);
FluidPressureSensor pressure_after_radiator_sensor(A0);
Thermistor temperature_before_radiator(A0, 100000);
Thermistor temperature_after_radiator(A0, 100000);
Barometer barometer;
Multiplexer mux;
int i = 0;
//flag to use from web update to reboot the ESP
bool shouldReboot = false;
void onRequest(AsyncWebServerRequest *request){
//Handle Unknown Request
request->send(200);
}
double floatmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
void onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
//Handle body
}
float readTemperature()
{
int temp = analogRead(A0);
float ohm = (1000.0/3.3)*temp;
return ohm;
void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
//Handle upload
}
void setup() {
// put your setup code here, to run once:
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
if(type == WS_EVT_CONNECT){
//client connected
os_printf("ws[%s][%u] connect\n", server->url(), client->id());
client->ping();
} else if(type == WS_EVT_DISCONNECT){
//client disconnected
os_printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
} else if(type == WS_EVT_ERROR){
//error was received from the other end
os_printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
} else if(type == WS_EVT_PONG){
//pong message was received (in response to a ping request maybe)
os_printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
} else if(type == WS_EVT_DATA){
//data packet
AwsFrameInfo * info = (AwsFrameInfo*)arg;
//the whole message is in a single frame and we got all of it's data
}
}
void setup(){
Serial.begin(115200);
initAccessPoint();
LittleFS.begin();
int temp_range_start = 300;
int temp_range_end = -55;
int temp_range_step = 10;
float tempistor_y[35] = {519910.0, 158090.0, 71668.0, 44087.0, 27936.0, 18187.0, 12136.0, 8284.0, 5774.0, 4103.0, 2967.0, 2182.0, 1629.0, 1234.0, 946.6, 735.5, 578.1, 459.4, 368.8, 298.9, 244.4, 201.6, 167.6, 140.4, 118.5, 100.7, 86.08, 74.05, 64.08, 55.75, 48.76, 42.87, 37.86, 33.59, 29.94 };
// attach AsyncWebSocket
ws.onEvent(onEvent);
server.addHandler(&ws);
// respond to GET requests on URL /heap
server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
// send a file when /index is requested
server.on("/", HTTP_ANY, [](AsyncWebServerRequest *request){
request->send(LittleFS, "/index.html");
});
// attach filesystem root at URL /fs
server.serveStatic("/static", LittleFS, "/static");
// Catch-All Handlers
// Any request that can not find a Handler that canHandle it
// ends in the callbacks below.
server.onNotFound(onRequest);
server.onRequestBody(onBody);
server.begin();
pinMode(D8, OUTPUT);
barometer.init();
barometer.setAmbientPressure();
}
void loop() {
// put your main code here, to run repeatedly:
void loop(){
if(shouldReboot){
Serial.println("Rebooting...");
delay(100);
ESP.restart();
}
async_wait(10);
//Serial.println(barometer.readWindSpeed());
Serial.println(barometer.readWindSpeed());
//ws.cleanupClients();
}

139
src/sensor.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <Arduino.h>
#include "AsyncJson.h"
#include "ArduinoJson.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#include "sensor.h"
float Thermistor::resistance(float measured_bit_value){
return (voltage_divider_ohm * measured_bit_value/1024)/(1-measured_bit_value/1024);
}
float Thermistor::ohm_to_temperature(float resistance){
int len = sizeof tempistor_y / sizeof tempistor_y[0];
int i = 0;
while (i < len && tempistor_y[i] < resistance) {
i++;
}
i--;
if (i == 0 || i == len-1)
{
return 0.0;
}
return floatmap(resistance, tempistor_y[i], tempistor_y[i+1], temp_range_start+ i*temp_range_step, temp_range_start+ (i+1)*temp_range_step);
}
Thermistor::Thermistor(int pin, int voltage_divider_ohm){
pin = pin;
voltage_divider_ohm = voltage_divider_ohm;
supply_voltage = supply_voltage;
}
void async_wait(int ms) {
unsigned long t1 = millis();
int n=0;
while (t1 + ms > millis())
{
n++;
}
}
float Thermistor::read()
{
int measured_voltage = analogRead(pin);
float ohm = resistance(measured_voltage);
return ohm;
}
void Barometer::init() {
if (!bmp.begin_I2C()) { // hardware I2C mode, can pass in address & alt Wire
Serial.println("Could not find a valid BMP3 sensor, check wiring!");
while (1);
}
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);
for (size_t i = 0; i < 100; i++)
{
bmp.performReading();
}
}
void Barometer::setAmbientPressure() {
bmp.performReading();
ambient_pressure = bmp.pressure;
Serial.print(ambient_pressure);
}
float Barometer::readWindSpeed() {
bmp.performReading();
double current_pressure = bmp.pressure;
double wind_speed = sqrt(2*abs(current_pressure-ambient_pressure)/1.184);
return wind_speed;
}
float Barometer::readAltitude() {
bmp.performReading();
return bmp.readAltitude(1008);
}
FluidPressureSensor::FluidPressureSensor(int pin) {
pin = pin;
}
double FluidPressureSensor::read() {
int measured_voltage = analogRead(pin);
float pressure = floatmap(measured_voltage, 0.0, 1023.0, 0.0, 4.0);
return pressure;
}
void Multiplexer::activate(Mux_Input device_to_activate){
for (int device = thermistor_before_radiator; device != Last; device++ )
{
Mux_Input device_mux = static_cast<Mux_Input>(device);
digitalWrite((uint8_t)device_mux, LOW);
}
digitalWrite((uint8_t)device_to_activate, HIGH);
}
double floatmap(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
DynamicJsonDocument createMeasurementJSON(Sensor_Readings* packet){
DynamicJsonDocument data_message(1024);
data_message["topic"] = "data";
data_message["data"]["ts"] = packet->ts;
data_message["data"]["temp_before_radiator"] = packet->temp_before_radiator;
data_message["data"]["temp_after_radiator"] = packet->temp_after_radiator;
data_message["data"]["pressure_before_radiator"] = packet->pressure_before_radiator;
data_message["data"]["pressure_after_radiator"] = packet->pressure_after_radiator;
data_message["data"]["wind_speed"] = packet->wind_speed;
return data_message;
}
DynamicJsonDocument createEndMessageJSON(long ts, float temp_before_radiator, float temp_after_radiator, float pressure_before_radiator, float pressure_after_radiator, float wind_speed){
DynamicJsonDocument end_message(1024);
end_message["topic"] = "end_test";
return end_message;
}
void sendJSONtoClient(AsyncWebSocket ws, AsyncWebSocketClient* client, DynamicJsonDocument message) {
size_t len = measureJson(message);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(message, (char *)buffer->get(), len);
client->text(buffer);
}
}

79
src/sensor.h Normal file
View File

@ -0,0 +1,79 @@
#ifndef _SENSOR_H
#define _SENSOR_H
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
struct Sensor_Readings
{
float ts;
float temp_before_radiator;
float temp_after_radiator;
float pressure_before_radiator;
float pressure_after_radiator;
float wind_speed;
};
enum Mux_Input {
thermistor_before_radiator=D0,
thermistor_after_radiator=D0,
pressure_before_radiator=D5,
pressure_sensor_after_radiator=D5,
Last
};
class Multiplexer {
public:
void activate(Mux_Input device_to_activate);
};
extern double floatmap(float x, float in_min, float in_max, float out_min, float out_max);
class Barometer {
Adafruit_BMP3XX bmp;
double ambient_pressure;
public:
void init();
float readWindSpeed();
float readAltitude();
void setAmbientPressure();
};
class Thermistor { // The class
private: // Access specifier
int temp_range_start = 300;
int temp_range_end = -55;
int temp_range_step = 10;
float tempistor_y[35] = {519910.0, 158090.0, 71668.0, 44087.0, 27936.0, 18187.0, 12136.0, 8284.0, 5774.0, 4103.0, 2967.0, 2182.0, 1629.0, 1234.0, 946.6, 735.5, 578.1, 459.4, 368.8, 298.9, 244.4, 201.6, 167.6, 140.4, 118.5, 100.7, 86.08, 74.05, 64.08, 55.75, 48.76, 42.87, 37.86, 33.59, 29.94 };
int pin;
int voltage_divider_ohm;
float supply_voltage;
float resistance(float measured_bit_value);
float ohm_to_temperature(float resistance);
public:
Thermistor(int pin, int voltage_divider_ohm);
float read();
};
class FluidPressureSensor {
private:
int pin;
public:
FluidPressureSensor(int pin);
double read();
};
extern void async_wait(int ms);
#endif