use futures_lite::StreamExt;
// const SSID: &str = "IOT-MikroTik-TEST";
// const PASSWORD: &str = "11223344";
// const SSID: &str = "TeleCentro-33bb";
// const PASSWORD: &str = "DDZ4ZYZETYHV";
const SSID: &str = "Personal-WiFi-789-2.4Ghz";
const PASSWORD: &str = "UFgxUuqz5Z";
esp_idf_svc::hal::sys::esp_app_desc!();
fn main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
let config = esp_idf_svc::hal::sys::esp_vfs_eventfd_config_t {
max_fds: 8,
..Default::default()
};
esp_idf_svc::hal::sys::esp! { unsafe { esp_idf_svc::hal::sys::esp_vfs_eventfd_register(&config) } }?;
let peripherals = esp_idf_svc::hal::peripherals::Peripherals::take()?;
// wifi setup
let sysloop = esp_idf_svc::eventloop::EspSystemEventLoop::take()?;
let nvs = esp_idf_svc::nvs::EspDefaultNvsPartition::take()?;
log::warn!("initializing wifi...");
let mut wifi = esp_idf_svc::wifi::BlockingWifi::wrap(
esp_idf_svc::wifi::EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs))?,
sysloop,
)?;
let wifi_configuration: esp_idf_svc::wifi::Configuration = esp_idf_svc::wifi::Configuration::Client(
esp_idf_svc::wifi::ClientConfiguration {
ssid: SSID.try_into().unwrap(),
bssid: None,
auth_method: esp_idf_svc::wifi::AuthMethod::WPA2Personal,
password: PASSWORD.try_into().unwrap(),
channel: None,
}
);
wifi.set_configuration(&wifi_configuration)?;
wifi.start()?;
log::warn!("Wifi started");
wifi.connect()?;
log::warn!("Wifi connected");
wifi.wait_netif_up()?;
log::warn!("Wifi netif up");
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
log::warn!("Wifi DHCP info: {:?}", ip_info);
// timer setup
let mut timer = esp_idf_svc::hal::timer::TimerDriver::new(
peripherals.timer00,
&esp_idf_svc::hal::timer::TimerConfig::new())?;
let delay_ms = 2500;
log::warn!("current ticks: {} | target delay: {}ms",timer.tick_hz(),delay_ms);
log::info!("async code START");
// tokio
tokio::runtime::Builder::new_current_thread()
// .thread_stack_size(1024)
.enable_all()
.build()?
.block_on(
async {
// async test
log::warn!("awaiting launched");
timer.delay(1000 * delay_ms).await?;
log::warn!("awaiting compleated!");
async_nats_thread().await?;
Ok(())
}
)
}
async fn async_nats_thread() -> anyhow::Result<()> {
// async nats
let cert = include_bytes!("../NGS-Default-Rust.creds");
let cert_str = std::str::from_utf8(cert)?;
log::error!("{}", cert_str);
// Conectarse al servidor NATS con TLS y proporcionar las credenciales JWT
let nc = async_nats::ConnectOptions::with_credentials(cert_str)?
.connect("connect.ngs.global")
.await?;
// Crear una suscripción a un tema
let mut subs = nc.subscribe("tema_prueba").await?;
subs.unsubscribe_after(1).await?;
// Publicar un mensaje en el tema
let data = bytes::Bytes::from("Hola desde Rust");
nc.publish("tema_prueba", data).await?;
println!("Mensaje publicado con éxito en 'tema_prueba'");
if let Some(message) = subs.next().await {
println!("Mensaje Recibido: {:?}", (message));
}
println!("No mas mensajes, unsubscribed");
Ok(())
}