You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android通过MQTT向Raspberry Pi发布GPIO控制消息无响应问题排查求助

Troubleshooting MQTT Message Delivery from Android to Raspberry Pi

Let’s work through the possible issues in your code and setup to get those messages flowing properly:

1. Fix the Switch Listener Setup (Android App)

Right now, your switch’s OnCheckedChangeListener only gets attached when you trigger the Switch() method (likely tied to a button click). If you toggle the switch without hitting that button, nothing will happen. Let’s move the listener setup to onCreate() so it’s active as soon as the app starts, and clean up the publish logic:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String clientId = MqttClient.generateClientId();
    client = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.2.193:1883", clientId);
    
    // Initialize switch and attach listener immediately
    aswitch = findViewById(R.id.simpleSwitch1);
    aswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            publishToMqtt(isChecked ? "On" : "Off");
        }
    });
}

// Reusable publish method with connection checks and feedback
private void publishToMqtt(String payload) {
    if (!client.isConnected()) {
        Toast.makeText(this, "Not connected to MQTT broker", Toast.LENGTH_SHORT).show();
        return;
    }
    
    String topic = "rpi/gpio";
    try {
        byte[] encodedPayload = payload.getBytes(StandardCharsets.UTF_8);
        MqttMessage message = new MqttMessage(encodedPayload);
        message.setRetained(true);
        client.publish(topic, message);
        Toast.makeText(this, "Sent: " + payload, Toast.LENGTH_SHORT).show();
    } catch (MqttException e) {
        e.printStackTrace();
        Toast.makeText(this, "Publish failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

This change ensures the switch responds immediately when toggled, and gives you clear feedback if publishing fails or the client isn’t connected.

2. Raspberry Pi & Broker Checks

If the app now shows "Sent" toasts but the Pi still doesn’t get messages, verify these:

  • Topic Case Sensitivity: MQTT topics are case-sensitive. Make sure your Pi is subscribed to exactly rpi/gpio (not RPI/GPIO or any variation). Test with this command on the Pi:

    mosquitto_sub -h localhost -t "rpi/gpio" -v
    

    Toggle the Android switch—if this command shows the message, the broker is working, and the issue is with your Pi’s subscription code.

  • Broker Network Access: Ensure your MQTT broker (e.g., Mosquitto) is listening on all network interfaces, not just localhost. Edit your mosquitto.conf file and uncomment/set:

    listener 1883 0.0.0.0
    

    Restart the broker:

    sudo systemctl restart mosquitto
    
  • Firewall Port: Open port 1883 on your Pi to allow incoming MQTT traffic:

    sudo ufw allow 1883/tcp
    
  • Test with a Publisher: Use mosquitto_pub from another device (or the Pi itself) to send a test message:

    mosquitto_pub -h <your-pi-ip> -t "rpi/gpio" -m "TestMessage" -r
    

    If the Pi’s mosquitto_sub picks this up, the broker is functioning correctly, and the problem lies with your Android app’s implementation.

内容的提问来源于stack exchange,提问作者tham Tantisunthorn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:42:44