Skip to content

Commit 54e9492

Browse files
authored
Merge pull request #31 from Lyr3x/master-v1.1
v1.0.1
2 parents 161ac57 + b02375a commit 54e9492

File tree

6 files changed

+152
-52
lines changed

6 files changed

+152
-52
lines changed

common/api.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Enable Home Assistant API
22
api:
33
password: !secret api_password
4+
reboot_timeout: 60min

common/mqtt_conf.yaml

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/Calibration/Calibration.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
static int DIST_THRESHOLD_MAX[] = {0, 0}; // treshold of the two zones
1212
static int MIN_DISTANCE[] = {0, 0};
1313
static int center[2] = {0, 0}; /* center of the two zones */
14-
static int Zone = 0;
1514
static int ROI_height = 0;
1615
static int ROI_width = 0;
16+
static int zone = 0;
1717

1818
static int delay_between_measurements = 0;
1919
static int time_budget_in_ms = 0;
20-
const int threshold_percentage = 80;
2120

2221
// this value has to be true if the sensor is oriented as in Duthdeffy's picture
2322
static bool advised_orientation_of_the_sensor = true;
@@ -115,10 +114,7 @@ void calibration(VL53L1XSensor Sensor)
115114
void calibration(VL53L1XSensor Sensor)
116115
{
117116
// the sensor does 100 measurements for each zone (zones are predefined)
118-
// each measurements is done with a timing budget of 100 ms, to increase the precision
119-
// client.publish(mqtt_serial_publish_distance_ch, "Computation of new threshold");
120-
// we set the standard values for the measurements
121-
Sensor.setIntermeasurementPeriod(time_budget_in_ms_short);
117+
Sensor.setIntermeasurementPeriod(time_budget_in_ms_long);
122118
Sensor.setRangeMode(LONG_RANGE);
123119
time_budget_in_ms = time_budget_in_ms_long;
124120
delay_between_measurements = delay_between_measurements_long;
@@ -128,7 +124,7 @@ void calibration(VL53L1XSensor Sensor)
128124
ROI_width = 8;
129125
delay(500);
130126

131-
Zone = 0;
127+
zone = 0;
132128
float sum_zone_0 = 0;
133129
float sum_zone_1 = 0;
134130
uint16_t distance;
@@ -141,17 +137,17 @@ void calibration(VL53L1XSensor Sensor)
141137
distance = Sensor.readRangeContinuoisMillimeters(roiConfig1);
142138
Sensor.stopMeasurement();
143139
sum_zone_0 = sum_zone_0 + distance;
144-
Zone++;
145-
Zone = Zone % 2;
140+
zone++;
141+
zone = zone % 2;
146142

147143
// increase sum of values in Zone 2
148144
Sensor.startMeasurement();
149145
delay(delay_between_measurements);
150146
distance = Sensor.readRangeContinuoisMillimeters(roiConfig2);
151147
Sensor.stopMeasurement();
152148
sum_zone_1 = sum_zone_1 + distance;
153-
Zone++;
154-
Zone = Zone % 2;
149+
zone++;
150+
zone = zone % 2;
155151
}
156152
// after we have computed the sum for each zone, we can compute the average distance of each zone
157153
float average_zone_0 = sum_zone_0 / number_attempts;
@@ -228,7 +224,7 @@ void calibration(VL53L1XSensor Sensor)
228224
}
229225
delay(2000);
230226
// we will now repeat the calculations necessary to define the thresholds with the updated zones
231-
Zone = 0;
227+
zone = 0;
232228
sum_zone_0 = 0;
233229
sum_zone_1 = 0;
234230
for (int i = 0; i < number_attempts; i++)
@@ -239,22 +235,22 @@ void calibration(VL53L1XSensor Sensor)
239235
distance = Sensor.readRangeContinuoisMillimeters(roiConfig1);
240236
Sensor.stopMeasurement();
241237
sum_zone_0 = sum_zone_0 + distance;
242-
Zone++;
243-
Zone = Zone % 2;
238+
zone++;
239+
zone = zone % 2;
244240

245241
// increase sum of values in Zone 1
246242
Sensor.startMeasurement();
247243
delay(delay_between_measurements);
248244
distance = Sensor.readRangeContinuoisMillimeters(roiConfig2);
249245
Sensor.stopMeasurement();
250246
sum_zone_1 = sum_zone_1 + distance;
251-
Zone++;
252-
Zone = Zone % 2;
247+
zone++;
248+
zone = zone % 2;
253249
}
254250
average_zone_0 = sum_zone_0 / number_attempts;
255251
average_zone_1 = sum_zone_1 / number_attempts;
256-
float threshold_zone_0 = average_zone_0 * threshold_percentage / 100; // they can be int values, as we are not interested in the decimal part when defining the threshold
257-
float threshold_zone_1 = average_zone_1 * threshold_percentage / 100;
252+
float threshold_zone_0 = average_zone_0 * id(threshold_percentage) / 100; // they can be int values, as we are not interested in the decimal part when defining the threshold
253+
float threshold_zone_1 = average_zone_1 * id(threshold_percentage) / 100;
258254

259255
DIST_THRESHOLD_MAX[0] = threshold_zone_0;
260256
DIST_THRESHOLD_MAX[1] = threshold_zone_1;

lib/Configuration/Config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Be carfeul with reconfiguring! Some options shouldnt be changed!
3434
* general bug fixes and improvements
3535
* added untested VL53L1X support
3636
*/
37-
#define ROODE_VERSION "1.1-alpha"
37+
#define ROODE_VERSION "1.1-beta"
3838
/*
3939
###### FEATURE SELECTION ######
4040
*/
@@ -56,8 +56,8 @@ Be carfeul with reconfiguring! Some options shouldnt be changed!
5656
#include <vl53l1_api.h>
5757
#define dev1_sel digitalWrite(XSHUT_PIN, HIGH);
5858
#define dev1_desel digitalWrite(XSHUT_PIN, LOW);
59-
static VL53L1_UserRoi_t roiConfig1 = {10, 15, 15, 0}; //TopLeftX, TopLeftY, BotRightX, BotRightY
60-
static VL53L1_UserRoi_t roiConfig2 = {0, 15, 5, 0}; //TopLeftX, TopLeftY, BotRightX, BotRightY
59+
static VL53L1_UserRoi_t roiConfig1 = {9, 15, 15, 0}; //TopLeftX, TopLeftY, BotRightX, BotRightY
60+
static VL53L1_UserRoi_t roiConfig2 = {0, 15, 6, 0}; //TopLeftX, TopLeftY, BotRightX, BotRightY
6161

6262
#define SENSOR_I2C 0x52
6363

peopleCounter.yaml

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ esphome:
1919
<<: !include common/common.yaml
2020

2121
globals:
22-
- id: DIST_THRESHOLD_MAX_G
23-
type: int
24-
restore_value: no
25-
initial_value: "1780"
2622
- id: cnt
2723
type: int
2824
restore_value: yes
2925
initial_value: "0"
26+
- id: recalibrate
27+
type: int
28+
restore_value: no
29+
initial_value: "0"
30+
- id: threshold_percentage
31+
type: int
32+
restore_value: no
33+
initial_value: "60"
3034

3135
# Enable logging
3236
logger:
@@ -57,7 +61,7 @@ binary_sensor:
5761
- platform: template
5862
name: $friendly_name photo sensor
5963
lambda: |-
60-
if (id(tof_distance_avg).state < 1400) {
64+
if (id(tof_distance_avg).state < DIST_THRESHOLD_MAX[0] || id(tof_distance_avg).state < DIST_THRESHOLD_MAX[1]) {
6165
// someone in the door
6266
return true;
6367
} else {
@@ -73,7 +77,28 @@ sensor:
7377
update_interval: 60s
7478

7579
- platform: uptime
76-
name: $friendly_name Uptime
80+
name: Uptime Sensor
81+
id: uptime_sensor
82+
update_interval: 120s
83+
internal: true
84+
on_raw_value:
85+
then:
86+
- text_sensor.template.publish:
87+
id: uptime_human
88+
state: !lambda |-
89+
int seconds = round(id(uptime_sensor).raw_state);
90+
int days = seconds / (24 * 3600);
91+
seconds = seconds % (24 * 3600);
92+
int hours = seconds / 3600;
93+
seconds = seconds % 3600;
94+
int minutes = seconds / 60;
95+
seconds = seconds % 60;
96+
return (
97+
(days ? String(days) + "d " : "") +
98+
(hours ? String(hours) + "h " : "") +
99+
(minutes ? String(minutes) + "m " : "") +
100+
(String(seconds) + "s")
101+
).c_str();
77102
78103
- platform: custom
79104
lambda: |-
@@ -97,13 +122,36 @@ sensor:
97122
- lambda: |-
98123
resetCounter = 1;
99124
125+
- platform: mqtt_subscribe
126+
name: "People set counter"
127+
id: people_set_counter
128+
topic: $devicename/sensor/people/set
129+
internal: true
130+
on_value:
131+
then:
132+
- lambda: |-
133+
if(id(cnt)!=id(people_set_counter).state){
134+
id(cnt)=id(people_set_counter).state;
135+
forceSetValue=1;
136+
}
137+
138+
- platform: mqtt_subscribe
139+
name: "People recalibrate"
140+
id: people_recalibrate
141+
topic: $devicename/sensor/people/recalibrate
142+
internal: true
143+
on_value:
144+
then:
145+
- lambda: |-
146+
id(recalibrate) = 1;
147+
100148
- platform: template
101149
name: $friendly_name measured distance
102150
id: tof_distance
103151
accuracy_decimals: 0
104152
icon: "mdi:ruler"
105153
unit_of_measurement: "mm"
106-
update_interval: 1s
154+
update_interval: 5s
107155
lambda: |-
108156
return distance;
109157
filters:
@@ -116,10 +164,45 @@ sensor:
116164
icon: "mdi:ruler"
117165
unit_of_measurement: "mm"
118166
internal: true
119-
update_interval: 0.1s
167+
update_interval: 2s
120168
lambda: |-
121169
return distance;
122170
filters:
123171
- sliding_window_moving_average:
124172
window_size: 10
125173
send_every: 10
174+
175+
- platform: template
176+
name: $friendly_name threshold zone 0
177+
id: threshold_zone0
178+
accuracy_decimals: 0
179+
icon: "mdi:ruler"
180+
unit_of_measurement: "mm"
181+
lambda: |-
182+
return DIST_THRESHOLD_MAX[0];
183+
filters:
184+
- delta: 1
185+
186+
- platform: template
187+
name: $friendly_name threshold zone 1
188+
id: threshold_zone1
189+
accuracy_decimals: 0
190+
icon: "mdi:ruler"
191+
unit_of_measurement: "mm"
192+
update_interval: 60s
193+
lambda: |-
194+
return DIST_THRESHOLD_MAX[1];
195+
filters:
196+
- delta: 1
197+
198+
text_sensor:
199+
- platform: template
200+
name: "$friendly_name Version"
201+
icon: mdi:git
202+
lambda: |-
203+
std::string version = ROODE_VERSION;
204+
return version;
205+
- platform: template
206+
name: Uptime Human Readable
207+
id: uptime_human
208+
icon: mdi:clock-start

src/app.h

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <Calibration.h>
99

1010
#define USE_VL53L1X
11-
VL53L1XSensor count_sensor(XSHUT_PIN, SENSOR_I2C);
11+
VL53L1XSensor countSensor(XSHUT_PIN, SENSOR_I2C);
1212
int gesture_code;
1313
#define NOBODY 0
1414
#define SOMEONE 1
@@ -19,12 +19,15 @@ static const char *TAG = "main";
1919
int distance = 0;
2020
int left = 0, right = 0, oldcnt;
2121
static uint8_t peopleCount = 0; //default state: nobody is inside the room
22-
static int resetCounter = 0;
2322
boolean lastTrippedState = 0;
2423

2524
//static int num_timeouts = 0;
2625
double people, distance_avg;
2726

27+
// MQTT Commands
28+
static int resetCounter = 0;
29+
static int forceSetValue = -1;
30+
2831
class PeopleCountSensor : public Component, public Sensor
2932
{
3033
public:
@@ -38,43 +41,69 @@ class PeopleCountSensor : public Component, public Sensor
3841
Wire.begin();
3942
Wire.setClock(400000);
4043

41-
count_sensor.init();
44+
countSensor.init();
4245
#ifdef CALIBRATION
43-
calibration(count_sensor);
46+
calibration(countSensor);
4447
#endif
4548
#ifdef CALIBRATIONV2
46-
calibration_boot(count_sensor);
49+
calibration_boot(countSensor);
4750
#endif
4851
ESP_LOGI("VL53L1X custom sensor", "Starting measurements");
49-
count_sensor.startMeasurement();
52+
countSensor.startMeasurement();
53+
}
54+
55+
void checkMQTTCommands()
56+
{
57+
if (resetCounter == 1)
58+
{
59+
ESP_LOGI("MQTTCommand", "Reset counter command received");
60+
resetCounter = 0;
61+
sendCounter(-1);
62+
}
63+
if (id(recalibrate) == 1)
64+
{
65+
ESP_LOGI("MQTTCommand", "Recalibration command received");
66+
calibration(countSensor);
67+
recalibrate = 0;
68+
}
69+
if (forceSetValue != -1)
70+
{
71+
ESP_LOGI("MQTTCommand", "Force set value command received");
72+
publishMQTT(id(cnt));
73+
forceSetValue = -1;
74+
}
5075
}
5176

77+
void publishMQTT(int val)
78+
{
79+
peopleCount = val;
80+
people_sensor->publish_state(val);
81+
}
5282
void loop() override
5383
{
84+
checkMQTTCommands();
5485
static int PathTrack[] = {0, 0, 0, 0};
5586
static int PathTrackFillingSize = 1; // init this to 1 as we start from state where nobody is any of the zones
5687
static int LeftPreviousStatus = NOBODY;
5788
static int RightPreviousStatus = NOBODY;
58-
static int zone = 0;
89+
5990

6091
int CurrentZoneStatus = NOBODY;
6192
int AllZonesCurrentStatus = 0;
6293
int AnEventHasOccured = 0;
63-
if (zone == LEFT)
94+
if (zone == 0)
6495
{
65-
distance = count_sensor.readRangeContinuoisMillimeters(roiConfig1);
96+
distance = countSensor.readRangeContinuoisMillimeters(roiConfig1);
6697
}
67-
else
98+
else if (zone == 1)
6899
{
69-
distance = count_sensor.readRangeContinuoisMillimeters(roiConfig2);
100+
distance = countSensor.readRangeContinuoisMillimeters(roiConfig2);
70101
}
71102

72-
// if (distance < id(DIST_THRESHOLD_MAX_G))
73-
if (distance < DIST_THRESHOLD_MAX[Zone] && distance > MIN_DISTANCE[Zone])
103+
if (distance < DIST_THRESHOLD_MAX[zone] && distance > MIN_DISTANCE[zone])
74104
{
75105
// Someone is in !
76106
CurrentZoneStatus = SOMEONE;
77-
//ESP_LOGE(TAG, "Global value is: %d", id(DIST_THRESHOLD_MAX_G));
78107
}
79108

80109
// left zone

0 commit comments

Comments
 (0)