Skip to content

Commit c0117c5

Browse files
Update training to latest releases (#257)
* feat: Update training to latest releases Co-authored-by: juliankrieger <[email protected]> * feat: Update handler methods * refactor: Use expect instead of unwrap * feat: Update esp-idf version and add bin section to Cargo.toml * docs: Update message_event docs --------- Co-authored-by: juliankrieger <[email protected]>
1 parent 3d672fb commit c0117c5

File tree

36 files changed

+156
-165
lines changed

36 files changed

+156
-165
lines changed

advanced/button-interrupt/.cargo/config.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"]
1010

1111
[unstable]
1212
build-std = ["panic_abort", "std"]
13-
build-std-features = ["panic_immediate_abort"]
1413

1514
[env]
1615
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.1.2)
17-
ESP_IDF_VERSION = { value = "tag:v5.1.2" }
16+
ESP_IDF_VERSION = { value = "tag:v5.2.2" }
1817

1918
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
2019
# that you might put in the root of the project

advanced/button-interrupt/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ authors = [
99
edition = "2021"
1010
resolver = "2"
1111

12+
[[bin]]
13+
name = "button-interrupt"
14+
harness = false # We can't use the default rust libtest harness for a crosscompile target
15+
1216
[profile.release]
1317
opt-level = "s"
1418

@@ -17,9 +21,9 @@ debug = true # Symbols are nice and they don't increase the size on Flash
1721
opt-level = "z"
1822

1923
[dependencies]
20-
anyhow = "=1.0.75"
21-
esp-idf-svc = "=0.47.3"
24+
anyhow = "=1.0.86"
25+
esp-idf-svc = "=0.49.0"
2226
rgb-led = { path = "../../common/lib/rgb-led" }
2327

2428
[build-dependencies]
25-
embuild = "=0.31.4"
29+
embuild = "=0.32.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-11-14"
2+
channel = "nightly-2024-06-30"
33
components = ["rust-src"]

advanced/i2c-driver/.cargo/config.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"]
1010

1111
[unstable]
1212
build-std = ["panic_abort", "std"]
13-
build-std-features = ["panic_immediate_abort"]
1413

1514
[env]
1615
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.1.2)
17-
ESP_IDF_VERSION = { value = "tag:v5.1.2" }
16+
ESP_IDF_VERSION = { value = "tag:v5.2.2" }
1817

1918
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
2019
# that you might put in the root of the project

advanced/i2c-driver/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ authors = [
88
edition = "2021"
99
resolver = "2"
1010

11+
[[bin]]
12+
name = "i2c-driver"
13+
harness = false # We can't use the default rust libtest harness for a crosscompile target
14+
1115
[profile.release]
1216
opt-level = "s"
1317

@@ -16,9 +20,9 @@ debug = true # Symbols are nice and they don't increase the size on Flash
1620
opt-level = "z"
1721

1822
[dependencies]
19-
anyhow = "=1.0.75"
23+
anyhow = "=1.0.86"
2024
embedded-hal = "=0.2.7"
21-
esp-idf-svc = "=0.47.3"
25+
esp-idf-svc = "=0.49.0"
2226

2327
[build-dependencies]
24-
embuild = "=0.31.4"
28+
embuild = "=0.32.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-11-14"
2+
channel = "nightly-2024-06-30"
33
components = ["rust-src"]

advanced/i2c-sensor-reading/.cargo/config.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"]
1010

1111
[unstable]
1212
build-std = ["panic_abort", "std"]
13-
build-std-features = ["panic_immediate_abort"]
1413

1514
[env]
1615
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.1.2)
17-
ESP_IDF_VERSION = { value = "tag:v5.1.2" }
16+
ESP_IDF_VERSION = { value = "tag:v5.2.2" }
1817

1918
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
2019
# that you might put in the root of the project

advanced/i2c-sensor-reading/Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ authors = [
88
edition = "2021"
99
resolver = "2"
1010

11+
[[bin]]
12+
name = "i2c-sensor-reading"
13+
harness = false # We can't use the default rust libtest harness for a crosscompile target
14+
1115
[profile.release]
1216
opt-level = "s"
1317

@@ -16,13 +20,13 @@ debug = true # Symbols are nice and they don't increase the size on Flash
1620
opt-level = "z"
1721

1822
[dependencies]
19-
anyhow = "=1.0.75"
23+
anyhow = "=1.0.86"
2024
embedded-hal = "=0.2.7"
21-
esp-idf-svc = "=0.47.3"
25+
esp-idf-svc = "=0.49.0"
2226
icm42670 = "=0.1.1"
2327
lis3dh = "=0.4.2"
2428
shared-bus = "=0.3.1"
2529
shtcx = "=0.11.0"
2630

2731
[build-dependencies]
28-
embuild = "=0.31.4"
32+
embuild = "=0.32.0"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-11-14"
2+
channel = "nightly-2024-06-30"
33
components = ["rust-src"]

book/src/03_5_3_mqtt.md

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -39,41 +39,19 @@ client.subscribe(subscribe_topic, QoS::AtLeastOnce)
3939
## Handling Incoming Messages
4040

4141

42-
The `message_event` parameter in the handler closure is of type `Result<Event<EspMqttMessage>`.
43-
Since we're only interested in processing successfully received messages, we can make use of deep pattern matching into the closure:
42+
The `message_event` parameter in the handler closure is of type `EspMqttEvent`, which has a `payload()` method to access the `EventPayload`
43+
Since we're only interested in processing successfully received messages:
4444

4545
```rust
46-
let mut client =
47-
EspMqttClient::new(
48-
broker_url,
49-
&mqtt_config,
50-
move |message_event| match message_event {
51-
Ok(Received(msg)) => process_message(msg, &mut led),
52-
_ => warn!("Received from MQTT: {:?}", message_event),
53-
},
54-
)?;
46+
{{#include ../../intro/mqtt/exercise/examples/solution_publ_rcv.rs:mqtt_client}}
5547
```
5648

5749
In the processing function, you will handle `Complete` messages.
5850

5951
💡 Use Rust Analyzer to generate the missing match arms or match any other type of response by logging an `info!()`.
6052

6153
```rust
62-
match message.details() {
63-
// All messages in this exercise will be of type `Complete`
64-
// The other variants of the `Details` enum are for larger message payloads
65-
Complete => {
66-
67-
// Cow<&[u8]> can be coerced into a slice &[u8] or a Vec<u8>
68-
// You can coerce it into a slice to be sent to try_from()
69-
let message_data: &[u8] = &message.data();
70-
if let Ok(ColorData::BoardLed(color)) = ColorData::try_from(message_data) {
71-
// Set the LED to the newly received color
72-
73-
}
74-
}
75-
// Use Rust Analyzer to generate the missing match arms or match an incomplete message with a log message.
76-
}
54+
{{#include ../../intro/mqtt/exercise/examples/solution_publ_rcv.rs:process_message}}
7755
```
7856

7957
💡 Use a logger to see what you are receiving, for example, `info!("{}", color);` or `dbg!(color)`.
@@ -98,7 +76,6 @@ let raw = RawCommandData {
9876
path: command,
9977
data: message.data(),
10078
};
101-
10279
```
10380

10481
Check the `host-client`:
@@ -109,36 +86,6 @@ Check the `host-client`:
10986
let command = Command::BoardLed(color)
11087
```
11188

112-
✅ in the `process_message()` function, you will need to parse the topic.
113-
114-
```rust
115-
match message.details() {
116-
Complete => {
117-
// All messages in this exercise will be of type `Complete`
118-
// the other variants of the `Details` enum
119-
// are for larger message payloads
120-
121-
// Cow<str> behaves a lot like other Rust strings (&str, String)
122-
let topic: Cow<str> = message.topic(token);
123-
124-
// Determine if we're interested in this topic and
125-
// Dispatch based on its content
126-
let is_command_topic: bool = /* ... */;
127-
if is_command_topic {
128-
let raw = RawCommandData { /* ... */ };
129-
if let Ok(Command::BoardLed(color)) = Command::try_from(raw) {
130-
// Set the LED to the newly received color
131-
}
132-
133-
},
134-
_ => {}
135-
}
136-
}
137-
```
138-
139-
💡 Since you will be iterating over a MQTT topic, you will need to `split()` on a string returns an iterator. You can access a specific item from an iterator using [`nth()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.nth).
140-
💡 The solution implementing hierarchy can be run with `cargo run --example solution2`, while the solution without can be run with `cargo run` or `cargo run --example solution1`
141-
14289
### Other Tasks
14390

14491
✅ Leverage [`serde_json`](https://docs.serde.rs/serde_json/) to encode/decode your message data as JSON.

0 commit comments

Comments
 (0)