AutoRemote TCP or REST protocol for receiving commands

Discussion in 'AutoApps' started by kolban, Oct 21, 2015.

  1. kolban

    kolban New Member

    Joined:
    Oct 21, 2015
    Messages:
    5
    Likes Received:
    0
    I wish to submit network commands to my Android via AutoRemote. I wish to submit these from a hardware device (an ESP8266). This means that my device will form a TCP/IP sockets connection and then transmit data over the socket. This could be a REST request or it could be a socket stream. The ESP8266 is itself a WiFi access point allowing an Android phone to connect as a WiFi station ... meaning that there will be no Internet access. In effect, simply a WiFi LAN with two members ... the ESP8266 and the Android device. The ESP8266 will be running a C program that wishes to submit the command to AutoRemote.

    With this background, here are my questions (I have just discovered AutoRemote and spent an hour reading ... but couldn't find what I was needing):

    1. Can AutoRemote function without an Internet connection but only a LAN connection?
    2. Does AutoRemote listen on the Android's local IP address when it is WiFi connected?
    3. What is the port number that AutoRemote is listening upon?
    4. Is the protocol or format for sending commands exposed/documented?
    5. Has there been any work already done on writing code level libraries that can be incorporated in applications to trigger AutoRemote?
    6. Anything else that you might be able to think of that I might need to know in order to write a native C application that connects point-to-point over a TCP link to AutoRemote?

    Many thanks in advance. If and when I get this working, I'll be delighted to write up for others to follow or build upon in the future.

    Neil
     
  2. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,479
    Likes Received:
    806
    Hi! Thanks for your questions
    1. It can. You just need to enable the AutoRemote Wifi service on the receiving device in Tasker
    2. Yes it does after you turn on the service
    3. 1817
    4. It's not documented (sorry) but it's as simple as accessing a URL like http://192.168.1.77:1817/sendmessage?message=1
    5. Yes, here's a C# example: https://github.com/AutoApps/AutoRemote/tree/master/Plugin/C# You can add your own custom device to AutoRemote on your Android device and send and receive messages from it
    6. Not sure. Let me know if you have any trouble :)
    Would be great if you could get it working and share your results :D
    Thanks
     
    kolban likes this.
  3. kolban

    kolban New Member

    Joined:
    Oct 21, 2015
    Messages:
    5
    Likes Received:
    0
    Thank you sir. This should be enough to get me started. If I have problems, I'll be sure and post back with detailed questions and, when done, there will most certainly be a report on what was attempted, results and lessons learned.
     
  4. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,479
    Likes Received:
    806
    Thank you very much!
     
  5. kolban

    kolban New Member

    Joined:
    Oct 21, 2015
    Messages:
    5
    Likes Received:
    0
    I got around to sending a request to autoRemote from my ESP8266 hardware. The message I sent was:

    http://192.168.1.8:1817/sendmessage?message=1

    and I see Tasker wake up just fine. However, what I am finding is that response data coming back from the REST call is:


    {"communication_base_params":{"type":"ResponseBasic","fallback":false},"sender":"<data>","result":"OK"}HTTP/1.1 500 Internal Server Error
    Content-Type: text/plain
    Date: Wed, 28 Oct 2015 04:56:52 GMT
    Connection: keep-alive
    Content-Length: 40
    SERVER INTERNAL ERROR: IOException: null

    Basically ... I am getting a JSON response immediately followed by what appears to be some kind of exception message. If I were to guess, my C code is sending a REST (HTTP) request to autoRemote and then closing its end of the connection. I am then guessing that autoRemote wasn't expecting that (keep-alive?) and throws this exception which is then somehow making its way back down the socket.

    I could be all woolly on that idea ... any idea how I can disable the unexpected data at the end?
     
  6. joaomgcd

    joaomgcd Administrator Staff Member

    Joined:
    Feb 3, 2015
    Messages:
    9,479
    Likes Received:
    806
    What happens when you open that URL on your PC? Does the error also show up?
     
  7. kolban

    kolban New Member

    Joined:
    Oct 21, 2015
    Messages:
    5
    Likes Received:
    0
    When I open the URL from a browser, I don't see the error request in the browser. However, from the REST client here is the low level trace I see:

    From my ESP8266 ... this is the data sent:

    GET /sendmessage?message=1 HTTP/1.0
    User-Agent: Espruino 1v81.661
    Connection: close
    Host: 192.168.1.8:1817

    The response back from my Android phone is:

    HTTP/1.1 200 OK
    Content-Type: text/html
    Date: Wed, 28 Oct 2015 22:27:46 GMT
    Connection: keep-alive
    Content-Length: 237

    {"communication_base_params":{"type":"ResponseBasic","fallback":false},"sender":"<data>","result":"OK"}HTTP/1.1 500 Internal Server Error
    Content-Type: text/plain
    Date: Wed, 28 Oct 2015 22:27:51 GMT
    Connection: keep-alive
    Content-Length: 40

    SERVER INTERNAL ERROR: IOException: null


    However what this trace doesn't show is timing ...

    The ESP8266 sends the REST request ... the response from AutoRemote arrives back (up to the end of the JSON data) ... the ESP8266 then waits a few seconds and realizes that no further data is coming and closes its end of the connection. At THAT point AutoRemote sends the internal error data.

    What does AutoRemote do internally when it detects that the partner (ESP8266 in my case) closes its end of the socket connection? Notice also that the ESP8266 explicitly sent a "Connection: close" header which should have resulted in AutoRemote honoring that request and closing the connection at its end after sending the HTTP response (the JSON data) .. instead AutoRemote returned a "Connection: keep-alive" ... is that hard coded?
     
  8. dakipro

    dakipro New Member

    Joined:
    Jul 25, 2016
    Messages:
    2
    Likes Received:
    0
    any news on this one?
    I need it for a home automation project, I have a network of devices using the AutoApp, but I need to "ping" it via the REST protocol. Do you maybe know a workaround, or some other plugin that integrates with Tasker that will allow network device to send message to the Tasker running on mobil?
    Thanks!
     
  9. Tooandrew

    Tooandrew New Member

    Joined:
    Dec 11, 2016
    Messages:
    2
    Likes Received:
    0
    im also interested to know if you managed to get this working. I would add that if you have openwrt or any other open source router firmware with the ability to execute scripts (or really any linux machine at all, including raspberry pi), you add the linux machine as an autoremote device, and use tasker and autoremote to send the command to make it execute a script to ping your devices - @dakipro , that could be the workaround you are looking for. for example, i have setup a cron job that pings the phones of everyone in the household, and detects whether they responded or not, which runs every minute. if any phone stops or starts responding, a autoremote message telling me who is home is sent to my phone. otherwise nothing happens. If i wanted to, i could execute those scripts from within tasker via autoremote, though.

    EDIT: i found working code

    Code (Text):

    #include <ESP8266WiFi.h>
    #include <ESP8266WiFiAP.h>
    #include <ESP8266WiFiGeneric.h>
    #include <ESP8266WiFiMulti.h>
    #include <ESP8266WiFiScan.h>
    #include <ESP8266WiFiSTA.h>
    #include <ESP8266WiFiType.h>
    #include <WiFiClient.h>
    #include <WiFiClientSecure.h>
    #include <WiFiServer.h>
    #include <WiFiUdp.h>
    #include <ESP8266HTTPClient.h>

    #include <ESP8266WiFi.h>
    const char* host = "autoremotejoaomgcd.appspot.com";
    const char* url = "/sendmessage?key=<your key here>&message=LA";
    const char* ssid     = "ssid";
    const char* password = "pass";

    void setup() {
      // put your setup code here, to run once:
    delay(20);
    Serial.begin(115200);
      delay(10);

      // We start by connecting to a WiFi network

      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
         would try to act as both a client and an access-point and could cause
         network-issues with your other WiFi-devices on your WiFi-network. */
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());


    }

    int value = 0;

    void loop() {
        WiFiClient client;
      const int httpPort = 80;
      client.connect(host, httpPort);

      //// Get URL to send message ////
      //// Times out after f 5 seconds ////
      client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" +
                   "Connection: close\r\n\r\n");
      unsigned long timeout = millis();
      while (client.available() == 0) {
        if (millis() - timeout > 5000) {
          client.stop();
          return;
    }
    }
    }
     
     
    Last edited: Dec 16, 2016

Share This Page