more G-Labs products

Author Topic: Help Parse Web JSON data  (Read 10541 times)

January 02, 2015, 01:55:40 PM
Reply #15

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Quote
This is not working but you've pointed me to the right direction
I see that I forgot the semicolon at the end of the line...  ::)

If you use the parameters Sensor.Humidity and Sensor.Temperature with a sgeneric sensor widget (like used in the YouLess example) the values will show on the Analysis page. (after about 5 minutes or so)

I'm not sure if it will also work on a custom widget, but I can't see why not.
« Last Edit: January 02, 2015, 01:59:06 PM by mvdarend »

January 02, 2015, 02:49:48 PM
Reply #16

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
Quote
This is not working but you've pointed me to the right direction
I see that I forgot the semicolon at the end of the line...  ::)

If you use the parameters Sensor.Humidity and Sensor.Temperature with a sgeneric sensor widget (like used in the YouLess example) the values will show on the Analysis page. (after about 5 minutes or so)

I'm not sure if it will also work on a custom widget, but I can't see why not.

Now it works! Looks very great!
I was able to get temperature and humidity and place them on the generic widget. The values are now graphed as well.
That's a great point! Now, I think I need to create my own widget because in my array there are different temperatures. I would like to add them all to the widget. I ask you some suggestions:
As you've seen the central arduino expose a json array. It could add more leafes (records) as soon I add new sensors.
Do you suggest to create a program+widget per sensor or put all together into a single widget?

Maybe a good approach would be to have the program with the whole parsing system and then add widgets that are taken from the program. one widget for temperature+humidity of record 1. one widget for relay status from record 1. one widget for temperature+humidity of record 2. .... Possible? Or other suggestions?

About the graphs:
These values should graphed as well. Is there a naming convention that enables the graphs or how it works? I've seen calling the variables Sensor.Humidity it has generated the graph, but don't know how.

Opinions? :)

Thanks!

Simon

January 02, 2015, 02:56:03 PM
Reply #17

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
You can keep using bultin sensor widget to show all values from your app.
The sensor widget will display any parameter that starts with "Sensor.*".
So you can use "Sensor.Temperature.1", "Sensor.Temperature.2" and so on.
If you still want to create your own widget, I suggest to try the online editor:

http://codepen.io/genielabs/public/

there are few examples and also a slightly different sensor widget.

Cheers,
g.

January 02, 2015, 03:16:19 PM
Reply #18

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
You can keep using bultin sensor widget to show all values from your app.
The sensor widget will display any parameter that starts with "Sensor.*".
So you can use "Sensor.Temperature.1", "Sensor.Temperature.2" and so on.
If you still want to create your own widget, I suggest to try the online editor:

http://codepen.io/genielabs/public/

there are few examples and also a slightly different sensor widget.

Cheers,
g.


Hello Gene!
It's ok to use the generic widgets, maybe easier for me. I've tested using the naming convention you've suggested and it works.
The problem is I cannot specify a description on where the sensor is. I can only edit the title, but all temperatures are in the same windget so it's difficult to identfy which is the one I'm reading.
What do you suggest? Do I need to create a new Widget (maybe starting from your example)?
Is possible then to create multiple Widgets pointing to the same program or there is a relationship 1:1? Looking at the code "Program.AddControlWidget("homegenie/generic/sensor");" seems more a 1:1 relationship. One program, one widget. In this case I would need to create a bigger windget with all informations.

Thanks!

Simon

January 02, 2015, 03:27:41 PM
Reply #19

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
You can make your program add multiple sensor modules by using the Program.AddVirtualModules helper function:

AddVirtualModules

Code: [Select]
// create 4 virtual modules in the domain HomeAutomation.Arduino with address from 1 to 4
Program.AddVirtualModules("HomeAutomation.Arduino", "Sensor", "homegenie/generic/sensor", 1, 4);

then you program can use the function Program.RaiseEvent to update each sensor value:

Code: [Select]
// update temperature value of Sensor with address 1
var module1 = Modules.InDomain("HomeAutomation.Arduino").WithAddres("1").Get();
Program.RaiseEvent(module1, "Sensor.Temperature", value, "Sensor 1");

g.
« Last Edit: January 02, 2015, 03:29:25 PM by Gene »

January 02, 2015, 03:38:12 PM
Reply #20

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
That's exactly how I did it with my temperature sensors, I have 5 separate widgets. That way I can add a single widget per group (ie. Living room, Backyard etc.) and I also have one group, "temperature" with all of the temperature widgets.


January 02, 2015, 04:20:38 PM
Reply #21

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
You can make your program add multiple sensor modules by using the Program.AddVirtualModules helper function:

AddVirtualModules

Code: [Select]
// create 4 virtual modules in the domain HomeAutomation.Arduino with address from 1 to 4
Program.AddVirtualModules("HomeAutomation.Arduino", "Sensor", "homegenie/generic/sensor", 1, 4);


Code: [Select]
// update temperature value of Sensor with address 1
var module1 = Modules.InDomain("HomeAutomation.Arduino").WithAddres("1").Get();
Program.RaiseEvent(module1, "Sensor.Temperature", value, "Sensor 1");

g.


Thanks Gene!
I've written a long post to ask for other errors I've then solved by myself :)
Ok, I'm starting to understand it a little bit thanks to both you Gene and mvdarend!

@mvdarend:
The group with all the temperatures is a Widget you've created from new?

@all
What do you suggest to display the status of a relay?
I could get the status via json like temperatures and then act on the relay with an http post.

Suggestions?

Simon

January 02, 2015, 05:44:30 PM
Reply #22

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
Quote
The group with all the temperatures is a Widget you've created from new?

No, the group is just a standard HomeGenie group that I filled with widgets:

Configure -> Groups and Modules -> Add Group and then add al of my temperature widgets.

January 02, 2015, 07:29:10 PM
Reply #23

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
for implementing a relay logic you can use the "switch" widget ("homegenie/generic/switch").

g.

January 02, 2015, 10:11:09 PM
Reply #24

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
for implementing a relay logic you can use the "switch" widget ("homegenie/generic/switch").

g.

In this case do I need to make a new program to get the data via web to read the relay status or could I call the same program I've made to get the temperatures? the array I parse is containing all values, from temperature to relay status.
Do you have a relay example I could follow?
I suppose I need to:
- update the status every 'x' time via http get (like done here for temperatures)
- change the status if pressed via http post

Thanks again for the help!
I really like this software every day more :)

Simon

PS: @mvdarend: thanks, I've done the same :)

January 05, 2015, 03:47:30 PM
Reply #25

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
Sorry if I top post again this thread.
Could someone help me with my doubts and needs?
Most of all, an example of a SWITCH logic and how to use it, would be great. Even an example that does nothing or simple write a loca file ON or OFF when the switch button is pressed would be enough to start trying some code.

Thanks a lot!

Simon

January 05, 2015, 03:57:09 PM
Reply #26

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
This should get you started, I use the attached code for a couple of virtual switches.

By changing the code here you can create extra virtual switches: (I have two at the moment)
Code: [Select]
Program.AddVirtualModules("HomeAutomation.SettingSwitch", "Switch", "homegenie/generic/switch", 1, 2);

January 05, 2015, 04:31:48 PM
Reply #27

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31
This should get you started, I use the attached code for a couple of virtual switches.

By changing the code here you can create extra virtual switches: (I have two at the moment)
Code: [Select]
Program.AddVirtualModules("HomeAutomation.SettingSwitch", "Switch", "homegenie/generic/switch", 1, 2);

Thanks mvdarend!
What does exactly turn the switch ON or OFF?
Code: [Select]
case "Control.On"
This means here I should put the code that will be executed when the Relay is switched to on?

And what does this?
Code: [Select]
case "Control.Toggle"

Thanks a lot!

Simon

January 05, 2015, 04:44:48 PM
Reply #28

mvdarend

  • *****
  • Information
  • Hero Member
  • Posts: 431
You can control the virtual switches as follows:
Code: [Select]
// Get the Switch by ID
var mySwitch = Modules.InDomain("HomeAutomation.SettingSwitch").WithAddres("1").Get();
// Alternatively get the switch by name (you'll need to add it as a module to a group for this)
var myOtherSwitch = Modules.WithName("MyOtherSwitch").Get();

// Turn Switch on
mySwitch.Command("Control.On").Set();
// or Off
mySwitch.Command("Control.Off").Set();
// or Toggle the switch
mySwitch.Command("Control.Toggle").Set();

Quote
This means here I should put the code that will be executed when the Relay is switched to on?

You could, but putting it in When.ModuleParameterChanged() is probably better. I'm not certain about that though, maybe someone else could correct me here.

Quote
And what does this?

This 'toggles' the switch value, so if it was on, it will switch off and vice versa.

January 05, 2015, 05:12:35 PM
Reply #29

xefil

  • **
  • Information
  • Jr. Member
  • Posts: 31

I'll give a try, thank you!

Simon