Announcement

Collapse
No announcement yet.

JSON HTTP GET

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    JSON HTTP GET

    Hi
    I am unsure if this is the correct place to post this and if it isn't I am sorry, I am trying to do a HTTP Get using the JSON api to turn a device on or off. At this moment I am not concerned about the returning HTML.

    If I copy this into my localhost url browser

    Code:
    [URL]http://192.168.0.60:8080/JSON?request=controldevicebylabel&ref=347&label=off[/URL]
    It Works great,


    However using an Arduino like this I have an issue

    Code:
     int ret = client.connect(server, 8080) ;
         if (ret==1)
         {
        client.println("GET /JSON?request=controldevicebylabel&ref=347&label=off");
        client.println("Host: homeseer");
        client.println("Connection: close");
        client.println();
        }
    I get nothing, using Wireshark to watch the network I can see the source ip and the destination http request ip are both correct.

    The request looks correct to me and I have compared to the SDK documentation. Is their a security setting in homeseer that stops non localhost traffic? I doubt it but I am scratching my head a little

    many thanks

    #2
    You will need to include a username and password if you are remote to HS. Try using this url
    Code:
    https://connected2.homeseer.com/JSON?request=controldevicebylabel&ref=347&label=off&user=validHS3User&pass=validPassword
    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

    Comment


      #3
      Thanks, I guess you don't need to specify username password on localhost.
      Also I noticed that's a https header, it works ok for http I assume?

      Arduino and https would be something totally brand new, brain not ready for that as well.

      thanks!

      Comment


        #4
        Before you frustrate yourself senseless I have been in exactly this position (if you do a search when the JSON interface was first released I tried to do exactly this what you are doing and posted about it). You are likely doing nothing wrong.

        From what I can see the HS3 web server (which is custom) is expecting some characters which the Arduino is not providing, I didn't have the heart to start picking the characters apart in wireshark for a known good command and comparing it to what the arduino is sending. I suspect it is something relatively simple, whether it is is an end line/line feed or something that an apache/IIS server may be less forgiving over whereas the HS3 server is expecting it.

        Comment


          #5
          Originally posted by techboy View Post
          Thanks, I guess you don't need to specify username password on localhost.
          Also I noticed that's a https header, it works ok for http I assume?

          Arduino and https would be something totally brand new, brain not ready for that as well.

          thanks!
          Try this. Not sure why it works, but I never could get it to work using println

          client.print("GET " + url + " HTTP/1.1\r\n" +
          "Host: " + server + "\r\n" +
          "Connection: close\r\n\r\n");


          Also, the Arduino and MQTT plugins give you access to HS3 as well without using JSON. MQTT is a bit more involved but works great if you expect to have more IOT type devices.

          Z

          Comment


            #6
            Http GET fun

            Well I included the username and password as rupp suggested and this worked perfect on another pc.

            But no joy with the Arduino as Mr Happy said. Thank you both for the feedback.

            I did use wireshark at the same time, this is something completely new to me.

            I have taken a screen shot of the pc wireshark capture, there are 3 lines beginning with accept and one with user-agent.

            These are not included with the Arduino output, I am wondering am I ok to just add these lines as part of my GET routine?

            I was also thinking that the println in the Arduino might be an issue but wireshark shows \r\n at the end of lines.


            In the SDK it shows a POST example, would this be better or would a similar issue be encountered?

            Varsc, I will check out the Arduino/MQTT plugins. Ultimately I am wanting to retire an old Ocelot box.

            Does a webserver log exist I can check? the HS3 log isn't showing anything.

            Thank you all again.
            Attached Files

            Comment


              #7
              Here is a working code snippet

              Code:
              const char *hs3host = "192.168.1.103";
              const int jsonPort = 80;
              
              .....
              
                      Serial.print("connecting to ");
                      Serial.print(hs3host);
                      Serial.print(":");
                      Serial.println(jsonPort);
                  
                      // Use WiFiClient class to create TCP connections
                      WiFiClient client;
                      if (!client.connect(hs3host, jsonPort)) {
                          Serial.println("connection failed");
                          return;
                      }
                  
                      // create URI for the request
                      String url = "/JSON?request=";
                      url += "runevent";
                      url += "&group=";
                      url += URIEncode(eventGroup);
                      url += "&name=";
                      url += URIEncode(room);
                      url += URIEncode(" ");
                      url += URIEncode(eventDevice);
                      url += URIEncode(" ");
                      url += value ? "On" : "Off";
                  
                      Serial.print("Requesting URL: ");
                      Serial.println(url);
                  
                      // This will send the request to the server
                      client.print("GET " + url + " HTTP/1.1\r\n" +
                                   "Host: " + hs3host + "\r\n" +
                                   "Connection: close\r\n\r\n");
                      unsigned long timeout = millis();
                      while (client.available() == 0) {
                          yield();
                          if (millis() - timeout > 5000) {
                              Serial.println(">>> Response Timeout !");
                              client.stop();
                              return;
                          }
                      }
                  
                      // Read all the lines of the reply from server and print them to Serial
                      while(client.available()) {
                          String line = client.readStringUntil('\r');
                          Serial.print(line);
                      }
                  
                      Serial.println();
                      Serial.println("closing connection");
                      Serial.println();
                  }

              Comment


                #8
                http get

                Thank you Zwolfpack got it working copied how you structured the get statement and changed println to print.

                thank you happy, can progress now

                Comment

                Working...
                X