Skip to content

Commit 539e94d

Browse files
committed
docs: improve mqtt_example code docs
1 parent 6c9f851 commit 539e94d

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

mqtt_example/sdkconfig.defaults

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
88
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y
99
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y
1010
CONFIG_ESP_TLS_PSK_VERIFICATION=y
11+
12+
# Enable debug logs
13+
# CONFIG_LOG_MAXIMUM_LEVEL=4

mqtt_example/src/battery.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ mod capacity_curve {
9797
}
9898
}
9999

100+
/// Spawns a thread, that periodically reads battery voltage using ADC,
101+
/// transforms it into battery level and sends it to mpsc channel.
100102
pub fn spawn_reader_thread<'scope, T>(
101103
scope: &'scope Scope<'scope, '_>,
102104
adc: impl Peripheral<P = T::Adc> + 'scope + Send,

mqtt_example/src/controls.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ impl<'controls, SELECT: InputPin, ENTER: InputPin> Controls<'controls, SELECT, E
3434
})
3535
}
3636

37+
/// Spawns a thread that reads key presses,
38+
/// transforms them into `DeviceEvent`s and sends to mpsc channel.
3739
pub fn spawn_thread<'scope>(
3840
mut self,
3941
scope: &'scope Scope<'scope, '_>,
@@ -50,6 +52,12 @@ impl<'controls, SELECT: InputPin, ENTER: InputPin> Controls<'controls, SELECT, E
5052
let notifier_enter = notification.notifier();
5153

5254
// Usage of interrupts is currently unsafe.
55+
// We have to make sure, notifiers don't outlive the current task (thread).
56+
//
57+
// We are using notifiers instead of mpsc directly,
58+
// as the callback closure inside `subscribe`
59+
// is moved into ISR (Interrupt Service Routine),
60+
// and the list of allowed APIs there is heavily restricted.
5361
unsafe {
5462
self.btn_select
5563
.subscribe(move || {
@@ -63,6 +71,8 @@ impl<'controls, SELECT: InputPin, ENTER: InputPin> Controls<'controls, SELECT, E
6371
.unwrap();
6472
}
6573
loop {
74+
// We have to re-enable interrupts each time,
75+
// as they are automatically disabled when interrupt happens.
6676
self.enable_interrupts().unwrap();
6777
let btn_num = notification.wait(delay::BLOCK).unwrap().get();
6878
match btn_num {

mqtt_example/src/mqtt.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn configure() -> anyhow::Result<(EspMqttClient<'static>, EspMqttConnection)
1414
let mqtt_config = MqttClientConfiguration {
1515
username: Some(MQTT_USER),
1616
password: Some(MQTT_PASSWORD),
17+
// Enable MQTTS (MQTT with TLS)
1718
crt_bundle_attach: Some(esp_crt_bundle_attach),
1819
keep_alive_interval: Some(Duration::from_secs(10)),
1920
reconnect_timeout: Some(Duration::from_secs(10)),
@@ -26,6 +27,7 @@ pub fn configure() -> anyhow::Result<(EspMqttClient<'static>, EspMqttConnection)
2627
Ok((mqtt_client, mqtt_connection))
2728
}
2829

30+
/// Spawns a thread, that receives mqtt messages, parses them and sends to mpsc channel.
2931
pub fn spawn_receiver_thread<'scope>(
3032
scope: &'scope Scope<'scope, '_>,
3133
mut mqtt_connection: EspMqttConnection,
@@ -55,6 +57,7 @@ pub fn spawn_receiver_thread<'scope>(
5557
})
5658
}
5759

60+
/// Retries subscribing to topic until mqtt connection is established and subscription succeeds.
5861
pub fn try_until_subscribed(mqtt_client: &mut EspMqttClient, topic: &str) {
5962
loop {
6063
if let Err(_e) = mqtt_client.subscribe(topic, QoS::ExactlyOnce) {

0 commit comments

Comments
 (0)