Skip to content

Commit

Permalink
Error Handling for serial-forwarder
Browse files Browse the repository at this point in the history
  • Loading branch information
Nereuxofficial committed Jun 4, 2023
1 parent 810f9c9 commit 7cb4a4f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 3 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ This is a simple gateway to forward data from ESP-Now to http for the Project Pl

### serial-forwarder
1. `cd serial-forwarder`
2. `cargo run --release` to build and run the serial-forwarder
2. Install espmonitor via `cargo install espmonitor`
3. `cargo run --release` to build and run the serial-forwarder

## Adapting for your own use case

If you want to reuse this code for your own use case, you need to change the following things:

1. Change the data structures in `src/main.rs` to match your own data
2. Encode the data sent by your other ESP using the rust postcard library
3. Adapt serial-forwarder to your own use case

Change the data structures in `esp-firmware/src/main.rs` and `serial-forwarder` to match your own data
## Example Output

![Example Output](example.png)
Expand Down
9 changes: 7 additions & 2 deletions serial-forwarder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ async fn main() {

while let Some(line) = reader.next_line().await.unwrap() {
if line.starts_with("Decoded:") {
let data: SensorData = serde_json::from_str(&line[8..]).unwrap();
let data: Result<SensorData, serde_json::Error> = serde_json::from_str(&line[8..]);
// We do not want to block on network requests
if data.is_err() {
println!("Error decoding data: {}", data.err().unwrap());
continue;
}
task::spawn(async move {
// For efficiencys sake we could use a single client for all requests, but that would involve an Arc<Mutex<>> and the gain is too little to justify the complexity
let mut client = reqwest::Client::new();
let server_data = data.into();
let server_data = data.unwrap().into();
match publish_data(&mut client, &server_data).await {
Ok(_) => println!("Published data {:?}", server_data),
Err(e) => println!("Error publishing data: {}", e),
Expand Down

0 comments on commit 7cb4a4f

Please sign in to comment.