Log temperature via REST request

Functionality and usage of the OpenNethome Server
Post Reply
tobbe
Posts: 17
Joined: Mon Dec 15, 2014 8:14 pm

Log temperature via REST request

Post by tobbe »

Hi

i was wondering if it would be possible to log temperatures via a REST request?
I have several wireless sensors that log to a MySQL database and would like to see those sensors in Opennethome as well.
My idea would be to run a cron script on the Opennethome server, that requests that last value from the database and then do a wget request to Opennethome, something like http://localhost:8020/rest/items/100/update/22.25 (100 = item number, 22.25 = temperature)

Or if REST is not an option, is there some other way to do this?

Regards,
Tobbe
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Log temperature via REST request

Post by stefangsbb »

Hi Tobbe!

There is a HomeItem designed specifically for this case. It is called GenericThermometer http://wiki.nethome.nu/doku.php/genericthermometer. This Item is updated by sending a specific event to it and it will present the latest value sent along with some update time information.

Events can currently not be sent via the REST interface (maybe this should be added?), but you can send them via the TCPCommandPort-Item. There are examples in the GenericThermometer documentation page (link above), but basically you can send it via this kind of script:

Code: Select all

echo 'event,Temperature_Message,Address,1,Value,230' | nc -u -w 0 127.0.0.1 8005
Hope this helps!

/Stefan
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Log temperature via REST request

Post by stefangsbb »

I realized that it would be easy to add the possibility to do the updates via REST instead, so I implemented that. I added a new attribute called "RawValue" in the GenericThrermometer-Item which you can update and that will update the value of the Item, just as via the events. If you read it it always return an empty string and if you write an empty string, the current value is not affected. The REST call to do this update would be:

Code: Select all

PUT http://127.0.0.1:8020/rest/items/153
{
    "attributes": [
        {
            "name": "RawValue",
            "value": " 222"
        }

    ]
}
The update is in the nightly build - hope this helps.
/Stefan
tobbe
Posts: 17
Joined: Mon Dec 15, 2014 8:14 pm

Re: Log temperature via REST request

Post by tobbe »

Hi

There seems to be something odd about the nightly release 3.0-c10e9.
When I ran the update script if erased half of my config.xml and refused to start the gui of opennethome.
Apparently the script does not backup the config file, so I lost about 3 months of refinements :(
Well, not your fault, I should have made backups...

Well, back to the drawing board, as they say

Regards,
Tobbe
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Log temperature via REST request

Post by stefangsbb »

Ohh, I am sorry to hear, this sounds really bad!!
I have upgraded my own home server with this release, and I have not seen any problems so far.

I really want to know what went wrong here, because upgrades should not fail like that. Could you please tell me exactly how you did the upgrade and what versions you are running?

I did my upgrade with the built in upgrade command:

Code: Select all

sudo nethome upgrade-nightly
If you got any error logs when starting up after the upgrade, could you please share them as well?
tobbe
Posts: 17
Joined: Mon Dec 15, 2014 8:14 pm

Re: Log temperature via REST request

Post by tobbe »

Hi

I might know what went wrong, I used the old upgrade method, the upgrade_nethome_nightly.sh script.
I was not aware of the nethome upgrade-nightly command.
Really should pay more attention to the instructions...

My old version was the 3.0-0022a** release
I logged in via SSH and ran the upgrade_nethome_nightly.sh script, which is what I have used to upgrade to the nightly before.


Regards,
Tobbe


The logfile:

Code: Select all

2018.09.30 20:12:01.354 INFO:**Starting HomeManager 3.0-c10e9** (HomeManagerStarter 0, TID1)
2018.09.30 20:12:01.552 INFO:Logging to: /var/log/nethome/HomeManager%g.log (HomeManagerStarter 1, TID1)
2018.09.30 20:12:01.717 WARNING:Prefs file removed in background /home/nethome/.java/.userPrefs/nu/nethome/home/impl/prefs.xml (FileSystemPreferences$6 2, TID1)
2018.09.30 20:12:01.731 INFO:Loading Items from /etc/opt/nethome/config.xml (HomeItemFileLoader 3, TID1)
2018.09.30 20:12:06.40 WARNING:/etc/opt/nethome/config.xml is not well-formed. (HomeItemFileLoader 4, TID1)
2018.09.30 20:12:08.518 INFO:Activated 3 of 3 Items (HomeServer 5, TID1)
stefangsbb
Site Admin
Posts: 313
Joined: Sun Nov 30, 2014 2:16 pm

Re: Log temperature via REST request

Post by stefangsbb »

Hmm, the best way to do the upgrade is via the upgrade command, but I don't think using the script was the cause of your problem. The logs say that the config.xml is not "well formed" which indicates that your config file was corrupted somehow prior to starting the upgraded server. Did you have any unexpected power cuts to the pi prior to or during the upgrade?
tobbe
Posts: 17
Joined: Mon Dec 15, 2014 8:14 pm

Re: Log temperature via REST request

Post by tobbe »

Not that I know of.
Been running the same setup since 2014, and run several nightly upgrades with the same script.
Most likely it was just a very unlucky fluke.
I have now rebuilt my setup and make damn sure I have a backup before doing upgrades in the future! :)

Will try to implement the REST logging, but not tonight anymore, too busy at work.

Thanks for your help anyway

Tobbe
tobbe
Posts: 17
Joined: Mon Dec 15, 2014 8:14 pm

Re: Log temperature via REST request

Post by tobbe »

Well, went back on my word.
I can now update the temperature via a curl command, still need to write a php call that fetches the latest value of a certain sensor, but that is a different issue not related to opennethome.

This is the Curl call I used , just in case anyone else is wondering

Code: Select all

curl -X PUT -H "Content-Type: application/json" -d '{"attributes": [{"name": "RawValue","value": "41.76"}]}' http://127.0.0.1:8020/rest/items/334
Since the value is an actual temperature value I use a K value of 1.0
The value in the example is 41.76, that translates into 41.8. I assume opennethome does not like 2 decimal temperatures?
The 334 on the end just happened to be my generic temp gauges ID

Great work as usual usual Stefan!

Tobbe
Post Reply