Uploaded image for project: 'Help-Desk'
  1. Help-Desk
  2. HELP-15411

[fiware-stackoverflow] How to send commands to iot-sensors using android application

    Details

      Description

      Created question in FIWARE Q/A platform on 01-02-2019 at 18:02
      Please, ANSWER this question AT https://stackoverflow.com/questions/54484127/how-to-send-commands-to-iot-sensors-using-android-application

      Question:
      How to send commands to iot-sensors using android application

      Description:
      I setup an the fiware201:iot-sensor inside an Ubuntu 16.04 instance on FIWARE-lab , and am currently building an android application to be able to send commands to access the dummy devices provided by the context provider , i setup a class that formats the data and sends it to the endpoint provided by the tutorial , but i cant seem to get it to work , i get a 422 error whenever i try to send any command.

      additional details:
      i am using volley -> a package to send post,get requests in android
      fiware-lab region : crete

      here is the code:

      import android.content.Context;
      import android.util.Log;

      import com.android.volley.AuthFailureError;
      import com.android.volley.Request;
      import com.android.volley.RequestQueue;
      import com.android.volley.Response;
      import com.android.volley.VolleyError;
      import com.android.volley.toolbox.StringRequest;
      import com.android.volley.toolbox.Volley;

      import java.util.HashMap;
      import java.util.Locale;
      import java.util.Map;

      public class ApiController {
      private String urlString
      public String serverResponse;
      private RequestQueue queue ;

      public ApiController(Context context)

      { //set context variables if required serverResponse = ""; queue = Volley.newRequestQueue(context); }

      public void setDevice(String device)

      { this.device = device; urlString = "http://myFloatingIp:3001/iot/" +"Lamp001"; // URL to call }

      public void send() {
      String uri = String.format(Locale.US, urlString);

      // Request a string response from the provided URL.
      StringRequest stringRequest = new StringRequest(Request.Method.POST,uri,
      new Response.Listener<String>() {
      @Override
      public void onResponse(String response)

      { serverResponse = response; }

      }, new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error)

      { serverResponse = "Error"; }

      }
      ) {
      @Override
      protected Map<String, String> getParams() throws AuthFailureError

      { Map<String,String> params=new HashMap<String,String>(); params.put("data","urn:ngsi-ld:Lamp:001"@On"); return params; }

      @Override
      public Map<String, String> getHeaders() throws AuthFailureError

      { Map<String,String> headers=new HashMap<String,String>(); headers.put("Content-Type","text/plain"); return headers; }

      };

      // Add the request to the RequestQueue.
      queue.add(stringRequest);
      }

      }

        Activity

        fla Fernando Lopez made changes -
        Fix Version/s 2021 [ 12600 ]
        fla Fernando Lopez made changes -
        Resolution Done [ 10000 ]
        Status Answered [ 10104 ] Closed [ 6 ]
        fla Fernando Lopez made changes -
        HD-Enabler IDAS [ 10884 ]
        Description
        Created question in FIWARE Q/A platform on 01-02-2019 at 18:02
        {color: red}Please, ANSWER this question AT{color} https://stackoverflow.com/questions/54484127/how-to-send-commands-to-iot-sensors-using-android-application


        +Question:+
        How to send commands to iot-sensors using android application

        +Description:+
        I setup an the fiware201:iot-sensor inside an Ubuntu 16.04 instance on FIWARE-lab , and am currently building an android application to be able to send commands to access the dummy devices provided by the context provider , i setup a class that formats the data and sends it to the endpoint provided by the tutorial , but i cant seem to get it to work , i get a 422 error whenever i try to send any command.

        additional details:
        i am using volley -> a package to send post,get requests in android
        fiware-lab region : crete

        here is the code:



        import android.content.Context;
        import android.util.Log;

        import com.android.volley.AuthFailureError;
        import com.android.volley.Request;
        import com.android.volley.RequestQueue;
        import com.android.volley.Response;
        import com.android.volley.VolleyError;
        import com.android.volley.toolbox.StringRequest;
        import com.android.volley.toolbox.Volley;

        import java.util.HashMap;
        import java.util.Locale;
        import java.util.Map;


        public class ApiController {
            private String urlString
            public String serverResponse;
            private RequestQueue queue ;

            public ApiController(Context context) {
                //set context variables if required

                serverResponse = "";
                queue = Volley.newRequestQueue(context);


            }

            public void setDevice(String device) {
                this.device = device;
                urlString = "http://myFloatingIp:3001/iot/" +"Lamp001"; // URL to call

        }


            public void send() {
                String uri = String.format(Locale.US, urlString);

                // Request a string response from the provided URL.
                StringRequest stringRequest = new StringRequest(Request.Method.POST,uri,
                        new Response.Listener&lt;String&gt;() {
                            @Override
                            public void onResponse(String response) {
                                       serverResponse = response;
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                      serverResponse = "Error";
                    }
                }
                              ) {
                    @Override
                    protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
                        Map&lt;String,String&gt; params=new HashMap&lt;String,String&gt;();
                        params.put("data","urn:ngsi-ld:Lamp:001"@On");
                        return params;

                    }

                    @Override
                    public Map&lt;String, String&gt; getHeaders() throws AuthFailureError {
                        Map&lt;String,String&gt; headers=new HashMap&lt;String,String&gt;();
                        headers.put("Content-Type","text/plain");
                        return headers;
                    }

                };

        // Add the request to the RequestQueue.
                queue.add(stringRequest);
            }


        }

        Created question in FIWARE Q/A platform on 01-02-2019 at 18:02
        {color: red}Please, ANSWER this question AT{color} https://stackoverflow.com/questions/54484127/how-to-send-commands-to-iot-sensors-using-android-application


        +Question:+
        How to send commands to iot-sensors using android application

        +Description:+
        I setup an the fiware201:iot-sensor inside an Ubuntu 16.04 instance on FIWARE-lab , and am currently building an android application to be able to send commands to access the dummy devices provided by the context provider , i setup a class that formats the data and sends it to the endpoint provided by the tutorial , but i cant seem to get it to work , i get a 422 error whenever i try to send any command.

        additional details:
        i am using volley -> a package to send post,get requests in android
        fiware-lab region : crete

        here is the code:



        import android.content.Context;
        import android.util.Log;

        import com.android.volley.AuthFailureError;
        import com.android.volley.Request;
        import com.android.volley.RequestQueue;
        import com.android.volley.Response;
        import com.android.volley.VolleyError;
        import com.android.volley.toolbox.StringRequest;
        import com.android.volley.toolbox.Volley;

        import java.util.HashMap;
        import java.util.Locale;
        import java.util.Map;


        public class ApiController {
            private String urlString
            public String serverResponse;
            private RequestQueue queue ;

            public ApiController(Context context) {
                //set context variables if required

                serverResponse = "";
                queue = Volley.newRequestQueue(context);


            }

            public void setDevice(String device) {
                this.device = device;
                urlString = "http://myFloatingIp:3001/iot/" +"Lamp001"; // URL to call

        }


            public void send() {
                String uri = String.format(Locale.US, urlString);

                // Request a string response from the provided URL.
                StringRequest stringRequest = new StringRequest(Request.Method.POST,uri,
                        new Response.Listener&lt;String&gt;() {
                            @Override
                            public void onResponse(String response) {
                                       serverResponse = response;
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                      serverResponse = "Error";
                    }
                }
                              ) {
                    @Override
                    protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
                        Map&lt;String,String&gt; params=new HashMap&lt;String,String&gt;();
                        params.put("data","urn:ngsi-ld:Lamp:001"@On");
                        return params;

                    }

                    @Override
                    public Map&lt;String, String&gt; getHeaders() throws AuthFailureError {
                        Map&lt;String,String&gt; headers=new HashMap&lt;String,String&gt;();
                        headers.put("Content-Type","text/plain");
                        return headers;
                    }

                };

        // Add the request to the RequestQueue.
                queue.add(stringRequest);
            }


        }

        fla Fernando Lopez made changes -
        Assignee Jason Fox [ jason.fox ]
        backlogmanager Backlog Manager made changes -
        Status In Progress [ 3 ] Answered [ 10104 ]
        backlogmanager Backlog Manager made changes -
        Status Open [ 1 ] In Progress [ 3 ]
        backlogmanager Backlog Manager made changes -
        Field Original Value New Value
        Component/s FIWARE-TECH-HELP [ 10278 ]
        backlogmanager Backlog Manager created issue -

          People

          • Assignee:
            jason.fox Jason Fox
            Reporter:
            backlogmanager Backlog Manager
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: