Skip to content

Commit 8303a7d

Browse files
DaneSlatteryDane Slattery
authored andcommitted
Add EspModem to glue EspNetifDriver to TX/RX logic
1 parent 423ada8 commit 8303a7d

File tree

5 files changed

+886
-1
lines changed

5 files changed

+886
-1
lines changed

.github/configs/sdkconfig.defaults

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ CONFIG_ETH_SPI_ETHERNET_DM9051=y
1717
CONFIG_ETH_SPI_ETHERNET_W5500=y
1818
CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL=y
1919

20+
# GSM
21+
CONFIG_LWIP_PPP_SUPPORT=y
22+
CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=2048
23+
CONFIG_LWIP_PPP_ENABLE_IPV6=n
24+
25+
2026
# We don't have an example for classic BT - yet - we need to enable class BT
2127
# specifically to workaround this bug in ESP IDF v5.2 (fixed in ESP IDF v5.2.1+):
2228
# https://github.com/espressif/esp-idf/issues/13113

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ uncased = { version = "0.9.7", default-features = false }
5252
embedded-hal-async = { version = "1", default-features = false }
5353
embedded-svc = { version = "0.28", default-features = false }
5454
esp-idf-hal = { version = "0.44", default-features = false }
55-
embassy-time-driver = { version = "0.1", optional = true, features = ["tick-hz-1_000_000"] }
55+
embassy-time-driver = { version = "0.1", optional = true, features = [
56+
"tick-hz-1_000_000",
57+
] }
5658
embassy-futures = "0.1"
59+
at-commands = "0.5.4"
5760

5861
[build-dependencies]
5962
embuild = "0.32"

examples/lte_modem.rs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//! Example of using blocking wifi.
2+
//!
3+
//! Add your own ssid and password
4+
5+
use std::{thread::ScopedJoinHandle, time::Duration};
6+
7+
use embedded_svc::{
8+
http::{client::Client as HttpClient, Method},
9+
io::Write,
10+
utils::io,
11+
};
12+
13+
use esp_idf_hal::gpio;
14+
use esp_idf_hal::uart::UartDriver;
15+
use esp_idf_hal::units::Hertz;
16+
use esp_idf_svc::log::EspLogger;
17+
use esp_idf_svc::modem::sim::sim7600::SIM7600;
18+
use esp_idf_svc::modem::sim::SimModem;
19+
use esp_idf_svc::modem::EspModem;
20+
use esp_idf_svc::{eventloop::EspSystemEventLoop, nvs::EspDefaultNvsPartition};
21+
use esp_idf_svc::{hal::prelude::Peripherals, http::client::EspHttpConnection};
22+
23+
use log::{error, info};
24+
25+
// const SSID: &str = env!("WIFI_SSID");
26+
// const PASSWORD: &str = env!("WIFI_PASS");
27+
28+
fn main() -> anyhow::Result<()> {
29+
esp_idf_svc::sys::link_patches();
30+
EspLogger::initialize_default();
31+
32+
let peripherals = Peripherals::take()?;
33+
let sys_loop = EspSystemEventLoop::take()?;
34+
let nvs = EspDefaultNvsPartition::take()?;
35+
let serial = peripherals.uart2;
36+
let tx = peripherals.pins.gpio17;
37+
let rx = peripherals.pins.gpio18;
38+
let mut serial = UartDriver::new(
39+
serial,
40+
tx,
41+
rx,
42+
Option::<gpio::Gpio0>::None,
43+
Option::<gpio::Gpio0>::None,
44+
&esp_idf_hal::uart::UartConfig {
45+
baudrate: Hertz(115200),
46+
..Default::default()
47+
},
48+
)?;
49+
log::info!("Hello");
50+
let mut sim_device = SIM7600::new();
51+
let buff = [0u8; 64];
52+
match sim_device.negotiate(&mut serial, buff) {
53+
Err(x) => log::error!("Error = {}", x),
54+
Ok(()) => log::info!("Device in PPP mode"),
55+
}
56+
57+
let mut modem = EspModem::new(&mut serial, sys_loop)?;
58+
59+
let _scope = std::thread::scope::<_, anyhow::Result<()>>(|s| {
60+
let my_thread: ScopedJoinHandle<anyhow::Result<()>> = s.spawn(|| {
61+
match modem.run(buff) {
62+
Err(x) => log::error!("Error: {:?}", x),
63+
Ok(_x) => (),
64+
};
65+
Ok(())
66+
});
67+
std::thread::sleep(Duration::from_secs(10));
68+
69+
// while !modem.netif().is_up()? {}
70+
// while !(modem.is_connected()?) {}
71+
72+
let mut client = HttpClient::wrap(EspHttpConnection::new(&Default::default())?);
73+
74+
// GET
75+
loop {
76+
std::thread::sleep(Duration::from_secs(10));
77+
match get_request(&mut client) {
78+
Err(x) => log::error!("Failed, reason = {}", x),
79+
Ok(_) => break,
80+
}
81+
}
82+
my_thread.join().unwrap()?;
83+
Ok(())
84+
});
85+
86+
std::thread::sleep(core::time::Duration::from_secs(5));
87+
88+
Ok(())
89+
}
90+
91+
/// Send an HTTP GET request.
92+
fn get_request(client: &mut HttpClient<EspHttpConnection>) -> anyhow::Result<()> {
93+
// Prepare headers and URL
94+
let headers = [("accept", "text/plain")];
95+
let url = "http://ifconfig.net/";
96+
97+
// Send request
98+
//
99+
// Note: If you don't want to pass in any headers, you can also use `client.get(url, headers)`.
100+
let request = client.request(Method::Get, url, &headers)?;
101+
info!("-> GET {}", url);
102+
let mut response = request.submit()?;
103+
104+
// Process response
105+
let status = response.status();
106+
info!("<- {}", status);
107+
let mut buf = [0u8; 1024];
108+
let bytes_read = io::try_read_full(&mut response, &mut buf).map_err(|e| e.0)?;
109+
info!("Read {} bytes", bytes_read);
110+
match std::str::from_utf8(&buf[0..bytes_read]) {
111+
Ok(body_string) => info!(
112+
"Response body (truncated to {} bytes): {:?}",
113+
buf.len(),
114+
body_string
115+
),
116+
Err(e) => error!("Error decoding response body: {}", e),
117+
};
118+
119+
// Drain the remaining response bytes
120+
while response.read(&mut buf)? > 0 {}
121+
122+
Ok(())
123+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub mod mdns;
7878
esp_idf_comp_mqtt_enabled,
7979
esp_idf_comp_esp_event_enabled
8080
))]
81+
pub mod modem;
8182
pub mod mqtt;
8283
#[cfg(esp_idf_lwip_ipv4_napt)]
8384
pub mod napt;

0 commit comments

Comments
 (0)