java – Trying to Publish message from Android to Raspberry pi utilizing MQTT

I personal raspberry pi and android smartphone. I'm making an attempt to manage raspburry pi GPIO with self-made software by android studio. The units are ready join to one another through MQTT, however once I was making an attempt to publish a message (on/off), it would not confirmed up within the raspberry pi terminal. There isn't any error detected. I've no clue, what I am lacking right here. Thanks prematurely for any suggestion.

public class MainActivity extends AppCompatActivity {

    Swap aswitch;
    MqttAndroidClient shopper;
    TextView subText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tremendous.onCreate(savedInstanceState);
        setContentView(R.structure.activity_main);
        String clientId = MqttClient.generateClientId();
        shopper = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.2.193:1883",clientId);
        //shopper = new MqttAndroidClient(this.getApplicationContext(), "tcp://192.168.43.41:1883",clientId);

    }
    public void Swap(View v){
        aswitch = (Swap) findViewById(R.id.simpleSwitch1);
        aswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true){
                    String subject = "rpi/gpio";
                    String payload = "On";
                    byte[] encodedPayload = new byte[0];
                    strive {
                        encodedPayload = payload.getBytes(StandardCharsets.UTF_8);
                        MqttMessage message = new MqttMessage(encodedPayload);
                        message.setRetained(true);
                        shopper.publish(subject, message);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }else{
                    String subject = "rpi/gpio";
                    String payload = "Off";
                    byte[] encodedPayload = new byte[0];
                    strive {
                        encodedPayload = payload.getBytes(StandardCharsets.UTF_8);
                        MqttMessage message = new MqttMessage(encodedPayload);
                        message.setRetained(true);
                        shopper.publish(subject, message);
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    

    public void conn(View v){

        strive {
            IMqttToken token = shopper.join();
            token.setActionCallback(new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Toast.makeText(MainActivity.this,"linked",Toast.LENGTH_LONG).present();
                    setSubscription();
                }
                non-public void setSubscription() {
                }
                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Toast.makeText(MainActivity.this,"connection failed",Toast.LENGTH_LONG).present();
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
        }

    }


}



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *