Skip to content

Commit

Permalink
MQTT minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CDOrtona committed May 11, 2021
1 parent 16914e1 commit 48f1c89
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
44 changes: 36 additions & 8 deletions app/src/main/java/org/cdortona/tesi/MqttService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@
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;

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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/org/cdortona/tesi/SensorsInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
});
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/cdortona/tesi/StaticResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 48f1c89

Please sign in to comment.