Access Device Info via Raspberry Pi

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Access Device Info via Raspberry Pi

#1 Post by 1CM69 » 09 Apr 2019, 13:48

Hi,

I have two Sonoff POW R2 switches, both are flashed with ESPeasy Mega 20190315 and working perfectly as expected.

They are both controlled via a script which is run on my Raspberry Pi which only switches them on if the temperature falls below a set level received via a BME280 attached to my Raspberry Pi.

Is there a way that I can interrogate each switch & retrieve the Energy data i.e. Voltage, Current & Uptime using my script so I can store this info on my Raspberry Pi?

I have looked at Domoticz but this does not really suit my needs as I use my Raspberry Pi for other purposes too & do not want to format it.

Thanks in advance

Kirk

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Access Device Info via Raspberry Pi

#2 Post by grovkillen » 09 Apr 2019, 15:12

You should fetch the JSON page and grab the values there and then store them however you want.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#3 Post by 1CM69 » 09 Apr 2019, 15:29

grovkillen wrote: 09 Apr 2019, 15:12 You should fetch the JSON page and grab the values there and then store them however you want.
Perfect, thank you.

Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Re: Access Device Info via Raspberry Pi

#4 Post by Wiki » 09 Apr 2019, 17:40

To simply store the data on my Raspi I have chosen to write a small php script (Apache server is installed) and placed it under /var/www/html/Data.php.
Example:

Code: Select all

<?php
isset($_GET['value1']) ? $value1=$_GET['value1'] : $value1='';
isset($_GET['value2']) ? $value2=$_GET['value2'] : $value2='';
$jetzt = time();
$handle = fopen("./Data.csv", 'a');
fputcsv($handle, array($jetzt,$value1,$value2));
fclose($handle);
?>
So I am able to use either the Generic HTTP controller by using Controller Publish with:

Code: Select all

Data.php?value1=%val1%&value2=%val2%
or - more flexible - out of a set of rules using SendToHTTP:

Code: Select all

On Sensor#Event Do
SendToHTTP 192.168.1.1,80,/Data.php?value1=[Sensor#Data1]&value2=[Sensor#Data2]
EndOn
So I havn't to friggle around with parsing the JSON stuff - which I even don't understand.

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#5 Post by 1CM69 » 09 Apr 2019, 18:29

Wiki wrote: 09 Apr 2019, 17:40 To simply store the data on my Raspi I have chosen to write a small php script (Apache server is installed) and placed it under /var/www/html/Data.php.
Example:

Code: Select all

<?php
isset($_GET['value1']) ? $value1=$_GET['value1'] : $value1='';
isset($_GET['value2']) ? $value2=$_GET['value2'] : $value2='';
$jetzt = time();
$handle = fopen("./Data.csv", 'a');
fputcsv($handle, array($jetzt,$value1,$value2));
fclose($handle);
?>
So I am able to use either the Generic HTTP controller by using Controller Publish with:

Code: Select all

Data.php?value1=%val1%&value2=%val2%
or - more flexible - out of a set of rules using SendToHTTP:

Code: Select all

On Sensor#Event Do
SendToHTTP 192.168.1.1,80,/Data.php?value1=[Sensor#Data1]&value2=[Sensor#Data2]
EndOn

So I havn't to friggle around with parsing the JSON stuff - which I even don't understand.
Thanks WiKi, looks like an option that I may need to use if I cannot get my head around this JSON stuff either.

Just a quick question, do the POW R2 units naturally store the values for current etc or do i need to add something to the espeasy web interface in order to setup the unit for recording these parameters?

Regards..,

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Access Device Info via Raspberry Pi

#6 Post by grovkillen » 09 Apr 2019, 18:32

JSON is great, especially if you're using JavaScript. But whatever get you where you want to go is good.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#7 Post by 1CM69 » 09 Apr 2019, 18:36

grovkillen wrote: 09 Apr 2019, 18:32 JSON is great, especially if you're using JavaScript. But whatever get you where you want to go is good.
yep ;)

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#8 Post by 1CM69 » 10 Apr 2019, 01:11

Hi,
so I was just attempting to get some info from my Sonoff unit via JSON & failed miserably.

Here is my JSON output:

Code: Select all

{
  "System": {
    "Build": 20103,
    "Git Build": "mega-20190315",
    "System Libraries": "ESP82xx Core 2_4_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3 PUYA support",
    "Plugins": 46,
    "Plugin description": " [Normal]",
    "Local Time": "1970-00-00 00:00:00",
    "Unit Number": 0,
    "Unit Name": "Dimplex-Heater-Landing",
    "Uptime": 40,
    "Last Boot Cause": "Cold boot",
    "Reset Reason": "Power on",
    "Load": 12.4,
    "Load LC": 3140,
    "CPU Eco Mode": "false",
    "Free RAM": 17024
  },
  "WiFi": {
    "Hostname": "Dimplex-Heater-Landing",
    "IP Config": "DHCP",
    "IP Address": "***.***.***.***",
    "IP Subnet": "255.255.255.0",
    "Gateway": "***.***.***.***",
    "STA MAC": "***",
    "DNS 1": "***.***.***.***",
    "SSID": "***",
    "BSSID": "***",
    "Channel": 1,
    "Connected msec": 2423893,
    "Last Disconnect Reason": 1,
    "Last Disconnect Reason str": "(1) Unspecified",
    "Number Reconnects": 0,
    "Force WiFi B/G": "false",
    "Restart WiFi Lost Conn": "false",
    "Force WiFi No Sleep": "false",
    "Periodical send Gratuitous ARP": "false",
    "Connection Failure Threshold": 0,
    "RSSI": -44
  },
  "Sensors": [
    {
      "TaskValues": [
        {
          "ValueNumber": 1,
          "Name": "State",
          "NrDecimals": 0,
          "Value": 0
        }
      ],
      "DataAcquisition": [
        {
          "Controller": 1,
          "IDX": 0,
          "Enabled": "false"
        },
        {
          "Controller": 2,
          "IDX": 0,
          "Enabled": "false"
        },
        {
          "Controller": 3,
          "IDX": 0,
          "Enabled": "false"
        }
      ],
      "TaskInterval": 0,
      "Type": "Switch input - Switch",
      "TaskName": "POWER",
      "TaskEnabled": "true",
      "TaskNumber": 1
    },
    {
      "TaskValues": [
        {
          "ValueNumber": 1,
          "Name": "Volts",
          "NrDecimals": 1,
          "Value": -1
        },
        {
          "ValueNumber": 2,
          "Name": "%",
          "NrDecimals": 0,
          "Value": 12
        },
        {
          "ValueNumber": 3,
          "Name": "Uptime--mins",
          "NrDecimals": 0,
          "Value": 40
        },
        {
          "ValueNumber": 4,
          "Name": "",
          "NrDecimals": 0,
          "Value": 0
        }
      ],
      "DataAcquisition": [
        {
          "Controller": 1,
          "IDX": 0,
          "Enabled": "false"
        },
        {
          "Controller": 2,
          "IDX": 0,
          "Enabled": "false"
        },
        {
          "Controller": 3,
          "IDX": 0,
          "Enabled": "false"
        }
      ],
      "TaskInterval": 60,
      "Type": "Generic - System Info",
      "TaskName": "Energy",
      "TaskEnabled": "true",
      "TaskNumber": 2
    }
  ],
  "TTL": 60000
}
so my question is, how do I construct the URL to for instance retrieve the 'Volts' value from this data?

I assume it starts something like http://[my ip]/json? but from there on I do not know how to add the parameters.

I have had a look through the forum and found the sample zip file with the JS example.html page but this is not really helping me as I am struggling to navigate to what I should be using & personally for my purposes the JS HTML page does not really work, I do not want to display the values on a webpage but only store the value at my end.

Regards..,

Wiki
Normal user
Posts: 413
Joined: 23 Apr 2018, 17:55
Location: Germany

Re: Access Device Info via Raspberry Pi

#9 Post by Wiki » 10 Apr 2019, 10:23

Now you know, what I was talking about :mrgreen:

Code: Select all

pi@raspberrypi:~ $ man woman
No manual entry for woman
pi@raspberrypi:~ $

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Access Device Info via Raspberry Pi

#10 Post by grovkillen » 10 Apr 2019, 10:56

I love using JavaScript but in your case maybe cURL?

https://www.weichieprojects.com/blog/cu ... -with-php/
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#11 Post by 1CM69 » 10 Apr 2019, 13:17

Wiki wrote: 10 Apr 2019, 10:23 Now you know, what I was talking about :mrgreen:
:D

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#12 Post by 1CM69 » 10 Apr 2019, 13:18

grovkillen wrote: 10 Apr 2019, 10:56 I love using JavaScript but in your case maybe cURL?

https://www.weichieprojects.com/blog/cu ... -with-php/
Thank you, that's very helpful

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#13 Post by 1CM69 » 10 Apr 2019, 13:26

I think that I may have been over complicating things & have thought of a far more simpler approach.

As my Raspberry Pi only sends out a command to each of my switches when certain criteria have already been met, I can simply add a counter to the end of each 'switch on' command inside my Python script & keep a tally of how many 'ON' commands have been sent.
Each 'ON' command periods is 15 mins long, so using a counter it is then very easy to calculate total UpTime and all this is done at my Raspberry Pi end.

Given that my switches are powering heaters that have a constant current I can also use the UpTime calculation that I have gathered to calculate energy consumed.

I know this is a little off topic & beyond the scope of ESPeasy but I will post back my findings.

Regards..,

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Access Device Info via Raspberry Pi

#14 Post by ThomasB » 10 Apr 2019, 21:56

I think that I may have been over complicating things & have thought of a far more simpler approach.
It's a shame to ignore the power measurement features in your Sonoff. So rather than give up on JSON, maybe we can walk you through it.

Your RPI will do all the heavy lifting; You just have to add some commands to your existing Python script. So to get you started, I grabbed a Sonoff Basic and created a demonstration for reading ESPEasy's JSON.

My Python file is named sonoff.py. It shows how to retrieve the full JSON. Then using list indices any data that is wanted can be extracted. In this example it shows the value of the built-in PushSwitch (0/1). Plus some other things to make it interesting.

When it is run from the RPI's command line, this is the result:

Code: Select all

$ python sonoff.py
Wifi RSSI:  -77 dBm
Unit Name:  Basic_1
Channel: 1
Device Name:  PBSW
Device Value:  0
Here is the Python script (sonoff.py) example:

Code: Select all

#JSON example using Sonoff Basic Smart Switch & ESPEasy.
#ThomasB, Apr-10-2019

#Connect to ESPEasy on local IP (change as needed to match the device) and load the jason into a array called "espez"
import json, requests
url = requests.get('http://192.168.1.221/json')
espez = json.loads(url.text)

#Print out some info
print "Wifi RSSI: ", int(espez['WiFi']['RSSI']), "dBm"
print "Unit Name: ", espez['System']['Unit Name']

#Show an example of creating a variable and print it.
channel = (espez['WiFi']['Channel'])
print "Channel:", int(channel)

#Show an example of printing the current value of the built-in push button switch.
#Device configuration as follows: Task= 1, Device= Switch input-Switch, Name= PBSW
print "Device Name: ", espez['Sensors'][0]['TaskName']
PBSwValue = int(espez['Sensors'][0]['TaskValues'][0]['Value'])
print "Device Value: ", PBSwValue

Of course the example's details are specific to my setup. To update it with your device states just review the JSON output on your ESPEasy Sonoff. The data you want to extract is defined by indices (bracketed values) that can be found by studying ESPEasy's JSON print-out.

- Thomas

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#15 Post by 1CM69 » 10 Apr 2019, 22:23

ThomasB wrote: 10 Apr 2019, 21:56
I think that I may have been over complicating things & have thought of a far more simpler approach.
It's a shame to ignore the power measurement features in your Sonoff. So rather than give up on JSON, maybe we can walk you through it.

Your RPI will do all the heavy lifting; You just have to add some commands to your existing Python script. So to get you started, I grabbed a Sonoff Basic and created a demonstration for reading ESPEasy's JSON.

My Python file is named sonoff.py. It shows how to retrieve the full JSON. Then using list indices any data that is wanted can be extracted. In this example it shows the value of the built-in PushSwitch (0/1). Plus some other things to make it interesting.

When it is run from the RPI's command line, this is the result:

Code: Select all

$ python sonoff.py
Wifi RSSI:  -77 dBm
Unit Name:  Basic_1
Channel: 1
Device Name:  PBSW
Device Value:  0
Here is the Python script (sonoff.py) example:

Code: Select all

#JSON example using Sonoff Basic Smart Switch & ESPEasy.
#ThomasB, Apr-10-2019

#Connect to ESPEasy on local IP (change as needed to match the device) and load the jason into a array called "espez"
import json, requests
url = requests.get('http://192.168.1.221/json')
espez = json.loads(url.text)

#Print out some info
print "Wifi RSSI: ", int(espez['WiFi']['RSSI']), "dBm"
print "Unit Name: ", espez['System']['Unit Name']

#Show an example of creating a variable and print it.
channel = (espez['WiFi']['Channel'])
print "Channel:", int(channel)

#Show an example of printing the current value of the built-in push button switch.
#Device configuration as follows: Task= 1, Device= Switch input-Switch, Name= PBSW
print "Device Name: ", espez['Sensors'][0]['TaskName']
PBSwValue = int(espez['Sensors'][0]['TaskValues'][0]['Value'])
print "Device Value: ", PBSwValue

Of course the example's details are specific to my setup. To update it with your device states just review the JSON output on your ESPEasy Sonoff. The data you want to extract is defined by indices (bracketed values) that can be found by studying ESPEasy's JSON print-out.

- Thomas
WOW! Thomas, thank you for putting the time in to this, much appreciated. I think I can work through this now without it giving me a headache ;)

One question, while testing last night I setup a device in the web interface 'System Info', chose only 3 of the alloted 4 tasks that I wished to measure, I think from memory VCC, Load & UpTime. Now the section below the device setup you are able to enter your own values for the task names and there are 4 rows.
I only used 3 of the rows but it seemingly will not let me clear the text from the 4th row so as it is blank, is there a way to do this that I am missing?

Regards..,

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Access Device Info via Raspberry Pi

#16 Post by ThomasB » 10 Apr 2019, 22:37

I only used 3 of the rows but it seemingly will not let me clear the text from the 4th row so as it is blank, is there a way to do this that I am missing?
I tested this on a Sonoff (mega-20190409) and had no problem removing the 4th row value's name from "Generic - System Info" plugin device. Are you sure you submitted the edited device to save the changes?

- Thomas

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#17 Post by 1CM69 » 11 Apr 2019, 00:25

I am using mega-20190315 & have just tried again deleting and then remaking the device but it continually shows the blank 4th value name, see attached pics.
Untitled2.png
Untitled2.png (32.04 KiB) Viewed 13036 times
Untitled3.png
Untitled3.png (29.87 KiB) Viewed 13036 times
Odd, it's showing a colon & a zero value entry like it expecting something.

Regards..,

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Access Device Info via Raspberry Pi

#18 Post by ThomasB » 11 Apr 2019, 00:34

it continually shows the blank 4th value name, see attached pics.
That is normal, you can't hide unused System Info plugin values. I don't know why it was implemented that way, but the workaround is to ignore the unused values. If it helps, name it "Unused" as a reminder to ignore it.

- Thomas

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#19 Post by 1CM69 » 11 Apr 2019, 00:42

ThomasB wrote: 11 Apr 2019, 00:34
it continually shows the blank 4th value name, see attached pics.
That is normal, you can't hide unused System Info plugin values. I don't know why it was implemented that way, but the workaround is to ignore the unused values. If it helps, name it "Unused" as a reminder to ignore it.

- Thomas
OK, thanks Thomas, it was driving me around the bend trying to get rid of it ;)

1CM69
Normal user
Posts: 18
Joined: 20 Mar 2019, 19:31

Re: Access Device Info via Raspberry Pi

#20 Post by 1CM69 » 11 Apr 2019, 02:44

I have just tried the Python code that Thomas posted & it is extremely easy to use and manipulate to my needs.

Where I was getting stuck was the index value for each of the various Sensors, TaskName, TaskNumber etc... I thought that it was their postition in the JSON file but I couldn't make head nor tails of that.

Once I realised that these indexes relate to the the rows that the sensors appear in the device page of the web interface I was away.

The first row is index[0], second is index[1] and so on.

This also goes for any of the tasks for each sensor, the initial task is index[0], next is index[1] etc...

So if you have the Generic - System Info setup in row 2 of the Devices tab & classed as Task 2 you call it ['Senors'][1]

Then could have a number (max 4) fields setup in that sensor to collect data, for instance VCC & System Load, these are called TaskValues and have a Name & Value field.

So to get the value of the data held in VCC if VCC is at the top of the list it would be

Code: Select all

['Sensors'][1]['TaskValues'][0]['Value']
Thanks again Thomas

Regards..,

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Access Device Info via Raspberry Pi

#21 Post by ThomasB » 11 Apr 2019, 03:15

You're welcome! Glad to hear that JSON is working for you.

- Thomas

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests