Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
co2ampel2
co2-ampel
Commits
3031d3b1
Commit
3031d3b1
authored
Apr 10, 2025
by
StetterAme77
Browse files
MQTT Message new content and format
parent
c8789a6d
Pipeline
#11053
passed with stage
in 1 minute and 53 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
ampel-firmware/ampel-firmware.ino
View file @
3031d3b1
...
@@ -139,7 +139,7 @@ void loop() {
...
@@ -139,7 +139,7 @@ void loop() {
}
}
if
(
config
::
is_wifi_on
&&
config
::
is_mqtt_active
())
{
if
(
config
::
is_wifi_on
&&
config
::
is_mqtt_active
())
{
mqtt
::
publishIfTimeHasCome
(
sensor
::
timestamp
,
sensor
::
co2
,
sensor
::
temperature
,
sensor
::
humidity
);
mqtt
::
publishIfTimeHasCome
(
ampel
.
macAddress
,
sensor
::
timestamp
,
sensor
::
co2
,
sensor
::
temperature
,
sensor
::
humidity
);
}
}
#if defined(ESP32)
#if defined(ESP32)
...
...
ampel-firmware/mqtt.cpp
View file @
3031d3b1
...
@@ -38,7 +38,7 @@ namespace mqtt {
...
@@ -38,7 +38,7 @@ namespace mqtt {
char
last_successful_publish
[
23
]
=
""
;
char
last_successful_publish
[
23
]
=
""
;
void
initialize
(
const
char
*
sensorId
)
{
void
initialize
(
const
char
*
sensorId
)
{
json_sensor_format
=
PSTR
(
"{
\"
time
\"
:
\"
%s
\"
,
\"
co2
\"
:%d,
\"
temp
\"
:%.1f,
\"
rh
\"
:%.1f}"
);
json_sensor_format
=
PSTR
(
"{
\"
metadata
\"
:{
\"
mac-address
\"
:
\"
%s
\"
,
\"
time
\"
:
\"
%s
\"
}
,
\"
co2
\"
:%d,
\"
temp
\"
:%.1f,
\"
rh
\"
:%.1f}"
);
snprintf
(
publish_topic
,
sizeof
(
publish_topic
),
"%s%s"
,
config
::
mqtt_topic_prefix
,
sensorId
);
snprintf
(
publish_topic
,
sizeof
(
publish_topic
),
"%s%s"
,
config
::
mqtt_topic_prefix
,
sensorId
);
if
(
config
::
mqtt_encryption
)
{
if
(
config
::
mqtt_encryption
)
{
...
@@ -59,15 +59,15 @@ namespace mqtt {
...
@@ -59,15 +59,15 @@ namespace mqtt {
F
(
"(Sends local IP and SSID via MQTT. Can be useful to find sensor)"
));
F
(
"(Sends local IP and SSID via MQTT. Can be useful to find sensor)"
));
}
}
void
publish
(
const
char
*
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
void
publish
(
const
char
*
macaddress
,
const
char
*
timestamp
,
int16_t
co2
,
float
temperature
,
float
humidity
)
{
if
(
wifi
::
connected
()
&&
mqttClient
.
connected
())
{
if
(
wifi
::
connected
()
&&
mqttClient
.
connected
())
{
led_effects
::
onBoardLEDOn
();
led_effects
::
onBoardLEDOn
();
Serial
.
print
(
F
(
"MQTT - Publishing message to '"
));
Serial
.
print
(
F
(
"MQTT - Publishing message to '"
));
Serial
.
print
(
publish_topic
);
Serial
.
print
(
publish_topic
);
Serial
.
print
(
F
(
"' ... "
));
Serial
.
print
(
F
(
"' ... "
));
char
payload
[
75
];
// Should be enough for json...
char
payload
[
130
];
// Should be enough for json...
snprintf
(
payload
,
sizeof
(
payload
),
json_sensor_format
,
timestamp
,
co2
,
temperature
,
humidity
);
snprintf
(
payload
,
sizeof
(
payload
),
json_sensor_format
,
macaddress
,
timestamp
,
co2
,
temperature
,
humidity
);
// Topic is 'MQTT_TOPIC_PREFIX/ESP123456'
// Topic is 'MQTT_TOPIC_PREFIX/ESP123456'
if
(
mqttClient
.
publish
(
publish_topic
,
payload
))
{
if
(
mqttClient
.
publish
(
publish_topic
,
payload
))
{
Serial
.
println
(
F
(
"OK"
));
Serial
.
println
(
F
(
"OK"
));
...
@@ -154,12 +154,12 @@ namespace mqtt {
...
@@ -154,12 +154,12 @@ namespace mqtt {
}
}
}
}
void
publishIfTimeHasCome
(
const
char
*
timestamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
)
{
void
publishIfTimeHasCome
(
const
char
*
macaddress
,
const
char
*
timestamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
)
{
// Send message via MQTT according to sending interval
// Send message via MQTT according to sending interval
unsigned
long
now
=
seconds
();
unsigned
long
now
=
seconds
();
if
(
now
-
last_sent_at
>
config
::
mqtt_sending_interval
)
{
if
(
now
-
last_sent_at
>
config
::
mqtt_sending_interval
)
{
last_sent_at
=
now
;
last_sent_at
=
now
;
publish
(
timestamp
,
co2
,
temp
,
hum
);
publish
(
macaddress
,
timestamp
,
co2
,
temp
,
hum
);
}
}
}
}
...
...
ampel-firmware/mqtt.h
View file @
3031d3b1
...
@@ -8,7 +8,7 @@ namespace mqtt {
...
@@ -8,7 +8,7 @@ namespace mqtt {
extern
bool
connected
;
extern
bool
connected
;
void
initialize
(
const
char
*
sensorId
);
void
initialize
(
const
char
*
sensorId
);
void
keepConnection
();
void
keepConnection
();
void
publishIfTimeHasCome
(
const
char
*
timestamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
);
void
publishIfTimeHasCome
(
const
char
*
macaddress
,
const
char
*
timestamp
,
const
int16_t
&
co2
,
const
float
&
temp
,
const
float
&
hum
);
void
setMQTTinterval
(
int32_t
sending_interval
);
void
setMQTTinterval
(
int32_t
sending_interval
);
void
sendInfoAboutLocalNetwork
();
void
sendInfoAboutLocalNetwork
();
...
...
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