[SOLVED] JSON from Arduino

My Arduino Mega (web server) is sending this JSON code to each client connection (GET). The numbers are updated each second.
*
{ “A”: [24], “B”: [121], “C”: [101.9673]}
*

I would like to read data in OH2 in order to create rules based on it and to publish data in HABpanel. Could you help me with an example? Or is there any tutorial to start working on? Thanks.

This is pretty close to what you’re after: [SOLVED] Mqtt Temp and humidity setup There’s a chap half way down wanting to read from a json string (which you are also doing)

You will need the http binding to get your JSON first of all
You will need to cache it. It will be more efficient because you have 3 values in your JSON. Also because the arduino can be slow and not respond to 3 very close in time http requests for three items.

See the http binding for configuration.

The to retreive data from a JSON you need to install the JSONPATH transform then for the value A for example, your JSONPATH would be $.A

BUT the value retrieved will be [24] not 24

You will need to change that in the rule code which means one more item per value
Can you change your Arduino code to get rid of the [ and ]?
Then you will be able to get the values directly with JSONPATH

Not figure out yet how to move on.
I was able to gather data from Arduino in Lua via Json but in OH2 seems sophisticated.

Is there any step-by-step tutorial?

Have a look here. :slight_smile:

Back to you for help… I’m completely stucked again. :frowning:
Where I am now:

  1. HTTP Binding installer and configured the Services/Http.cfg file
# 1
Eolian.url=http://192.168.1.101
Eolian.updateInterval=5000

# 2  
Solar.url=http://192.168.1.101
Solar.updateInterval=5000

# 3  
Battery.url=http://192.168.1.101
Battery.updateInterval=5000
  1. JSONPATH Transformation installed and Items/my.items file configured
Number EolianEvent { http="<[Eoliana.url:600:JSONPATH($.1.state.event)]" }
String EolianLastUpdated { http="<[Eoliana.url:600:JSONPATH($.1.state.lastupdated)]" }

Number SolarEvent { http="<[Solar.url:600:JSONPATH($.2.state.event)]" }
String SolarLastUpdated { http="<[Solar.url:600:JSONPATH($.2.state.lastupdated)]" }

Number BatteryEvent { http="<[Solar.url:600:JSONPATH($.3.state.event)]" }
String BatteryLastUpdated { http="<[Solar.url:600:JSONPATH($.3.state.lastupdated)]" }

  1. Created this rule
Number EolianEvent { http="<[Eolian.url:600:JSONPATH($.1.state.event)]" }
String EolianLastUpdated { http="<[Eolian.url:600:JSONPATH($.1.state.lastupdated)]" }


rule "Json check"
when Item EolianLastUpdated changed
then
// use the transformation service to retrieve the value
val newValue = transform("JSONPATH", "$.1.state.lastupdated", EolianLastUpdated.state.toString)

//post the new value to the Number Item
EolianEvent.postUpdate( newValue )
logInfo("JSON", " = ".newValue)

end

But, I cannot see any data (.newValue) in LogViewer.
What’s next step?! Where I’m wrong?
Thanks for any ideea.

Few issues here:

All your http.cfg configs are the same, you only need one.
Lets call it arduino

arduino.url=http://192.168.1.101
arduino.updateInterval=5000

Your items:

Number EolianEvent { http="<[.arduino.url:600:JSONPATH($.1.state.event)]" }
String EolianLastUpdated { http="<[arduino.url:600:JSONPATH($.1.state.lastupdated)]" }

Number SolarEvent { http="<[arduino.url:600:JSONPATH($.2.state.event)]" }
String SolarLastUpdated { http="<[arduino.url:600:JSONPATH($.2.state.lastupdated)]" }

Number BatteryEvent { http="<[arduino.url:600:JSONPATH($.3.state.event)]" }
String BatteryLastUpdated { http="<[arduino.url:600:JSONPATH($.3.state.lastupdated)]" }

The json you posted:

{ “A”: [24], “B”: [121], “C”: [101.9673]}

Is that the one being returned now?
Because the JSONPATH in the items don’t match that json.
Can you post the json being retrieved, please?

Vincent, the json structure coming from Arduino:

{
  "Eolian": [
    0.00962
  ],
  "Solar": [
    0.072126
  ],
  "Battery": [
    94.58078
  ]
}

These are the correct Jsonpath statements
See:

There is no time information in the json so your LastUpdated items will have to be timestamped differently with a rule

Number EolianEvent { http="<[arduino.url:1000:JSONPATH($.Eolian.[0])]" }
DateTime EolianLastUpdated

Number SolarEvent { http="<[arduino.url:1000:JSONPATH($.Solar.[0])]" }
DateTime SolarLastUpdated

Number BatteryEvent { http="<[arduino.url:1000:JSONPATH($.Battery.[0])]" }
DateTime BatteryLastUpdated

And a rule:

rule "Last updated"
when
    EolianEvent changed or
    SolarEvent changed or
    BatteryEvent changed
then
    var String itemName = triggeringItem.name.toString.replace("Event","LastUpdated")
    postUpdate(itemName, now.toString)
end

Vincent, I’ve played around but no, not able to make it work.

I have 3 values into Json string sent by Arduino, but I’m trying to make it work the first item/value only - to be much easy to work and understand the process. Then, I’ll extend the code to include all 3 items/values.
So, I’ll try now to read in OH2 the first item from Json only: Eolian

Vincent, as you instruct, I changed to this in html.cfg:

arduino.url=http://192.168.1.101
arduino.updateInterval=5000

My.items now:

Number EolianEvent { http="<[arduino.url:1000:JSONPATH($.Eolian.[0])]" }
DateTime EolianLastUpdated

The rule created:

rule "Last updated"
when
    EolianEvent changed
then
    var String EolianEvent = EolianLastUpdated.name.toString.replace("Event", "LastUpdated")
    postUpdate(EolianEvent, now.toString)
end

The error in LogViewer:

2018-07-10 12:51:20.240 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'json.rules' has errors, therefore ignoring it: [4,5]: no viable alternative at input 'EolianEvent'

My fault:
I changed your modifications of the var statement. didn’t make sense with only one item.

rule "Last updated"
when
    item EolianEvent changed
then
    // var String EolianEvent = EolianLastUpdated.name.toString.replace("Event", "LastUpdated")
    postUpdate("EolianLastUpdated", now.toString)
end

I’ve updated… LogViewer says: Loading model 'json.rules
But no other info in LogViewer to show any data.

It will only show an update when EolianEvent value changes

I have acces to an webpage (conected directly to Arduino via http) where I can see that data from “Eolian” is changing… since the windturbine is rotating right now (little wind outside :slight_smile: ).

What is the state of EolianEvent?

The binding is wrong. You don’t need the .url

Number EolianEvent { http="<[arduino:1000:JSONPATH($.Eolian.[0])]" }

Yeaaaaaaaaaaaaaaaaaaaaaaaaaaa… that’s it (no: .url):
2018-07-10 13:17:48.564 [vent.ItemStateChangedEvent] - EolianEvent changed from 30.33459 to 39.43356

2018-07-10 13:17:48.589 [vent.ItemStateChangedEvent] - EolianLastUpdated changed from 2018-07-10T13:17:42.967+0300 to 2018-07-10T13:17:48.557+0300

Good, now change the other two items (Remove the .url)
And use this:

rule "Last updated"
when
    item EolianEvent changed or
    item SolarEvent changed or
    item BatteryEvent changed
then
    var String itemName = triggeringItem.name.toString.replace("Event","LastUpdated")
    postUpdate(itemName, now.toString)
end
1 Like

Quick question. Did you make your own weather station?

No… I have an off-grid strawbale house powered by Wind and Solar wich stored energy in the battery bank. I fired Fibaro and now I’m combining OH2 with Raspbbery to make it smart again.

But an “weather station” will be the next step. Now I’m getting data from individual sensors only via Arduino.