From 48f1c89583ee1adc42627be555c02ca75a8c608f Mon Sep 17 00:00:00 2001 From: CDOrtona Date: Tue, 11 May 2021 02:44:20 +0200 Subject: [PATCH] MQTT minor changes --- .../java/org/cdortona/tesi/MqttService.java | 44 +++++++++++++++---- .../java/org/cdortona/tesi/SensorsInfo.java | 16 +++---- .../org/cdortona/tesi/StaticResources.java | 2 + 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/cdortona/tesi/MqttService.java b/app/src/main/java/org/cdortona/tesi/MqttService.java index 57cc733..23d224f 100644 --- a/app/src/main/java/org/cdortona/tesi/MqttService.java +++ b/app/src/main/java/org/cdortona/tesi/MqttService.java @@ -15,11 +15,13 @@ import org.eclipse.paho.client.mqttv3.IMqttActionListener; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.IMqttToken; +import org.eclipse.paho.client.mqttv3.MqttAsyncClient; import org.eclipse.paho.client.mqttv3.MqttCallback; import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttConnectOptions; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.internal.Token; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; import java.nio.charset.StandardCharsets; @@ -27,8 +29,11 @@ public class MqttService extends Service { String TAG = "MqttService"; - MqttClient client; + MqttAsyncClient client; private final String serverUri = "tcp://192.168.1.45:1883"; + private final String user = "cPanel"; + private final String pwd = "test"; + private MemoryPersistence persistance; String temp; String heartBeat; @@ -63,10 +68,17 @@ public void onCreate() { try { MqttConnectOptions mqttConnectOptions = new MqttConnectOptions(); mqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1); - mqttConnectOptions.setCleanSession(false); - //mqttConnectOptions.setUserName("test"); - //mqttConnectOptions.setPassword("test".toCharArray()); - client = new MqttClient(serverUri,clientId, null); + mqttConnectOptions.setCleanSession(true); + mqttConnectOptions.setAutomaticReconnect(true); + mqttConnectOptions.setUserName(user); + mqttConnectOptions.setPassword(pwd.toCharArray()); + client = new MqttAsyncClient(serverUri, clientId, persistance); + client.connect(mqttConnectOptions); + try { + Thread.sleep(5000); + } catch (Exception e){ + e.printStackTrace(); + } client.setCallback(new MqttCallback() { @Override public void connectionLost(Throwable cause) { @@ -95,8 +107,23 @@ public void deliveryComplete(IMqttDeliveryToken token) { @Override public int onStartCommand(Intent intent, int flags, int startId) { - if(temp != null) - pub(StaticResources.TEMP_TOPIC, temp); + boolean sos_on = intent.getBooleanExtra(StaticResources.EXTRA_SOS_FLAG, false); + String position = intent.getStringExtra(StaticResources.EXTRA_LOCATION); + //this is checking if the user has fired the sos + if(sos_on){ + pub(StaticResources.SOS_TOPIC, "ON", StaticResources.QOS_2); + } else { + if(temp != null) + pub(StaticResources.TEMP_TOPIC, temp, StaticResources.QOS_0); + if(humidity != null) + pub(StaticResources.HUMIDITY_TOPIC, humidity, StaticResources.QOS_0); + if(pressure != null) + pub(StaticResources.PRESSURE_TOPIC, pressure, StaticResources.QOS_0); + if(altitude != null) + pub(StaticResources.ALTITUDE_TOPIC, altitude, StaticResources.QOS_0); + if(position != null) + pub(StaticResources.GPS_TOPIC, position, StaticResources.QOS_0); + } return super.onStartCommand(intent, flags, startId); } @@ -132,11 +159,12 @@ public void onReceive(Context context, Intent intent) { }; //this handles the publishing of the messages - void pub(String topic, String payload){ + void pub(String topic, String payload, int QoS){ byte[] encodedPayload; try { encodedPayload = payload.getBytes(StandardCharsets.UTF_8); MqttMessage message = new MqttMessage(encodedPayload); + message.setQos(QoS); client.publish(topic, message); } catch (MqttException e) { e.printStackTrace(); diff --git a/app/src/main/java/org/cdortona/tesi/SensorsInfo.java b/app/src/main/java/org/cdortona/tesi/SensorsInfo.java index 363b877..3966874 100644 --- a/app/src/main/java/org/cdortona/tesi/SensorsInfo.java +++ b/app/src/main/java/org/cdortona/tesi/SensorsInfo.java @@ -10,6 +10,7 @@ import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.Service; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; @@ -117,7 +118,7 @@ public class SensorsInfo extends AppCompatActivity implements SensorEventListene String temp; String heartBeat; String humidity; - String locations; + String position; String pressure; String altitude; Boolean sos; @@ -229,6 +230,9 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { case (R.id.action_emergency): if(emergencyBoolean){ + mqttService = new Intent(this, MqttService.class); + mqttService.putExtra(StaticResources.EXTRA_SOS_FLAG, true); + startService(mqttService); //I have to make sure the user agrees with the permissions at run-time if(phoneCallPermissions()) { Intent callIntent = new Intent(Intent.ACTION_CALL); @@ -262,6 +266,7 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { case (R.id.action_mqtt): Toast.makeText(this, "Sending data to remote clients", Toast.LENGTH_SHORT).show(); mqttService = new Intent(this, MqttService.class); + mqttService.putExtra(StaticResources.EXTRA_LOCATION, position); try { startService(mqttService); } catch (IllegalStateException | SecurityException e){ @@ -402,27 +407,22 @@ public void onReceive(Context context, Intent intent) { case StaticResources.ESP32_TEMP_CHARACTERISTIC: temp = intent.getStringExtra(StaticResources.EXTRA_TEMP_VALUE); tempValue.setText(temp); - //sensorModel.setTemp(intent.getByteArrayExtra(StaticResources.EXTRA_TEMP_BYTE_VALUE)); break; case StaticResources.ESP32_HEARTH_CHARACTERISTIC: heartBeat = intent.getStringExtra(StaticResources.EXTRA_HEART_VALUE); heartValue.setText(heartBeat); - //sensorModel.setHeart(intent.getByteArrayExtra(StaticResources.EXTRA_HEART_BYTE_VALUE)); break; case StaticResources.ESP32_HUMIDITY_CHARACTERISTIC: humidity = intent.getStringExtra(StaticResources.EXTRA_HUMIDITY_VALUE); humidityValue.setText(humidity); - //sensorModel.setHumidity(intent.getByteArrayExtra(StaticResources.EXTRA_HUMIDITY_BYTE_VALUE)); break; case StaticResources.ESP32_PRESSURE_CHARACTERISTIC: pressure = intent.getStringExtra(StaticResources.EXTRA_PRESSURE_VALUE); pressureValue.setText(pressure); - //sensorModel.setPressure(intent.getByteArrayExtra(StaticResources.EXTRA_PRESSURE_BYTE_VALUE)); break; case StaticResources.ESP32_ALTITUDE_CHARACTERISTIC: altitude = intent.getStringExtra(StaticResources.EXTRA_ALTITUDE_VALUE); altitudeValue.setText(altitude); - //sensorModel.setAltitude(intent.getByteArrayExtra(StaticResources.EXTRA_ALTITUDE_BYTE_VALUE)); break; } } @@ -490,8 +490,8 @@ private void accessLocation(){ @Override public void onSuccess(Location location) { //location is null if there is no known location found - String position = "Lo: " + Math.round(location.getLongitude() * 100d) / 100d + '\n' + - "La: " + Math.round(location.getLatitude() * 100d) / 100d; + position = Math.round(location.getLongitude() * 100d) / 100d + "," + + + Math.round(location.getLatitude() * 100d) / 100d; gpsValue.setText(position); } }); diff --git a/app/src/main/java/org/cdortona/tesi/StaticResources.java b/app/src/main/java/org/cdortona/tesi/StaticResources.java index 611a4a7..efaeda9 100644 --- a/app/src/main/java/org/cdortona/tesi/StaticResources.java +++ b/app/src/main/java/org/cdortona/tesi/StaticResources.java @@ -58,6 +58,8 @@ final class StaticResources { static final String HEART_TOPIC = ESP32_ADDRESS + "/heart"; static final String SOS_TOPIC = ESP32_ADDRESS + "/sos"; static final String GPS_TOPIC = ESP32_ADDRESS + "/gps"; + static final String EXTRA_SOS_FLAG = PACKAGE_NAME + "sos_flag"; + static final String EXTRA_LOCATION = PACKAGE_NAME + "location_mqtt"; static final int QOS_0 = 0; static final int QOS_1 = 1; static final int QOS_2 = 2;