Announcement

Collapse
No announcement yet.

Cloud based server PHP script to access Homeseer ?

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

    Cloud based server PHP script to access Homeseer ?

    Is there a way to send and receive Homeseer data from a cloud based PHP script?

    I would like to send data to a device and also be able to read the status of a device from a cloud based server.

    If this is the wrong forum in which to ask about PHP scripts, please let me know.

    Thank you!

    #2
    A PHP curl call (https://www.php.net/manual/en/curl.examples-basic.php) to HS using JSON format should work. You'll want to go through the MYHS interface (https://docs.homeseer.com/display/HSPI/JSON+API)

    Comment


      #3
      Thank you. I'll give this a shot.

      Comment


        #4
        Seems I am close but I get this error:

        "Error generating page: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"

        The Newtonsoft.Json.dll version in the HS3 (Standard) root directory is 13.0.1.

        I don't know what is calling for Version 8.0.0.0 to be loaded.

        Any ideas?

        Comment


          #5
          Look in your Config/settings.ini file and see what it has for ScriptingReferences=
          It should show Newtonsoft.Json
          This is what I have in mine:
          Newtonsoft.Json;C:\Program Files (x86)\HomeSeer HS3\bin\scripting\Newtonsoft.Json.dll

          My complete entry ismay not be the same for ever user)
          ScriptingReferences=System.Web.Script;System.Web.Extensions. dll,Newtonsoft.Json;C:\Program Files (x86)\HomeSeer HS3\bin\scripting\Newtonsoft.Json.dll,PluginSdk;bin\homeseer \PluginSdk.dll

          If it does have an entry for it, check the directory it says it's in and see if it's actually there. If not, you'll need to put it in that directory as well as update the settings.ini file ScriptingReferences setting.

          Some put it in the /Bin directory which is fine as well. It (Newton.json.dll) should not be in the root directory though.

          I had to restart HS3 when I did it way back when, not sure if HS4 is different.

          LMK

          Comment


            #6
            I appreciate the help!!

            My settings.ini file shows the .dll file to be in the same place as yours. The version number of the file in that directory is 12.0.1.22727.

            I can access myHS from my cell phone and from another PC on the LAN. But I can't get to it using PHP. I am using this PHP script:

            PHP Code:
            <?php
            $ch 
            curl_init("https://myhs.homeseer.com?user=[userName]&pass=[password]/");
            $fp fopen("example_homepage.txt""w");
            curl_setopt($chCURLOPT_FILE$fp);
            curl_setopt($chCURLOPT_HEADER0);
            $response curl_exec($ch);
            if(
            curl_error($ch)) {
            fwrite($fpcurl_error($ch));
            }
            echo 
            $response;
            curl_close($ch);
            echo 
            $fp;
            fclose($fp);
            ?&
            gt
            I have also tried it with just "https://myhs.homeseer.com" with no user name or password.

            I am using HS3 Standard. I have the Basic myHS account.

            From PHP, I can use the above script with "www.example.com" and that does return the page.

            Still working on it.

            Thank you

            Comment


              #7
              Should have asked more details initially.
              I'm guessing you want a webpage to display and/or send data from your HS system?

              When you run your PHP code, what error does your webserver give you?

              In case you haven't found it, here are the JSON requests you can make:
              https://homeseer.com/support/homeseer/HS3/A2ZLink/default.htm


              Try this to get started. It should let you start moving data.

              Code:
              <?php
              $user = "MYHSusername";
              $pass = "MYHSpassword";
              
              # JSON examples
              # $url = "https://connectedXX.homeseer.com/JSON?user=USER&pass=PASS&request=";
              # JSON REQUEST
              # /JSON?request=controldevicebyvalue&ref=###&value=#
              
              # Get the Tunnel you're connected to
              $urlConnect = "https://myhs.homeseer.com/getsystems?user=".$user."&pass=".$pass;
              $response = getURL($urlConnect);
              
              # Parse out TunnelIP
              $json = json_decode($response,TRUE);
              $tunnelIP = $json['systems'][0]['tunnelip'];
              #echo "TunnelIP = " . $tunnelIP;
              
              #Build URLcmd you can use for JSON requests
              $urlCmd = 'https://'.$tunnelIP.'/JSON?user='.$user.'&pass='.$pass;
              
              # Request Example
              # Get Status
              $request = "&request=getstatus";
              $url = $urlCmd.$request;
              $response = getURL($url);
              echo $response;
              
              # Set Control Device
              $request = "&request=controldevicebyvalue&ref=###&value=#";
              $url = $urlCmd.$request;
              $response = getURL($url);
              exit;
              
              function getURL($_url) {
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, $_url);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                  curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
                  $response = curl_exec($ch);
                  if(curl_error($ch)) {
                     echo curl_error($ch);
                  }
                  curl_close($ch);
              
                  return ($response);
              }
              ?>

              Comment


                #8
                When I run the PHP script you posted above, the following is returned:

                Error generating page: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

                However, when I run the following PHP script, I can control the state of a device ( !! ):

                PHP Code:
                <?php
                $ch 
                curl_init('http://[WAN IP:port]/JSON?request=controldevicebylabel&ref=14&label=On');
                curl_setopt($chCURLOPT_HEADER0);
                curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
                curl_setopt($chCURLOPT_FOLLOWLOCATIONTRUE);
                curl_setopt($chCURLOPT_MAXREDIRS2);
                $response curl_exec($ch);
                if(
                curl_error($ch)) {
                echo 
                curl_error($ch);
                }
                curl_close($ch);
                echo 
                $response;
                ?>
                All I want to do is to read the value of a virtual device and to change the value of that device. So it seems we're on the home stretch. I'd like to understand why I get the error for the PHP script you just posted. But yet the simpler script works OK. I just need to change the HS statements above. I will refer to the list of JSON requests you posted above.

                It seems using myHS is causing the problem ? Yet, myHS shows to be connected in my log and I can use it from iOS apps.

                Again, thank you for your guidance with this.

                Comment


                  #9
                  Couple things, last, first
                  You allow direct WAN access to your HS system? (eg WANort)? Just for testing hopefully..
                  Not sure why you would get a Newtonsoft error, from a remote JSON call. This looks like an error you would get from running a script locally (not sure how the JSON call are implemented in HS)

                  Again, what are you actually trying to do and how are you implementing it?
                  Do you pull HS data for the Cloud using curl JSON calls via MYHS?
                  Send data to HS from the Cloud/Web PHP via MYHS JSON calls ?

                  Also:
                  Where are you executing the this test PHP code from? Cloud?
                  How are you executing that PHP code (via web query, CLI?)
                  Is the error coming from the PHP call (eg echo $error_code) or elsewhere (Isn't a PHP/webserver error, so I'm guessing a response from the Curl call)

                  Try putting in some more echo statements, particularly for the $url and $urlCmd strings just before you call getURL

                  Comment


                    #10
                    open WAN access: NO -- just trying to eliminate as many variables as possible. 😊

                    The JSON error shows up as a result of running the PHP script on a shared cloud Linux server that I pay a fee to use... I don't understand the error. It seems that the JSON request is reaching my HS3 program. But there is an issue with the version number at that point.

                    I'm trying to read and set the value of an HS3 virtual device, which is on a local PC. And I want to do this by using a PHP script running on a cloud server.

                    I haven't successfully pulled data from my local HS3 install using PHP-curl via myHS. Seems I always get the error. ( note the "HasSubscription: False below. ??)

                    I haven't successfully sent data to my local HS3 install using PHP-curl via myHS JSON calls.

                    The PHP code resides on a Cloud Linux server.

                    I execute the PHP script via a local Windows 10 Edge browser. Just a standard web query... e.g. http://distantsite.net/testfolder/testphpscript.php.


                    OK... I added Echos to the PHP script you had posted above. All sections of it worked properly except for the "Request Example" piece. The info between the quotes was correct but I deleted it for the sake of this post:

                    ------------------------

                    Get the Tunnel response: { "response": "ok", "responsecode": 200, "systems": [ { "license": "", "friendly_name": "", "hslocalip": "", "hswanip": "", "detected_wan_ip": "(IP of cloud server)", "tunnelip": "connectedxx.homeseer.com", "online": true } ], "HasSubscription": false} <--------- is this OK?

                    Parse out TunnelIP = connectedxx.homeseer.com

                    Build URLCmd = https://connectedxx.homeseer.com/JSON?user=""&pass=""

                    Request Example: Error generating page: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

                    Set Control Device response: { "Response":"Error, controlling device" } -----------> Even though this shows and Error, the device changed state.

                    -------------------------

                    I haven't found a JSON statement to obtain the value of a device. But I can control the state of the device as above.

                    Seems the PHP script is working and communicating with my HS3 install. But the failure occurs when Newtonsoft.Json is called for. Does that make any sense?

                    Thank you for looking at this stuff !!
                    Frank




                    Comment


                      #11
                      Couple of things. The connectedXX is you entering in the XX's? Should be numbers.
                      Subscription is only True if you have the paid one.

                      The myhs.homeseer.com request for system data seems to work, you get a tunnelIP back.

                      What is the full URL for the Value request?

                      Not sure what the "Request Example" is ? Is it a response (from what) and where is it located, or ???

                      I guess it makes sense that HS does use Newtonsoft when it gets a JSON request, so maybe the Vers # is a clue.
                      How are you determining what version you have?
                      How many Newtonsoft.json.dll do you have, what version and where are they located? I have one in /Bin and one in /Bin/scripting. Both Vers 8.0.3

                      Did you install/copy a Newtonsoft.json.dll manually?

                      Z

                      Comment


                        #12
                        Thank you!

                        I did have digits in place of the "XX" . I hadn't seen that before but since you asked, I know it is safe to say the digits were 22.

                        The "Value request URL": = "&request=controldevicebylabel&ref=14&label=Off"; (I have a device with ID 14 that I can turn on and off and I can hear the device controller click on and off).

                        I'd like to find similar requests to change the value and to read the value.

                        "Request Example" is from your PHP script above...

                        PHP Code:
                        # Request Example
                        # Get Status
                        $request "&request=getstatus";
                        $url $urlCmd.$request;
                        $response getURL($url); echo $response


                        It seems that HS is where the parsing of data occurs and Newtonsoft.json is what does that. Then results are returned to the requestor. (??)

                        The only place I know to look for the version number that HS uses is the version of the file in the bin/scripting directory... where the HS settings.ini file says to look to find it. The location specified in settings.ini is where Newtonsoft.json version 12.0.1.22727 is. I'd like to find a place that actually specifies what version is being used. It seems that version would be 8.0.0.0. (?)

                        While I didn't realize it, I have Newtonsoft.json in my bin folder too. Unfortunately, it is also 12.0.1.22727. (I had high hopes for a few seconds). I will do a full search of the PC to find other copies and versions.

                        I use Visual Studio 17 and tenScripting in Visual Studio to write the vb scripts. I install the NuGet package Newtonsoft.Json there. Then I make sure the version obtained is copied into the bin and bin/scripting folders. I also had a copy in the HS root directory. I removed it but it was version 12.0.1.22727. So I do install the Newtonsoft.Json.dll file manually.

                        Your version 8.0.3 Newtonsoft.Json is from March 2014.

                        I am using ..NetFramework 4.0. also visual studio vers 16.

                        Are the scripts written with a specific .NetFramework and Newtonsoft.Json version? (I would like to better understand that) I am wondering if I changed the NewtonsoftJson version to 8.03 if the world would collapse. I am assuming that if I compiled a script with Visual Studio, then it would be .NetFramework and Newtonsoft.Json specific. But it the scripts remain as vb.net when HS runs them, the .NetFramework and Newtonsoft.Json versions used by HS would apply.

                        I don't know whether Newtonsoft.Json 8.0.3 would be compatible with the version of Visual Studio I am using and with .NetFramework 4.0 and 4.5. But I believe I have read that Visual Studio 16 is compatible with Newtonsoft.Json 12. And Visual Studio 17 is compatible with Newtonsoft.Json 13 (13 is the newest).

                        Another issue is that I do have Imports in the script:
                        Imports Newtonsoft.Json.Linq
                        Imports Newtonsoft.Json

                        I don't know about the version of the Newtonsoft.Json.Ling file. Maybe I need to figure that out.


                        Again, thanks for the help with this.

                        Comment


                          #13
                          Probably need a Windows person (I'm not)..
                          There's been a lot of discussion about where Newtonsoft should be, but mostly for Plugins. Since it's HS that's doing the JSON -> .Net work (via Newtonsoft?), I would think it would be either the one in the /Bin or the /Bin/homeseer directory. I'm pretty sure there should NOT be one in the root directory.
                          I did a:
                          ....request=getstatus&ref=xxxx and had no problem retrieving the device info from my system. So I think the Newtonsoft issue is probably the problem.

                          You might confirm those two Newtonsoft dlls and their version (perhaps they need to be the same)? Have you restarted your HS box since your removed the Root verson?

                          Hopefully someone more knowledgeable on it will chime in..

                          Comment


                            #14
                            I have searched for Newtonsoft versions. I found several. But they are not in places where they would be used. The ones might be referred to are all of the same version.

                            Yes. I have restarted the HS PC since removing the newtonsoft file from the root directory. But it was the same version anyway. I feel the key to this whole problem is to figure out what "Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0" really refers to. I didn't know better, I would say it is coming from HS.

                            I appreciate your help and I am now able to control the state of an HS device from PHP. Odd I haven't found similar requests to control values of devices and to read those device values. But I'll look some more.

                            I'm going to change the newtonsoft version to 8.0.3 just for a test to see what happens.

                            Thanks again!!

                            Comment


                              #15
                              Try here, I gave you the Plugin doc before:
                              https://homeseer.com/support/homesee...nk/default.htm

                              If you do:
                              request=getstatus&ref=xxxxx
                              You'll get the status of the device back (and lots more)

                              Add this code to parse the getstatus call into a device array
                              Code:
                              $response = getURL($url);
                              $json = json_decode($response, TRUE);
                              $device = $json['Devices'][0];
                              print_r($device) to see the array of device options
                              Value would be: $device['value']
                              Name would be: $device['name']
                              etc..

                              Comment

                              Working...
                              X