I am experimenting with Android Things which is still in preview mode.
Facing lot of challenges with communicating with wifi as well as local network via code.
One of such occasion is making Android Thing on RPi3 as MQTT broker.
I am not sure if the issue is with the MQTT code or with Android Things.
Can anyone help me identify the issue here?
Here is my code:
package com.example.androidthings.myproject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.PeripheralManagerService;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.io.IOException;
public class MainActivity extends Activity implements MqttCallback{
private static final String TAG = MainActivity.class.getSimpleName();
public static final String MOTOR_A_PIN_1 = "BCM21"; //physical pin #40
public static final String MOTOR_A_PIN_2 = "BCM20"; //physical pin #38
public static final String MOTOR_B_PIN_1 = "BCM24"; //physical pin #18
public static final String MOTOR_B_PIN_2 = "BCM23"; //physical pin #16
private Gpio motorAPin1;
private Gpio motorAPin2;
private Gpio motorBPin1;
private Gpio motorBPin2;
private MqttClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate.....Motor MQTT");
try {
MemoryPersistence persistance = new MemoryPersistence();
client = new MqttClient("tcp://127.0.0.1:1883", "AndroidThingBroker", persistance);
client.connect();
String topic = "topic/androidthings";
int qos = 1;
client.subscribe(topic, qos);
} catch (MqttException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
PeripheralManagerService service = new PeripheralManagerService();
try {
// Create GPIO connection for L293D (Motor will run through L293D).
motorAPin1 = service.openGpio(MOTOR_A_PIN_1);
// Configure as an output.
motorAPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorAPin2 = service.openGpio(MOTOR_A_PIN_2);
// Configure as an output.
motorAPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorBPin1 = service.openGpio(MOTOR_B_PIN_1);
// Configure as an output.
motorBPin1.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
// Create GPIO connection for L293D (Motor will run through L293D).
motorBPin2 = service.openGpio(MOTOR_B_PIN_2);
// Configure as an output.
motorBPin2.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if (motorAPin1 != null) {
try {
motorAPin1.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorAPin2 != null) {
try {
motorAPin2.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorBPin1 != null) {
try {
motorBPin1.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
if (motorBPin2 != null) {
try {
motorBPin2.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
}
@Override
public void connectionLost(Throwable cause) {
Log.d(TAG, "connectionLost....");
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
Log.d(TAG, payload);
// do stuff
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "deliveryComplete....");
}
}
Error:
12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err:
Unable to connect to server (32103) - java.net.ConnectException: Connection refused
12-27 15:03:58.898 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.lang.Thread.run(Thread.java:761)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: Caused by: java.net.ConnectException: Connection refused
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
12-27 15:03:58.899 2245-2245/com.example.androidthings.myproject W/System.err: at java.net.Socket.connect(Socket.java:586)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
12-27 15:03:58.900 2245-2245/com.example.androidthings.myproject W/System.err: ... 2 more
** EDIT **
change ip address to 127.0.0.1 and now receiving different error stack. I have updated the error log above.
My bad... MQTT Broker was down. I thought I could create a broker on the device itself. Then I realized it should be running on a server and the one I started during my initial testing was down.
As I started the broker server using Mosquitto, I am now able to connect to it from Android :D
if
adb shell
thenping
is working it means your Android Things board has internet connection and is most likely not your problem.Looking at the stacktrace
is the code that is throwing the error, to narrow down the problem you could create a simple Android app or even just a java main method on your computer and working out what the route problem is. Perhaps the ip is wrong, etc, but it doesn't appear to be an Android Things issue (or at least get your code working as a java app before hand to minimise the possibilities).
I would also remove this code:
so that you aren't catching exceptions - you want the app to break as soon as possible so you can see the route error, not the side affects of the original error.