Android通过MQTT向Raspberry Pi发布GPIO控制消息无响应问题排查求助
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(notRPI/GPIOor any variation). Test with this command on the Pi:mosquitto_sub -h localhost -t "rpi/gpio" -vToggle 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 yourmosquitto.conffile and uncomment/set:listener 1883 0.0.0.0Restart the broker:
sudo systemctl restart mosquittoFirewall Port: Open port 1883 on your Pi to allow incoming MQTT traffic:
sudo ufw allow 1883/tcpTest with a Publisher: Use
mosquitto_pubfrom another device (or the Pi itself) to send a test message:mosquitto_pub -h <your-pi-ip> -t "rpi/gpio" -m "TestMessage" -rIf the Pi’s
mosquitto_subpicks this up, the broker is functioning correctly, and the problem lies with your Android app’s implementation.
内容的提问来源于stack exchange,提问作者tham Tantisunthorn

