newAM / ambientsensor-rs

A rust re-implementation of an old embedded C project.
MIT License
18 stars 1 forks source link

connection fails #70

Open LongZoz opened 1 year ago

LongZoz commented 1 year ago

when i start function->mqtt_sn,the program doesn't crash,but it's not connected to the target cloud,the program can execute to the first ok,then ... it break..... So what's the solution

`pub fn mqtt_sn(cx: mqtt_sn::Context){ defmt::info!("[TASK] mqtt_sn");

   (cx.shared.w5500_2, cx.shared.mqtt, cx.shared.mqtt_spawn_at).lock(
       |w5500_2, mqtt, mqtt_spawn_at| {
           loop {
               let now: u32 = monotonic_secs();
               defmt::info!("now is {:?}",now);
               // mqtt.subscribe(w5500, filter);
               match mqtt.process(w5500_2, now) {
                   Ok(MqttEvent::CallAfter(secs)) => {
                       *mqtt_spawn_at = Some(now + secs + 1);
                       mqtt.subscribe(w5500_2, "/filter/test1");
                       break;
                   }
                   Ok(MqttEvent::ConnAck) => {
                       defmt::info!("[MQTT] ConnAck");
                       // can subscribe to topics here
                       // not needed for this
                       mqtt.subscribe(w5500_2, "/filter/test");
                   }
                   Ok(MqttEvent::Publish(reader)) => {
                       log::warn!("should not get Publish never subscribed");
                       reader.done().unwrap();
                   }
                   Ok(MqttEvent::SubAck(_) | MqttEvent::UnSubAck(_)) => {
                       log::warn!("should not get (Un)SubAck, never (un)subscribed");
                   }
                   Ok(MqttEvent::None) => {
                       {
                           let mut data: heapless::String<16> = heapless::String::new();
                           write!(&mut data, "{:.1}", 1).unwrap();
                           mqtt.publish(w5500_2, TEMPERATURE_TOPIC, data.as_bytes())
                               .unwrap();
                       }
                       *mqtt_spawn_at = Some(now + 5);
                       break;
                   }
                   Err(e) => {
                       log::error!("[MQTT] {e:?}");
                       *mqtt_spawn_at = Some(now + 10);
                       break;
                   }
               }
           }
       },
   );

}`

newAM commented 1 year ago

What do the logs show?

LongZoz commented 1 year ago

I tried to print error,when i use tcp/ip connect another address(for example 192.168.0.1) ,mqtt connect target address(41.147.212.200) ,the result A 4,then disconnect the tcp/ip(192.168.0.1),finally return A 3 Err(e) => { match e{ w5500_mqtt::Error::StateTimeout(_c) => { defmt::info!("A "); match _c{ w5500_mqtt::State::Init => defmt::info!("2"), w5500_mqtt::State::WaitConInt => defmt::info!("3"), w5500_mqtt::State::WaitConAck => defmt::info!("4"), w5500_mqtt::State::Ready => defmt::info!("5"), } }, w5500_mqtt::Error::Disconnect => defmt::info!("A2"), w5500_mqtt::Error::TcpTimeout => defmt::info!("A3"), w5500_mqtt::Error::Decode => defmt::info!("A4"), w5500_mqtt::Error::Protocol => defmt::info!("A5"), w5500_mqtt::Error::ConnAck(_) => defmt::info!("A6"), w5500_mqtt::Error::OutOfMemory => defmt::info!("A7"), w5500_mqtt::Error::NotConnected => defmt::info!("A8"), w5500_mqtt::Error::Other(_) => defmt::info!("A9"), }

newAM commented 1 year ago

There are logs being printed out over UART, they contain a lot of information for debugging such as the status of the DHCP client. You can also look at a wireshark packet capture and your MQTT broker logs for more debug information.

LongZoz commented 1 year ago

it can send message to 192.168.0.1,as a client can receive from 192.168.0.1,but do mqtt_sn,the tcp connection(192.168.0.1) is disconnected,Perhaps it is trying to connect to mqtt's server using tcp,so (192.168.0.1) it disconnected

newAM commented 1 year ago

You'll definitely want to be looking at wireshark and your MQTT broker logs in that case. The UART logs would still be helpful because they'll inform you of the internal state.

LongZoz commented 1 year ago

I may have found the cause of the failure, if the mqtt server does not support users and passwords,i find this message in lib.rs

//! MQTT v5 client for the [Wiznet W5500] SPI internet offload chip.

//! # Limitations //! This is very basic at the moment, and will be expanded in the future. //! Does not support password protected MQTT servers. //! Only supports QoS 0: At most once delivery.`