1

I am sending sensor data from Bosch XDK sensor using MQTT protocol to an Android App. Everything works fine if the sampling frequency is below 100Hz. However, When I increase this frequency, the APP hangs and becomes unresponsive after a few seconds. I believe this could be a Code optimization problem for the App.

This is the function to handle incoming

This is the function to handle incoming MQTT message:

public void updateSensorValues(String mqttMessage){

    try{
        JSONArray jsonArray = new JSONArray(mqttMessage);
        for(int i = 0;i<jsonArray.length();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            jsonAccX.put(jsonObject.get("bma280_x"));
            jsonAccY.put(jsonObject.get("bma280_y"));
            jsonAccZ.put(jsonObject.get("bma280_z"));
            jsonGyrX.put(jsonObject.get("bmg160_x"));
            jsonGyrY.put(jsonObject.get("bmg160_y"));
            jsonGyrZ.put(jsonObject.get("bmg160_z"));
            jsonMagX.put(jsonObject.get("bmm150_x"));
            jsonMagY.put(jsonObject.get("bmm150_y"));
            jsonMagZ.put(jsonObject.get("bmm150_z"));
        }




    } catch (JSONException e){
        System.out.println("Error " + e.getMessage());

    }
}

Could anyone guide me if this an optimization problem in Java or I should receive data less frequently?

It is also important to mention that the XDK sensor only allows to transmit 997 characters at a time, therefore, I have to publish the data faster.

Salman Shaukat
  • 333
  • 3
  • 14
  • 1
    can you avoid creating and modifying JSON objects/arrays? If not, use faster JSON parser like https://jsoniter.com/ – diginoise Sep 30 '19 at 12:23
  • 1
    Would you explain how the messages are received/sent? Are you leveraging Web Sockets? Are you leveraging the Eclipse Paho library? – Apostolos Emmanouilidis Sep 30 '19 at 12:31
  • @ApostolosEmmanouilidis Yes, I am using the Eclipse Paho library to receive the messages. – Salman Shaukat Sep 30 '19 at 12:35
  • 1
    @SalmanShaukat through Web Sockets? I mean, do you have a bi-directional TCP like open connection and push/pull data or you're opening connection each time? – Apostolos Emmanouilidis Sep 30 '19 at 12:35
  • @ApostolosEmmanouilidis Hi I have a bi-directional TCP connection and connection is done only a single time. – Salman Shaukat Sep 30 '19 at 12:41
  • 1
    do you really need to store the data seperate in multiple Lists? You could store all data together and query the data from complete dataset when you're analysing. – Starbax Sep 30 '19 at 15:21
  • @SalmanShaukat I think your mqtt code is working in the Main Thread. please refer to the my answer here : https://stackoverflow.com/questions/58952157/mqtt-send-message-from-main-thread/58990338#58990338 – Ghasem Sadeghi Nov 22 '19 at 08:48

0 Answers0