more updates to mqtt

This commit is contained in:
Evan Feenstra
2023-03-31 12:35:04 -07:00
parent 4619a66076
commit dcd6798504
5 changed files with 73 additions and 23 deletions

View File

@@ -1,8 +1,8 @@
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use embedded_svc::storage::StorageBase;
use esp_idf_svc::nvs::EspNvsStorage;
use esp_idf_svc::nvs::*;
use esp_idf_svc::nvs_storage::EspNvsStorage;
use std::sync::Arc;

View File

@@ -4,8 +4,9 @@ use sphinx_signer::sphinx_glyph::control::Config;
use serde::Deserialize;
use std::sync::{Arc, Condvar, Mutex};
use embedded_svc::http::server::registry::Registry;
// use embedded_svc::http::server::registry::Registry;
use embedded_svc::http::server::*;
use embedded_svc::httpd::registry::Registry;
use embedded_svc::httpd::Result;
use esp_idf_svc::httpd as idf;

View File

@@ -3,14 +3,14 @@ use sphinx_signer::sphinx_glyph::topics;
use anyhow::Result;
use embedded_svc::mqtt::client::utils::ConnState;
use embedded_svc::mqtt::client::utils::Connection as MqttConnection;
use embedded_svc::mqtt::client::{Connection, Event, Message as MqttMessage, MessageImpl, QoS};
use embedded_svc::utils::mqtt::client::Connection as MqttConnection;
use embedded_svc::utils::mutex::Condvar;
use esp_idf_svc::mqtt::client::*;
use esp_idf_sys::EspError;
use esp_idf_sys::{self};
use log::*;
use std::sync::mpsc;
use std::sync::Condvar;
use std::thread;
pub const QOS: QoS = QoS::ExactlyOnce;
@@ -20,10 +20,8 @@ pub fn make_client(
client_id: &str,
username: &str,
password: &str,
) -> Result<(
EspMqttClient<ConnState<MessageImpl, EspError>>,
MqttConnection<Condvar, MessageImpl, EspError>,
)> {
tx: mpsc::Sender<CoreEvent>,
) -> Result<EspMqttClient<ConnState<MessageImpl, EspError>>> {
log::info!("make_client with id {}", client_id);
let conf = MqttClientConfiguration {
client_id: Some(client_id),
@@ -37,20 +35,11 @@ pub fn make_client(
};
let b = format!("mqtt://{}", broker);
// let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?;
let cc = EspMqttClient::new_with_conn(b, &conf)?;
let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?;
// let cc = EspMqttClient::new_with_conn(b, &conf)?;
info!("MQTT client started");
Ok(cc)
}
pub fn start_listening(
client: EspMqttClient<ConnState<MessageImpl, EspError>>,
mut connection: MqttConnection<Condvar, MessageImpl, EspError>,
tx: mpsc::Sender<CoreEvent>,
) -> Result<EspMqttClient<ConnState<MessageImpl, EspError>>> {
// must start pumping before subscribe or publish will not work
thread::spawn(move || {
info!("MQTT Listening for messages");
loop {
@@ -102,5 +91,65 @@ pub fn start_listening(
//info!("MQTT connection loop exit");
});
Ok(client)
Ok(cc)
}
// pub fn start_listening(
// client: EspMqttClient<ConnState<MessageImpl, EspError>>,
// mut connection: MqttConnection<Condvar, MessageImpl, EspError>,
// tx: mpsc::Sender<CoreEvent>,
// ) -> Result<EspMqttClient<ConnState<MessageImpl, EspError>>> {
// // must start pumping before subscribe or publish will not work
// thread::spawn(move || {
// info!("MQTT Listening for messages");
// loop {
// match connection.next() {
// Some(msg) => match msg {
// Err(e) => match e.to_string().as_ref() {
// "ESP_FAIL" => {
// error!("ESP_FAIL msg!");
// }
// _ => error!("Unknown error: {}", e),
// },
// Ok(msg) => match msg {
// Event::BeforeConnect => info!("RECEIVED BeforeConnect MESSAGE"),
// Event::Connected(_flag) => {
// info!("RECEIVED Connected MESSAGE");
// tx.send(CoreEvent::Connected)
// .expect("couldnt send Event::Connected");
// }
// Event::Disconnected => {
// warn!("RECEIVED Disconnected MESSAGE");
// tx.send(CoreEvent::Disconnected)
// .expect("couldnt send Event::Disconnected");
// }
// Event::Subscribed(_mes_id) => info!("RECEIVED Subscribed MESSAGE"),
// Event::Unsubscribed(_mes_id) => info!("RECEIVED Unsubscribed MESSAGE"),
// Event::Published(_mes_id) => info!("RECEIVED Published MESSAGE"),
// Event::Received(msg) => {
// let topic_opt = msg.topic();
// if let Some(topic) = topic_opt {
// match topic {
// topics::VLS => tx
// .send(CoreEvent::VlsMessage(msg.data().to_vec()))
// .expect("couldnt send Event::VlsMessage"),
// topics::CONTROL => tx
// .send(CoreEvent::Control(msg.data().to_vec()))
// .expect("couldnt send Event::Control"),
// _ => log::warn!("unrecognized topic {}", topic),
// };
// } else {
// log::warn!("empty topic in msg!!!");
// }
// }
// Event::Deleted(_mes_id) => info!("RECEIVED Deleted MESSAGE"),
// },
// },
// None => break,
// }
// }
// //info!("MQTT connection loop exit");
// });
// Ok(client)
// }

View File

@@ -30,7 +30,7 @@ pub fn start_client(
let sysloop = EspSystemEventLoop::take()?;
let mut wifi = Box::new(EspWifi::new(modem, sysloop, default_nvs)?);
let mut wifi = Box::new(EspWifi::new(modem, sysloop, Some(default_nvs))?);
let ap_infos = wifi.scan()?;
let ssid = config.ssid.as_str();
let pass = config.pass.as_str();

View File

@@ -142,8 +142,8 @@ fn make_and_launch_client(
let token = ctrlr.make_auth_token().expect("couldnt make auth token");
log::info!("PUBKEY {} TOKEN {}", &pubkey, &token);
let (mqtt, connection) = conn::mqtt::make_client(&config.broker, CLIENT_ID, &pubkey, &token)?;
let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;
let mqtt_client = conn::mqtt::make_client(&config.broker, CLIENT_ID, &pubkey, &token, tx)?;
// let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;
// this blocks forever... the "main thread"
let do_log = true;