compiled rust mqtt
This commit is contained in:
@@ -17,7 +17,6 @@ use esp_wifi::{
|
|||||||
wifi::{ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiState},
|
wifi::{ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent, WifiState},
|
||||||
};
|
};
|
||||||
use log::info;
|
use log::info;
|
||||||
mod mqtt;
|
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
pub mod mqtt;
|
||||||
|
|||||||
@@ -1,21 +1,25 @@
|
|||||||
use embassy_time::{Duration, Timer};
|
|
||||||
use embassy_net::{tcp::TcpSocket, Stack};
|
use embassy_net::{tcp::TcpSocket, Stack};
|
||||||
use esp_wifi::wifi::WifiDevice;
|
use embassy_time::{Duration, Timer};
|
||||||
use rust_mqtt::client::client::MqttClient;
|
|
||||||
use rust_mqtt::packet::v5::{connect::ConnectPacket, publish::PublishPacket};
|
|
||||||
use rust_mqtt::utils::rng_generator::CountingRng32;
|
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
|
use rust_mqtt::client::client::MqttClient;
|
||||||
|
use rust_mqtt::client::client_config::{ClientConfig, MqttVersion};
|
||||||
|
use rust_mqtt::packet::v5::publish_packet::QualityOfService;
|
||||||
|
use rust_mqtt::utils::rng_generator::CountingRng;
|
||||||
|
|
||||||
#[embassy_executor::task]
|
#[embassy_executor::task]
|
||||||
pub async fn mqtt_task(stack: &'static Stack<WifiDevice<'static>>) {
|
pub async fn mqtt_task(stack: &'static Stack<'static>) {
|
||||||
info!("MQTT task starting...");
|
info!("MQTT task starting...");
|
||||||
|
|
||||||
|
// TCP socket buffers
|
||||||
static mut RX_BUFFER: [u8; 2048] = [0; 2048];
|
static mut RX_BUFFER: [u8; 2048] = [0; 2048];
|
||||||
static mut TX_BUFFER: [u8; 2048] = [0; 2048];
|
static mut TX_BUFFER: [u8; 2048] = [0; 2048];
|
||||||
|
|
||||||
let mut socket = TcpSocket::new(stack, unsafe { &mut RX_BUFFER }, unsafe { &mut TX_BUFFER });
|
// Fix #1: Dereference stack
|
||||||
|
let mut socket =
|
||||||
|
TcpSocket::new(*stack, unsafe { &mut RX_BUFFER }, unsafe { &mut TX_BUFFER });
|
||||||
|
|
||||||
// TODO: replace with your broker IP
|
// Connect to broker
|
||||||
use embassy_net::Ipv4Address;
|
use embassy_net::Ipv4Address;
|
||||||
let broker_ip = Ipv4Address::new(192, 168, 1, 100);
|
let broker_ip = Ipv4Address::new(192, 168, 1, 100);
|
||||||
let broker_port = 1883;
|
let broker_port = 1883;
|
||||||
@@ -24,30 +28,61 @@ pub async fn mqtt_task(stack: &'static Stack<WifiDevice<'static>>) {
|
|||||||
info!("TCP connect failed: {:?}", e);
|
info!("TCP connect failed: {:?}", e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
info!("Connected to MQTT broker");
|
info!("Connected to TCP broker");
|
||||||
|
|
||||||
let rng = CountingRng32::new();
|
// MQTT client needs TWO separate buffers!
|
||||||
let mut client: MqttClient<_, _, 1024, 1024> = MqttClient::new(socket, rng);
|
static mut MQTT_WRITE_BUF: [u8; 1024] = [0; 1024];
|
||||||
|
static mut MQTT_RECV_BUF: [u8; 1024] = [0; 1024];
|
||||||
|
|
||||||
let connect = ConnectPacket::new("esp32-client");
|
// Fix #2: CountingRng initialization
|
||||||
if client.connect(connect).await.is_ok() {
|
let rng = CountingRng(12345);
|
||||||
info!("MQTT CONNECT sent");
|
|
||||||
|
// Fix #3: ClientConfig takes MqttVersion and rng
|
||||||
|
let mut client_config = ClientConfig::new(MqttVersion::MQTTv5, rng);
|
||||||
|
client_config.add_client_id("esp32-client");
|
||||||
|
client_config.keep_alive = 60;
|
||||||
|
|
||||||
|
// Fix #4: MqttClient::new takes TWO buffers (write + receive)
|
||||||
|
let mut client: MqttClient<_, 16, _> = MqttClient::new(
|
||||||
|
socket,
|
||||||
|
unsafe { &mut MQTT_WRITE_BUF }, // write buffer
|
||||||
|
1024, // write buffer length
|
||||||
|
unsafe { &mut MQTT_RECV_BUF }, // receive buffer
|
||||||
|
1024, // receive buffer length
|
||||||
|
client_config,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Connect to MQTT broker
|
||||||
|
match client.connect_to_broker().await {
|
||||||
|
Ok(_) => info!("MQTT CONNECT successful"),
|
||||||
|
Err(e) => {
|
||||||
|
info!("MQTT connect failed: {:?}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let publish = PublishPacket::new("esp32/topic", b"hello from esp32", 0);
|
// Publish a message using send_message()
|
||||||
if client.publish(publish).await.is_ok() {
|
match client.send_message(
|
||||||
info!("MQTT PUBLISH sent");
|
"esp32/topic",
|
||||||
|
b"hello from esp32",
|
||||||
|
QualityOfService::QoS0,
|
||||||
|
false // retain
|
||||||
|
).await {
|
||||||
|
Ok(_) => info!("MQTT message published"),
|
||||||
|
Err(e) => info!("MQTT publish error: {:?}", e),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep polling
|
// Main loop - receive messages
|
||||||
loop {
|
loop {
|
||||||
match client.poll().await {
|
match client.receive_message().await {
|
||||||
Ok(_) => {}
|
Ok((topic, payload)) => {
|
||||||
|
info!("Received on {}: {:?}", topic, payload);
|
||||||
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
info!("MQTT error: {:?}", e);
|
info!("MQTT error: {:?}", e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Timer::after(Duration::from_secs(5)).await;
|
Timer::after(Duration::from_millis(100)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user