Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Käppler
ampel-firmware
Commits
a735b10a
Commit
a735b10a
authored
Apr 16, 2021
by
Eric Duminil
Browse files
Calibration
parent
eac9178c
Changes
5
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/co2_sensor.cpp
View file @
a735b10a
...
@@ -5,6 +5,8 @@ namespace config {
...
@@ -5,6 +5,8 @@ namespace config {
uint16_t
measurement_timestep
=
MEASUREMENT_TIMESTEP
;
// [s] Value between 2 and 1800 (range for SCD30 sensor)
uint16_t
measurement_timestep
=
MEASUREMENT_TIMESTEP
;
// [s] Value between 2 and 1800 (range for SCD30 sensor)
const
uint16_t
altitude_above_sea_level
=
ALTITUDE_ABOVE_SEA_LEVEL
;
// [m]
const
uint16_t
altitude_above_sea_level
=
ALTITUDE_ABOVE_SEA_LEVEL
;
// [m]
uint16_t
co2_calibration_level
=
ATMOSPHERIC_CO2_CONCENTRATION
;
// [ppm]
uint16_t
co2_calibration_level
=
ATMOSPHERIC_CO2_CONCENTRATION
;
// [ppm]
int8_t
max_deviation_during_calibration
=
30
;
// [ppm]
int8_t
enough_stable_measurements
=
60
;
#ifdef TEMPERATURE_OFFSET
#ifdef TEMPERATURE_OFFSET
// Residual heat from CO2 sensor seems to be high enough to change the temperature reading. How much should it be offset?
// Residual heat from CO2 sensor seems to be high enough to change the temperature reading. How much should it be offset?
// NOTE: Sign isn't relevant. The returned temperature will always be shifted down.
// NOTE: Sign isn't relevant. The returned temperature will always be shifted down.
...
@@ -42,6 +44,21 @@ namespace sensor {
...
@@ -42,6 +44,21 @@ namespace sensor {
}
}
}
}
void
calibrateSensorToSpecificPPM
(
int32_t
calibrationLevel
)
{
if
(
calibrationLevel
>=
400
&&
calibrationLevel
<=
2000
)
{
Serial
.
print
(
F
(
"Force calibration, at "
));
config
::
co2_calibration_level
=
calibrationLevel
;
Serial
.
print
(
config
::
co2_calibration_level
);
Serial
.
println
(
" ppm."
);
sensor
::
startCalibrationProcess
();
}
}
void
calibrateSensorRightNow
(
int32_t
calibrationLevel
)
{
stable_measurements
=
config
::
enough_stable_measurements
;
calibrateSensorToSpecificPPM
(
calibrationLevel
);
}
void
initialize
()
{
void
initialize
()
{
#if defined(ESP8266)
#if defined(ESP8266)
Wire
.
begin
(
12
,
14
);
// ESP8266 - D6, D5;
Wire
.
begin
(
12
,
14
);
// ESP8266 - D6, D5;
...
@@ -88,6 +105,8 @@ namespace sensor {
...
@@ -88,6 +105,8 @@ namespace sensor {
sensor_commands
::
defineCallback
(
"co2"
,
setCO2forDebugging
);
sensor_commands
::
defineCallback
(
"co2"
,
setCO2forDebugging
);
sensor_commands
::
defineCallback
(
"timer"
,
setTimer
);
sensor_commands
::
defineCallback
(
"timer"
,
setTimer
);
sensor_commands
::
defineCallback
(
"calibrate"
,
calibrateSensorToSpecificPPM
);
sensor_commands
::
defineCallback
(
"calibrate!"
,
calibrateSensorRightNow
);
}
}
//NOTE: should timer deviation be used to adjust measurement_timestep?
//NOTE: should timer deviation be used to adjust measurement_timestep?
...
@@ -102,7 +121,8 @@ namespace sensor {
...
@@ -102,7 +121,8 @@ namespace sensor {
void
countStableMeasurements
()
{
void
countStableMeasurements
()
{
static
int16_t
previous_co2
=
0
;
static
int16_t
previous_co2
=
0
;
if
(
co2
>
(
previous_co2
-
30
)
&&
co2
<
(
previous_co2
+
30
))
{
if
(
co2
>
(
previous_co2
-
config
::
max_deviation_during_calibration
)
&&
co2
<
(
previous_co2
+
config
::
max_deviation_during_calibration
))
{
stable_measurements
++
;
stable_measurements
++
;
Serial
.
print
(
F
(
"Number of stable measurements : "
));
Serial
.
print
(
F
(
"Number of stable measurements : "
));
Serial
.
println
(
stable_measurements
);
Serial
.
println
(
stable_measurements
);
...
@@ -197,7 +217,7 @@ namespace sensor {
...
@@ -197,7 +217,7 @@ namespace sensor {
}
}
if
(
should_calibrate
)
{
if
(
should_calibrate
)
{
if
(
stable_measurements
==
60
)
{
if
(
stable_measurements
==
config
::
enough_stable_measurements
)
{
calibrateAndRestart
();
calibrateAndRestart
();
}
}
led_effects
::
showWaitingLED
(
waiting_color
);
led_effects
::
showWaitingLED
(
waiting_color
);
...
...
ampel-firmware/csv_writer.cpp
View file @
a735b10a
...
@@ -126,6 +126,7 @@ namespace csv_writer {
...
@@ -126,6 +126,7 @@ namespace csv_writer {
Serial
.
println
();
Serial
.
println
();
sensor_commands
::
defineCallback
(
"csv"
,
setCSVinterval
);
sensor_commands
::
defineCallback
(
"csv"
,
setCSVinterval
);
// sensor_commands::defineCallback("format_filesystem", FS_LIB.format);
}
}
File
openOrCreate
()
{
File
openOrCreate
()
{
...
...
ampel-firmware/mqtt.cpp
View file @
a735b10a
...
@@ -27,6 +27,14 @@ namespace mqtt {
...
@@ -27,6 +27,14 @@ namespace mqtt {
const
char
*
json_sensor_format
;
const
char
*
json_sensor_format
;
String
last_successful_publish
=
""
;
String
last_successful_publish
=
""
;
void
setMQTTinterval
(
int32_t
sending_interval
)
{
config
::
sending_interval
=
sending_interval
;
Serial
.
print
(
F
(
"Setting Sending Interval to : "
));
Serial
.
print
(
config
::
sending_interval
);
Serial
.
println
(
"s."
);
led_effects
::
showKITTWheel
(
color
::
green
,
1
);
}
void
initialize
(
String
&
topic
)
{
void
initialize
(
String
&
topic
)
{
json_sensor_format
=
PSTR
(
"{
\"
time
\"
:
\"
%s
\"
,
\"
co2
\"
:%d,
\"
temp
\"
:%.1f,
\"
rh
\"
:%.1f}"
);
json_sensor_format
=
PSTR
(
"{
\"
time
\"
:
\"
%s
\"
,
\"
co2
\"
:%d,
\"
temp
\"
:%.1f,
\"
rh
\"
:%.1f}"
);
publish_topic
=
topic
;
publish_topic
=
topic
;
...
@@ -35,6 +43,8 @@ namespace mqtt {
...
@@ -35,6 +43,8 @@ namespace mqtt {
#endif
#endif
// mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect()
// mqttClient.setSocketTimeout(config::mqtt_timeout); //NOTE: somehow doesn't seem to have any effect on connect()
mqttClient
.
setServer
(
config
::
mqtt_server
,
config
::
mqtt_port
);
mqttClient
.
setServer
(
config
::
mqtt_server
,
config
::
mqtt_port
);
sensor_commands
::
defineCallback
(
"mqtt"
,
setMQTTinterval
);
}
}
void
publish
(
const
String
&
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
void
publish
(
const
String
&
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
...
@@ -55,27 +65,6 @@ namespace mqtt {
...
@@ -55,27 +65,6 @@ namespace mqtt {
}
}
}
}
void
setMQTTinterval
(
String
messageString
)
{
messageString
.
replace
(
"mqtt "
,
""
);
config
::
sending_interval
=
messageString
.
toInt
();
Serial
.
print
(
F
(
"Setting Sending Interval to : "
));
Serial
.
print
(
config
::
sending_interval
);
Serial
.
println
(
"s."
);
led_effects
::
showKITTWheel
(
color
::
green
,
1
);
}
void
calibrateSensorToSpecificPPM
(
String
messageString
)
{
messageString
.
replace
(
"calibrate "
,
""
);
long
int
calibrationLevel
=
messageString
.
toInt
();
if
(
calibrationLevel
>=
400
&&
calibrationLevel
<=
2000
)
{
Serial
.
print
(
F
(
"Force calibration, at "
));
config
::
co2_calibration_level
=
messageString
.
toInt
();
Serial
.
print
(
config
::
co2_calibration_level
);
Serial
.
println
(
" ppm."
);
sensor
::
startCalibrationProcess
();
}
}
void
sendInfoAboutLocalNetwork
()
{
void
sendInfoAboutLocalNetwork
()
{
char
info_topic
[
60
];
// Should be enough for "CO2sensors/ESPd03cc5/info"
char
info_topic
[
60
];
// Should be enough for "CO2sensors/ESPd03cc5/info"
snprintf
(
info_topic
,
sizeof
(
info_topic
),
"%s/info"
,
publish_topic
.
c_str
());
snprintf
(
info_topic
,
sizeof
(
info_topic
),
"%s/info"
,
publish_topic
.
c_str
());
...
@@ -110,17 +99,13 @@ namespace mqtt {
...
@@ -110,17 +99,13 @@ namespace mqtt {
}
}
Serial
.
println
(
"'."
);
Serial
.
println
(
"'."
);
sensor_commands
::
run
(
messageString
.
c_str
());
sensor_commands
::
run
(
messageString
.
c_str
());
delay
(
50
);
led_effects
::
onBoardLEDOff
();
return
;
return
;
//TODO: Move this logic to a separate class, which could be used by Serial/MQTT/WebServer
//TODO: Move this logic to a separate class, which could be used by Serial/MQTT/WebServer
if
(
messageString
==
"calibrate"
)
{
if
(
messageString
==
"publish"
)
{
sensor
::
startCalibrationProcess
();
}
else
if
(
messageString
.
startsWith
(
"calibrate "
))
{
calibrateSensorToSpecificPPM
(
messageString
);
}
else
if
(
messageString
.
startsWith
(
"mqtt "
))
{
setMQTTinterval
(
messageString
);
}
else
if
(
messageString
==
"publish"
)
{
Serial
.
println
(
F
(
"Forcing MQTT publish now."
));
Serial
.
println
(
F
(
"Forcing MQTT publish now."
));
publish
(
sensor
::
timestamp
,
sensor
::
co2
,
sensor
::
temperature
,
sensor
::
humidity
);
publish
(
sensor
::
timestamp
,
sensor
::
co2
,
sensor
::
temperature
,
sensor
::
humidity
);
#ifdef AMPEL_CSV
#ifdef AMPEL_CSV
...
@@ -135,11 +120,7 @@ namespace mqtt {
...
@@ -135,11 +120,7 @@ namespace mqtt {
}
else
if
(
messageString
==
"reset"
)
{
}
else
if
(
messageString
==
"reset"
)
{
ESP
.
restart
();
// softer than ESP.reset()
ESP
.
restart
();
// softer than ESP.reset()
}
else
{
}
else
{
led_effects
::
showKITTWheel
(
color
::
red
,
1
);
Serial
.
println
(
F
(
"Message not supported. Doing nothing."
));
}
}
delay
(
50
);
led_effects
::
onBoardLEDOff
();
}
}
void
reconnect
()
{
void
reconnect
()
{
...
...
ampel-firmware/sensor_commands.cpp
View file @
a735b10a
...
@@ -62,6 +62,8 @@ namespace sensor_commands {
...
@@ -62,6 +62,8 @@ namespace sensor_commands {
Serial
.
print
(
" "
);
Serial
.
print
(
" "
);
Serial
.
println
(
callbacks
[
i
].
name
);
Serial
.
println
(
callbacks
[
i
].
name
);
}
}
led_effects
::
showKITTWheel
(
color
::
red
,
1
);
Serial
.
println
(
F
(
"Message not supported. Doing nothing."
));
}
}
void
run
(
const
char
*
command
)
{
void
run
(
const
char
*
command
)
{
...
...
ampel-firmware/sensor_commands.h
View file @
a735b10a
#include
<Arduino.h>
#include
<Arduino.h>
#include
"led_effects.h"
/** Other scripts can use this namespace, in order to define callbacks.
/** Other scripts can use this namespace, in order to define callbacks.
* Those callbacks can then be used to send commands to the sensor (reset, calibrate, night mode, ...)
* Those callbacks can then be used to send commands to the sensor (reset, calibrate, night mode, ...)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment