Announcement

Collapse
No announcement yet.

Adventures with Spark/Particle Core and HS3

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

    Adventures with Spark/Particle Core and HS3

    https://store.particle.io/ for more information.

    I have no real application for it right at the minute but thought I would just post that it is possible to control them by HS with a little effort (no I am not making this into a plugin, this is just to give people an idea - the possibilities are fairly limitless). If you have one of the boards and flash it with the example that Particle have in the IDE called 'WEB-CONNECTED LED' you can control the status LED on the board by sending it HTTP commands.

    You can create a simple on/off virtual device in HomeSeer or in my case I used the reference of one of my existing X10 devices. Take a note of the device reference number.

    Create a VB script in your scripts directory with a script that looks like this (this is quick and ugly but is just to prove a point);

    Code:
    Const LT As String = "ParticleCoreScript"
    Const DeviceID As String = "53ff6d066667574841522xxx"
    Const AccessToken As String = "9b0118604a7ba3b932b9efbd4b96d044dec28xxx"
    Const dvRef As Integer = 101
    
    Sub Main(Parm As Object)
    
        Try
            Dim URL As String = "https://api.particle.io/v1/devices/" & DeviceID & "/led" 'led is the name of our access point
            Dim responsebytes() As Byte
            Dim deviceStatus As String = hs.CAPIGetStatus(dvRef).Status.tolower
            Dim commandToSend As String = IIf(deviceStatus = "on", "on", "off")
    
            Using Wclient As New Net.WebClient
                Dim headerlist As New Specialized.NameValueCollection
                headerlist.Add("access_token", AccessToken) 'Send the access token
                headerlist.Add("params", commandToSend) 'to match the entry command in the spark code
                responsebytes = Wclient.UploadValues(URL, "POST", headerlist)
                hs.writelog(LT, "Reply: " & System.Text.Encoding.ASCII.GetString(responsebytes))
            End Using
    
        Catch ex As Exception : hs.writelog(LT, "Exception: " & ex.Message.ToString)
        End Try
    End Sub
    You will need to change the DeviceID, AccessToken and dvRef to the device ID of the relevant Core board (available in the dashboard), the access token also from the dashboard and the dvRef to the reference of your device you have created or wish to mimic.

    Then create an event that looks like the attached photo, it will just run the script when you change the device to any state. The script picks up the status and then sends the HTTP instructions to the board.

    Although it routes through their respective cloud service my LED turns on quicker than it takes my X10 signal to come from downstairs!

    Possibilities are limitless with these things! Remote temperature/humidity/sensor data...controlling stuff remotely or whatever.
    Last edited by mrhappy; December 4, 2015, 03:02 PM.

    #2
    Thanks, I was thinking about getting one myself to control my garage door. Wish it would work with the arduino plugin though...

    Verstuurd vanaf mijn Nexus 5 met Tapatalk

    Comment


      #3
      Originally posted by kriz83 View Post
      Thanks, I was thinking about getting one myself to control my garage door. Wish it would work with the arduino plugin though...

      Verstuurd vanaf mijn Nexus 5 met Tapatalk
      If you use IFTTT with HomeSeer then you can use the channel (Particle have an IFTTT channel) to turn the inputs on and off...just tried it and the reply was almost instant. No messing around with scripts...only really going to work for simple on/off control though.
      Last edited by mrhappy; January 23, 2016, 07:23 AM.

      Comment


        #4
        Oh, Very NICE work Adam! Like the ifttt possibility too.
        Tom
        baby steps...starting again with HS3
        HS3Pro: Z-NET & 80 Z wave Devices,
        HSTouch: 4 Joggler (Android Kitkat), 2 iPhone, 3 iPads
        Whole House Audio: 5 SqueezePlay Jogglers w Bose Speakers
        In The Works: 10 Cameras Geovision, new Adecmo/Envisalink Alarm, Arduinos
        System: XP on Fanless Mini-ITX w/ SSD

        Comment


          #5
          I've had two of these from their Kickstarter campaign and had them setup with HS2 so I could read temperatures from them. I was using a .bat file to curl the Core's web address, save the contents to a text file, process it and capture its temps. Having moved to HS3 and Linux, I'm struggling to find a way to do the same. Any ideas on how to do this in Linux now? I can figure out how to parse... I'm just sure how to run a script to do curl or wget.


          Sent from my iPad using Tapatalk

          Comment


            #6
            Originally posted by bpm32 View Post
            I've had two of these from their Kickstarter campaign and had them setup with HS2 so I could read temperatures from them. I was using a .bat file to curl the Core's web address, save the contents to a text file, process it and capture its temps. Having moved to HS3 and Linux, I'm struggling to find a way to do the same. Any ideas on how to do this in Linux now? I can figure out how to parse... I'm just sure how to run a script to do curl or wget.


            Sent from my iPad using Tapatalk
            I'm not sure really the Linux version of HS is not my strong point. There is of course the option to run an application in the HS3 events page but not sure if there are differences to how HS3 on windows runs applications.

            I would do it from a script personally but I have tried in Linux and I am not getting very far because I can't work out how the referencing of a DLL works and as such it fails because it can't find it (you may know - it is something I think needs to be done in Mono but I have tried installing all the libraries I think are relevant or linked). This would be the sort of script I would use in HS that would get the JSON output from the device and then send it to the HS log which can in turn could be used to set a device value.

            Code:
            Imports System.Net
            Imports System.Web.Script.Serialization
            
            Sub Main(ByVal Parm As Object)
            
                Try
                    Dim client As WebClient = New WebClient()
                    Dim reply As String = client.DownloadString("https://api.particle.io/v1/devices/53ff6d066667574841522xxx/analogvalue?access_token=9b0118604a7ba3b932b9efbd4b96d044dec28xxx")
            
                    Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(reply)
            
                    hs.writelog("", reply)
                    hs.writelog("", "Result: " & j("result"))
            
                Catch ex As Exception
                    hs.writelog("", ex.message)
                End Try
            
            End Sub
            One option depending on how much you know about the code on the core would be to go a HTTP GET into the new JSON API that HS3 has, as such you would need to do nothing on the HS side of things and the core would set the devices value.

            Comment


              #7
              Originally posted by mrhappy View Post
              There is of course the option to run an application in the HS3 events page but not sure if there are differences to how HS3 on windows runs applications.

              *forehead smack* Oh man, what was I thinking?? Of course! Thank you for the subtle kick there.

              I got it working great. For the record, I ended up using wget. I chose /usr/bin/wget for the 'application' and entered the following in the parameters field:

              Code:
              https://api.spark.io/v1/devices/MY_DEVICE_ID/MY_VAR?access_token=MY_TOKEN -O output.txt
              Note: it requires -O and not -o. There is a difference.

              I haven't finished this up yet, but to close this out, I will run this process, then add a 15 second "wait", then run a script to parse the temperature from the output.txt file and save it to a device (string and value).

              Now I can pull data from my Spark Cores in HS3.

              Thanks!

              Comment


                #8
                Originally posted by mrhappy View Post
                https://store.particle.io/ for more information.

                I have no real application for it right at the minute but thought I would just post that it is possible to control them by HS with a little effort (no I am not making this into a plugin, this is just to give people an idea - the possibilities are fairly limitless). If you have one of the boards and flash it with the example that Particle have in the IDE called 'WEB-CONNECTED LED' you can control the status LED on the board by sending it HTTP commands.

                You can create a simple on/off virtual device in HomeSeer or in my case I used the reference of one of my existing X10 devices. Take a note of the device reference number.

                Create a VB script in your scripts directory with a script that looks like this (this is quick and ugly but is just to prove a point);

                Code:
                Const LT As String = "ParticleCoreScript"
                Const DeviceID As String = "53ff6d066667574841522xxx"
                Const AccessToken As String = "9b0118604a7ba3b932b9efbd4b96d044dec28xxx"
                Const dvRef As Integer = 101
                
                Sub Main(Parm As Object)
                
                    Try
                        Dim URL As String = "https://api.particle.io/v1/devices/" & DeviceID & "/led" 'led is the name of our access point
                        Dim responsebytes() As Byte
                        Dim deviceStatus As String = hs.CAPIGetStatus(dvRef).Status.tolower
                        Dim commandToSend As String = IIf(deviceStatus = "on", "on", "off")
                
                        Using Wclient As New Net.WebClient
                            Dim headerlist As New Specialized.NameValueCollection
                            headerlist.Add("access_token", AccessToken) 'Send the access token
                            headerlist.Add("params", commandToSend) 'to match the entry command in the spark code
                            responsebytes = Wclient.UploadValues(URL, "POST", headerlist)
                            hs.writelog(LT, "Reply: " & System.Text.Encoding.ASCII.GetString(responsebytes))
                        End Using
                
                    Catch ex As Exception : hs.writelog(LT, "Exception: " & ex.Message.ToString)
                    End Try
                End Sub
                You will need to change the DeviceID, AccessToken and dvRef to the device ID of the relevant Core board (available in the dashboard), the access token also from the dashboard and the dvRef to the reference of your device you have created or wish to mimic.

                Then create an event that looks like the attached photo, it will just run the script when you change the device to any state. The script picks up the status and then sends the HTTP instructions to the board.

                Although it routes through their respective cloud service my LED turns on quicker than it takes my X10 signal to come from downstairs!

                Possibilities are limitless with these things! Remote temperature/humidity/sensor data...controlling stuff remotely or whatever.
                Hi, thanks to this code I am currently controlling a Particle Photon from HS3. I do not know how to make the opposite, get some data from Photon to be read in HS3. Any clue? Many thanks by the way.

                Comment


                  #9
                  Originally posted by iglesiasruben View Post
                  Hi, thanks to this code I am currently controlling a Particle Photon from HS3. I do not know how to make the opposite, get some data from Photon to be read in HS3. Any clue? Many thanks by the way.
                  Is it binary data (a high/low from a sensor) or a variable? You have two options really for the first but only one that I know of for the second.

                  Particle now supports web hooks, so you can go to https://console.particle.io/integrations and set up web hooks for each event raised. If you have an event that triggers when a pin is high and another event for low then using the MyHS + JSON interface you can turn a device on/off when the events get raised in the Particle device.

                  If you are talking about variable data like an LDR value then the only way AFAIK is polling the Particle service by a recurring HS event. Then running a script, this is an example of the script I run that gets my data and updates three HS devices.

                  Code:
                  Imports System.Web.Script.Serialization
                  
                  Dim access_token As String = "9b0118604a7ba3b932b"
                  Dim json As New JavaScriptSerializer
                  
                  Sub Main(ByVal Parms As Object)
                  
                      Try
                          Dim tempDataRaw As String = hs.GetURL("https://api.particle.io", "/v1/devices/240027000447353138383138/OutsideTemp?access_token=" & access_token, True, 80)
                  		Dim LDRDataRaw As String = hs.GetURL("https://api.particle.io", "/v1/devices/240027000447353138383138/LDRValue?access_token=" & access_token, True, 80)
                  		Dim SoilDataRaw As String = hs.GetURL("https://api.particle.io", "/v1/devices/240027000447353138383138/SoilValue?access_token=" & access_token, True, 80)
                  		
                  		'hs.writelog("Spark", tempDataRaw)
                  		
                          Dim tempData As resultdata = json.Deserialize(Of resultdata)(tempDataRaw)
                  		Dim LDRData As resultdata = json.Deserialize(Of resultdata)(LDRDataRaw)
                  		Dim SoilData As resultdata = json.Deserialize(Of resultdata)(SoilDataRaw)
                  		
                  		'hs.writelog("Spark", tempdata.coreInfo.connected)
                  		
                  		If tempdata.coreInfo.connected = true then
                  		hs.setdevicevaluebyref(835, 100, True)		
                  		else
                  		hs.setdevicevaluebyref(835, 0, True)		
                  		end if
                  		
                          'hs.writelog("SparkScript", "LDR AtoD Value: " & ldrData.result)
                  		'hs.writelog("SparkScript", "Soil Sensor AtoD Value: " & SoilData.result)
                          'hs.writelog("SparkScript", "TempData Value: " & tempData.result)
                  
                          Select Case convert.todouble(SoilData.result)
                              Case 0 To 2700
                                  hs.setdevicevaluebyref(838, 1, True) 'plants are wet
                                  hs.writelog("SparkScript", "Soil Sensor AtoD Value: " & SoilData.result)
                  			case 2701 to 4096 :	hs.setdevicevaluebyref(838, 0, True) 'plants are dry
                  			case else : hs.setdevicevaluebyref(838, -1, True)
                          End Select
                  
                          Select Case convert.todouble(ldrData.result)
                  			case 0 to 1000 : hs.setdevicevaluebyref(837, 0, True)
                              Case 1001 To 4096 : hs.setdevicevaluebyref(837, 1, True)
                          End Select
                  		
                  		Select Case convert.todouble(tempData.result)
                              Case -10 To 50 : hs.setdevicevaluebyref(448, convert.todouble(tempData.result), True)
                          End Select
                  		
                  
                      Catch ex As Exception
                          'hs.writelog("SparkScript", "Exception: " & ex.message.tostring)
                          If ex.message.tostring.tolower.contains("timeout.") Then
                              hs.writelog("SparkScript", "Possible Device Offline")
                          End If
                      End Try
                  
                  End Sub
                  
                  Public Class resultdata
                      Public Property name As String
                      Public Property result As String
                  	Public Property coreInfo As coreinfodata
                  End Class
                  
                  Public Class coreinfodata
                  	Public Property connected As Boolean
                  End Class
                  Last edited by mrhappy; December 28, 2016, 03:18 PM.

                  Comment


                    #10
                    Thanks a lot. I am trying to make the correspondent changes for my installation, but I get a script compiling error as it seems 'JavaScriptSerializer' type is not defined. I am not expert programmer in this context Thankful if you could help me.

                    Comment


                      #11
                      Originally posted by iglesiasruben View Post
                      Thanks a lot. I am trying to make the correspondent changes for my installation, but I get a script compiling error as it seems 'JavaScriptSerializer' type is not defined. I am not expert programmer in this context Thankful if you could help me.
                      My bad on this one, I should've said. Open your settings.ini file in your config directory, in here look for a line starting ScriptingReferences, if you have this line add to the end of it (separated by a , )

                      System.Web.Extensions;System.Web.Extensions.dll

                      if you don't have a ScriptingReferences line then just add one which looks like this;

                      ScriptingReferences=System.Web.Extensions;System.Web.Extensi ons.dll

                      and try the script again.

                      Comment


                        #12
                        Perfect, now is working properly. Many thanks.

                        Comment


                          #13
                          Hi, after the successful attempt polling data from Photon device to HS3, I would need to use webhooks in order to avoid polling for a situation in which I would need a faster response (I do not like having an event triggering every second).

                          I am a little bit lost about configuring webhooks in Particle integrations section. First issue is I do not know how to fill the URL field.

                          Hope someone tried this before.

                          Comment


                            #14
                            Originally posted by iglesiasruben View Post
                            Hi, after the successful attempt polling data from Photon device to HS3, I would need to use webhooks in order to avoid polling for a situation in which I would need a faster response (I do not like having an event triggering every second).

                            I am a little bit lost about configuring webhooks in Particle integrations section. First issue is I do not know how to fill the URL field.

                            Hope someone tried this before.
                            You need to use myHS first (they may allow basic authentication I can't remember) and the way I did was to put the url in the web hook. The easiest way to do it is via the JSON interface, example URL's are here - https://homeseer.com/support/homesee..._with_json.htm which then need to be put in the web hooks box.

                            Comment

                            Working...
                            X