Skip to content

Commit 0a5de56

Browse files
committed
tof sensor moving to chartjs
1 parent c3c51ad commit 0a5de56

9 files changed

+183
-22
lines changed

interface/.env.development

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Change the IP address to that of your ESP device to enable local development of the UI.
22
# Remember to also enable CORS in platformio.ini before uploading the code to the device.
3-
REACT_APP_HTTP_ROOT=http://192.168.0.24
4-
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.0.24
3+
REACT_APP_HTTP_ROOT=http://192.168.1.53
4+
REACT_APP_WEB_SOCKET_ROOT=ws://192.168.1.53

interface/package-lock.json

+32-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
"@material-ui/core": "^4.11.3",
77
"@material-ui/icons": "^4.11.2",
88
"@types/lodash": "^4.14.168",
9-
"@types/node": "^12.20.4",
10-
"@types/react": "^17.0.2",
11-
"@types/react-dom": "^17.0.1",
9+
"@types/node": "^12.20.5",
10+
"@types/react": "^17.0.3",
11+
"@types/react-dom": "^17.0.2",
1212
"@types/react-material-ui-form-validator": "^2.1.0",
1313
"@types/react-router": "^5.1.12",
1414
"@types/react-router-dom": "^5.1.6",
1515
"@types/recharts": "^1.8.19",
16+
"chartjs": "^0.3.24",
1617
"compression-webpack-plugin": "^4.0.0",
1718
"date-fns": "^2.19.0",
1819
"jwt-decode": "^3.1.2",
@@ -21,7 +22,8 @@
2122
"notistack": "^1.0.5",
2223
"parse-ms": "^2.1.0",
2324
"react": "^17.0.1",
24-
"react-day-picker": "^8.0.0-beta.17",
25+
"react-chartjs-2": "^2.11.1",
26+
"react-day-picker": "^8.0.0-beta.19",
2527
"react-dom": "^17.0.1",
2628
"react-dropzone": "^11.3.1",
2729
"react-form-validator-core": "^1.1.1",

interface/src/project/DemoInformation.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ function GraphView(props:GraphProps){
170170
<ResponsiveContainer width="100%" height={300}>
171171
<LineChart height={300} data={props.graphData} syncId="datalab" >
172172
<XAxis dataKey="ts" tickFormatter={RenderTick} />
173-
<YAxis />
173+
{/* <YAxis domain={[Math.max.apply(Math, props.graphData.map(function(o) { return o[props.sensorName]; })),Math.max.apply(Math, props.graphData.map(function(o) { return o[props.sensorName]; }))]}/> */}
174+
<YAxis domain={[0,Math.max.apply(Math, props.graphData.map(function(o) { return o[props.sensorName]; }))]}/>
174175
<Tooltip labelFormatter={RenderTick} />
175176
<CartesianGrid stroke="#eee" strokeDasharray="5 5" />
176177
<Line type="monotone" connectNulls={true} dataKey={props.sensorName} isAnimationActive={false} />

media/Sensordoc.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## How to create a Sensor
2+
3+
*We will take the VL53L0X ToF sensor as exemple.*
4+
1. Make sure you include the required libraries in the `platformio.ini` file:
5+
6+
```ini
7+
lib_deps =
8+
Adafruit_VL53L0X
9+
```
10+
11+
2. Start from a simple example file from your librairy. Here, `Adafruit_VL53L0X/examples/vl53l0x/vl53l0x.ino`
12+
13+
3. Copy the code from `src\sensor\TestSensor.h` to your sensor name. Here `src\sensor\vl5310x.ino`
14+
15+
4. Replace everywhere `TestSensor` with your sensor name. Here `Vlx53l0x`. And include the required libraries. Here:
16+
```cpp
17+
//Keep them
18+
#include "CSensor.h"
19+
#include <Arduino.h>
20+
//And add yours:
21+
#include "Adafruit_VL53L0X.h"
22+
```
23+
24+
5. Your example might have global variable. Place them in the class as private. Here:
25+
```cpp
26+
class Vlx53l0x : public CSensor
27+
{
28+
private:
29+
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
30+
```
31+
32+
33+
34+
5. Your sensor will probably need some variables to be configured by the user (eg GPIO ports numbers, min, max etc...)
35+
> Place your requiered variables in the private section of the class of your sensor.
36+
37+
```cpp
38+
class Vlx53l0x : public CSensor
39+
{
40+
private:
41+
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
42+
int scl;
43+
int sda;
44+
45+
public:
46+
...
47+
```
48+
49+
6. Adjust the constructor of your sensor to initialize the values from the user interface/config file:
50+
51+
```cpp
52+
Vlx53l0x( JsonObject& sensorConf): CSensor(sensorConf){
53+
sda = sensorConf["driver"]["config"]["sda"].as<int>();
54+
scl = sensorConf["driver"]["config"]["scl"].as<int>();
55+
};
56+
```
57+
58+
7. From your example, put the `setup()` content in the `begin()` method.
59+
```cpp
60+
void begin()
61+
{
62+
lox.begin();
63+
Serial.printf("we are starting the random sensor with min: %d and max:%d\n",minVal,maxVal);
64+
};
65+
```
66+
8. Now integrate your sensor in `src\SensorSettingsService.h`:
67+
> 1. Include your sensor file:
68+
```cpp
69+
#include "sensor/TestSensor.h"
70+
#include "sensor/vl53l0x.h"//Here
71+
#define SENSOR_SETTINGS_FILE "/config/sensorSettings.json"
72+
73+
```
74+
75+
> 2. Include your description:
76+
```cpp
77+
static constexpr const char* driverList[] = {
78+
Vlx53l0x::description, // Here
79+
BMP180Sensor::description,
80+
...
81+
```
82+
83+
> 3. Include the name correspondance: The name must match the one on your `description` property.
84+
```cpp
85+
// Add here your custom sensors
86+
if (strcmp(sensorConf["driver"]["name"], "ToF") == 0) {
87+
return new Vlx53l0x(sensorConf);
88+
}
89+
90+
```

partition.csv

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Name, Type, SubType, Offset, Size, Flags
22
nvs, data, nvs, 0x9000, 0x5000,
33
otadata, data, ota, 0xe000, 0x2000,
4-
app0, app, ota_0, 0x10000, 0x130000,
5-
spiffs, data, spiffs, 0x140000,0x2c0000,
4+
app0, app, ota_0, 0x10000, 0x135000,
5+
spiffs, data, spiffs, 0x145000,0x2b5000,

platformio.ini

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ monitor_speed = 115200
2626
extra_scripts =
2727
mklittlefs.py
2828
lib_deps =
29+
Adafruit_VL53L0X
2930
ArduinoJson@>=6.0.0,<7.0.0
3031
ESP Async WebServer@>=1.2.0,<2.0.0
3132
AsyncMqttClient@>=0.8.2,<1.0.0
@@ -43,8 +44,10 @@ board = m5stick-c
4344
board_build.filesystem = littlefs
4445
; monitor_filters = esp32_exception_decoder
4546
; build_type= debug
47+
upload_speed = 750000
4648
board_build.partitions = ./partition.csv
4749
lib_deps =
50+
Adafruit_VL53L0X
4851
LittleFS_esp32
4952
M5stickc
5053
me-no-dev/ESP Async WebServer@^1.2.3

src/SensorSettingsService.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "sensor/FreeMemSensor.cpp"
1616
#include "sensor/AnalogInSensor.cpp"
1717
#include "sensor/TestSensor.h"
18+
#include "sensor/vl53l0x.h"
1819
#define SENSOR_SETTINGS_FILE "/config/sensorSettings.json"
1920
#define SENSOR_SETTINGS_ENDPOINT_PATH "/rest/sensorsState"
2021
#define SENSOR_SETTINGS_SOCKET_PATH "/ws/sensorsState"
@@ -30,12 +31,13 @@ class SensorConfig {
3031

3132
// Add all sensors here
3233
static constexpr const char* driverList[] = {
34+
Vlx53l0x::description,
3335
BMP180Sensor::description,
3436
DHT11Sensor::description,
3537
TestSensor::description,
3638
BMP280Sensor::description,
3739
FreeMemSensor::description,
38-
AnalogInSensor::description
40+
AnalogInSensor::description,
3941
};
4042

4143

@@ -44,6 +46,9 @@ class SensorConfig {
4446
serializeJsonPretty(sensorConf,Serial);
4547

4648
// Add here your custom sensors
49+
if (strcmp(sensorConf["driver"]["name"], "ToF") == 0) {
50+
return new Vlx53l0x(sensorConf);
51+
}
4752
if (strcmp(sensorConf["driver"]["name"], "Random") == 0) {
4853
return new TestSensor(sensorConf);
4954
}

src/sensor/vl53l0x.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#if !defined(Vlx53l0x_c)
2+
#define Vlx53l0x_c
3+
#include "CSensor.h"
4+
#include <Arduino.h>
5+
#include "Adafruit_VL53L0X.h"
6+
7+
class Vlx53l0x : public CSensor
8+
{
9+
private:
10+
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
11+
int scl;
12+
int sda;
13+
TwoWire I2CVL = TwoWire(0);
14+
15+
public:
16+
Vlx53l0x(){};
17+
Vlx53l0x( JsonObject& sensorConf): CSensor(sensorConf){
18+
sda = sensorConf["driver"]["config"]["sda"].as<int>();
19+
scl = sensorConf["driver"]["config"]["scl"].as<int>();
20+
};
21+
22+
static constexpr const char* description = "{\"name\":\"ToF\",\"config\":{\"sda\":32,\"scl\":33}}\"";
23+
24+
void begin()
25+
{
26+
I2CVL.begin(sda,scl);
27+
lox.begin(VL53L0X_I2C_ADDR,true,&I2CVL);
28+
};
29+
30+
//This function is called to return the sensor value at every interval
31+
float getValue()
32+
{
33+
VL53L0X_RangingMeasurementData_t measure;
34+
lox.rangingTest(&measure, true);
35+
return measure.RangeMilliMeter;
36+
};
37+
38+
};
39+
40+
#endif // Vlx53l0x

0 commit comments

Comments
 (0)