Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel
ampel-firmware
Commits
8372df49
Commit
8372df49
authored
Apr 18, 2021
by
Eric Duminil
Browse files
NTP time : Slowly replacing Strings by C strings
parent
6ae78c3c
Changes
6
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/csv_writer.cpp
View file @
8372df49
...
...
@@ -132,11 +132,11 @@ namespace csv_writer {
return
csv_file
;
}
void
log
(
const
String
&
time
S
tamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
)
{
void
log
(
const
char
*
time
s
tamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
)
{
led_effects
::
onBoardLEDOn
();
File
csv_file
=
openOrCreate
();
char
csv_line
[
42
];
snprintf
(
csv_line
,
sizeof
(
csv_line
),
"%s;%d;%.1f;%.1f
\r\n
"
,
time
S
tamp
.
c_str
()
,
co2
,
temperature
,
humidity
);
snprintf
(
csv_line
,
sizeof
(
csv_line
),
"%s;%d;%.1f;%.1f
\r\n
"
,
time
s
tamp
,
co2
,
temperature
,
humidity
);
if
(
csv_file
)
{
size_t
written_bytes
=
csv_file
.
print
(
csv_line
);
csv_file
.
close
();
...
...
@@ -156,7 +156,7 @@ namespace csv_writer {
led_effects
::
onBoardLEDOff
();
}
void
logIfTimeHasCome
(
const
String
&
timeStamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
)
{
void
logIfTimeHasCome
(
const
char
*
timeStamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
)
{
unsigned
long
now
=
seconds
();
if
(
now
-
last_written_at
>
config
::
csv_interval
)
{
last_written_at
=
now
;
...
...
ampel-firmware/csv_writer.h
View file @
8372df49
...
...
@@ -22,7 +22,7 @@ namespace config {
namespace
csv_writer
{
extern
char
last_successful_write
[
23
];
void
initialize
();
void
logIfTimeHasCome
(
const
String
&
time
S
tamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
);
void
logIfTimeHasCome
(
const
char
*
time
s
tamp
,
const
int16_t
&
co2
,
const
float
&
temperature
,
const
float
&
humidity
);
int
getAvailableSpace
();
extern
const
String
filename
;
...
...
ampel-firmware/mqtt.cpp
View file @
8372df49
...
...
@@ -41,13 +41,13 @@ namespace mqtt {
" (Sends local IP and SSID via MQTT. Can be useful to find sensor)"
);
}
void
publish
(
const
String
&
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
void
publish
(
const
char
*
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
if
(
WiFi
.
status
()
==
WL_CONNECTED
&&
mqttClient
.
connected
())
{
led_effects
::
onBoardLEDOn
();
Serial
.
print
(
F
(
"MQTT - Publishing message ... "
));
char
payload
[
75
];
// Should be enough for json...
snprintf
(
payload
,
sizeof
(
payload
),
json_sensor_format
,
timestamp
.
c_str
()
,
co2
,
temperature
,
humidity
);
snprintf
(
payload
,
sizeof
(
payload
),
json_sensor_format
,
timestamp
,
co2
,
temperature
,
humidity
);
// Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da'
if
(
mqttClient
.
publish
(
publish_topic
.
c_str
(),
payload
))
{
Serial
.
println
(
F
(
"OK"
));
...
...
@@ -120,12 +120,12 @@ namespace mqtt {
}
}
void
publishIfTimeHasCome
(
const
String
&
time
S
tamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
)
{
void
publishIfTimeHasCome
(
const
char
*
time
s
tamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
)
{
// Send message via MQTT according to sending interval
unsigned
long
now
=
seconds
();
if
(
now
-
last_sent_at
>
config
::
mqtt_sending_interval
)
{
last_sent_at
=
now
;
publish
(
time
S
tamp
,
co2
,
temp
,
hum
);
publish
(
time
s
tamp
,
co2
,
temp
,
hum
);
}
}
...
...
ampel-firmware/mqtt.h
View file @
8372df49
...
...
@@ -15,7 +15,7 @@ namespace mqtt {
extern
bool
connected
;
void
initialize
(
String
&
topic
);
void
keepConnection
();
void
publishIfTimeHasCome
(
const
String
&
time
S
tamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
);
void
publishIfTimeHasCome
(
const
char
*
time
s
tamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
);
void
setMQTTinterval
(
int32_t
sending_interval
);
void
sendInfoAboutLocalNetwork
();
...
...
ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp
View file @
8372df49
...
...
@@ -152,19 +152,17 @@ int NTPClient::getSeconds() {
return
(
this
->
getEpochTime
()
%
60
);
}
String
NTPClient
::
getFormattedTime
(
unsigned
long
secs
)
{
void
NTPClient
::
getFormattedTime
(
char
*
formatted_time
,
unsigned
long
secs
)
{
unsigned
long
rawTime
=
secs
?
secs
:
this
->
getEpochTime
();
unsigned
int
hours
=
(
rawTime
%
86400L
)
/
3600
;
unsigned
int
minutes
=
(
rawTime
%
3600
)
/
60
;
unsigned
int
seconds
=
rawTime
%
60
;
char
formatted_time
[
9
];
snprintf
(
formatted_time
,
sizeof
(
formatted_time
),
"%02d:%02d:%02d"
,
hours
,
minutes
,
seconds
);
return
String
(
formatted_time
);
snprintf
(
formatted_time
,
9
,
"%02d:%02d:%02d"
,
hours
,
minutes
,
seconds
);
}
// Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp
void
NTPClient
::
getFormattedDate
(
char
*
formatted_date
,
unsigned
long
secs
)
{
void
NTPClient
::
getFormattedDate
(
char
*
formatted_date
,
unsigned
long
secs
)
{
unsigned
long
rawTime
=
(
secs
?
secs
:
this
->
getEpochTime
())
/
86400L
;
// in days
unsigned
long
days
=
0
,
year
=
1970
;
uint8_t
month
;
...
...
@@ -187,8 +185,9 @@ void NTPClient::getFormattedDate(char * formatted_date, unsigned long secs) {
month
++
;
// jan is month 1
rawTime
++
;
// first day is day 1
snprintf
(
formatted_date
,
23
,
"%4lu-%02d-%02lu %s%+03d"
,
year
,
month
,
rawTime
,
this
->
getFormattedTime
(
secs
).
c_str
(),
this
->
_timeOffset
/
3600
);
char
formatted_time
[
9
];
this
->
getFormattedTime
(
formatted_time
,
secs
);
snprintf
(
formatted_date
,
23
,
"%4lu-%02d-%02lu %s%+03d"
,
year
,
month
,
rawTime
,
formatted_time
,
this
->
_timeOffset
/
3600
);
}
void
NTPClient
::
end
()
{
...
...
ampel-firmware/src/lib/NTPClient-master/NTPClient.h
View file @
8372df49
...
...
@@ -80,7 +80,7 @@ class NTPClient {
/**
* @return secs argument (or 0 for current time) formatted like `hh:mm:ss`
*/
String
getFormattedTime
(
unsigned
long
secs
=
0
);
void
getFormattedTime
(
char
*
formatted_time
,
unsigned
long
secs
=
0
);
/**
* @return time in seconds since Jan. 1, 1970
...
...
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