We have moved at community.getvera.com

Author Topic: Getting data from a sensor in a file  (Read 12847 times)

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Getting data from a sensor in a file
« on: August 21, 2013, 03:08:27 pm »
Hello all,

I would like to get data (temperature for example) in a file , which another programm can read and use.

With the programm Blue iris it is possible to use data pulled from( I believe) a text file and overlay this on a camera view.
http://www.cam-it.org/index.php?topic=1965.0

Is it possible to get data from a temperature sensor and save it to a file on a computer?
Most important would be temperature readout from a fibaro universal sensor. But I have also some eversping temperature and humidity sensors.

What are the possibilities?

Thanks,
Cor

(someone allready used Blue iris macro's to display data from a device on a camera view?)

« Last Edit: August 21, 2013, 03:42:00 pm by Cor »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #1 on: August 21, 2013, 05:49:42 pm »
The following Lua in a scene should append each new reading to the file /var/temperatures.txt. You could schedule the scene by interval or trigger it by some appropriate means. <deviceID> is the ID of your temperature sensor. You would need some mechanism to clear the file whenever you have collected the data.

Code: [Select]
local devID = <deviceID>
local temperature = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",devID)
local file = io.open("/var/temperatures.txt", "a+")
file:write(temperature .. "\n")
file:close()

You could also make the file name /www/temperatures.txt and then you could inspect it from a browser using: <veraipaddress>/temperatures.txt
« Last Edit: August 21, 2013, 06:07:15 pm by RexBeckett »

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Getting data from a sensor in a file
« Reply #2 on: August 21, 2013, 11:57:15 pm »
@ RecBeckett;    I think that is very close to what I need.   Is it also possible not to append the data , but simply overwrite the data (2 digits) everytime.

Many  Thanks,
Cor


Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #3 on: August 22, 2013, 01:47:11 am »
If you change the file mode in the io.open(...) statement from "a+" to "w" then the file will contain only the last reading.

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Getting data from a sensor in a file
« Reply #4 on: August 22, 2013, 10:40:00 am »
WOW!!!! , Works great,I changed the code like you said:
Code: [Select]
local devID = 122
local temperature = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",devID)
local file = io.open("/www/temperatures.txt", "w")
file:write(temperature .. "\n")
file:close()

and when I click it it makes the temperature.txt file, which I can check via a browser ...GREAT!! :-)

Some more questions;

-When I made the scene it gave me in the middle top part in blue in the GUI an "error in LUUP file"    but it is working fine...... something to worry about?

- The plan is to have in totall 5 of those scenes running 24/7 every 10 minutes or so , will this affect the performance of vera3 and the activation of other scenes and devices ( This should be a low priority scene, If it is not refreshed I will be quite happy when it does in the next round).


I will conitnue to post the progress , maybe helpfull for other people who want to use this feature of Vera in Blue iris.

Cor

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #5 on: August 22, 2013, 01:34:41 pm »
Quote
-When I made the scene it gave me in the middle top part in blue in the GUI an "error in LUUP file"    but it is working fine...... something to worry about?
You should not get any errors when you save the scene. I pasted the Lua from your post into a new scene and I do not get any errors. It also works. Check you don't have any stray characters, spaces or blank lines in your Luup window.

Quote
- The plan is to have in totall 5 of those scenes running 24/7 every 10 minutes or so , will this affect the performance of vera3 and the activation of other scenes and devices ( This should be a low priority scene, If it is not refreshed I will be quite happy when it does in the next round).
This code will have very little impact on Vera performance.

Are you going to be logging to five separate files? I suppose you must otherwise you will step on your previous reading. Why do you need five different scenes? You could do it all in one scene.

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Getting data from a sensor in a file
« Reply #6 on: August 22, 2013, 02:22:36 pm »
@RexBeckett:   I am getting very close.  ;D   So much fun !

Attached allready a screenshot, it is the data on the left , the number "19"

Some more questions ;D

It seems that blue iris macro-option can only read from a local HD and not read the file from the network. Can the file be written to a computer on the LAN?  The computer which runs blue iris has Ip adress 10.0.0.20 and the file should be saved on C:\Blueiris\macro\temperature.txt

In the attachement you see it nicely ads the digits from the file ( as a test I copied the temperatures.txt to the computer which runs blue iris.)  Is it possible to add text via the LUUP code as well?, Ideally it would be  nice if the digits would be fetched and next to it "degrees C" would be written. like this (In blue iris I can only add text in front of the variable:
Code: [Select]
20 degrees C.

Your suggestion to use just 1 scene , that would make it much easier , That would mean to copy paste this LUUP code 5 times in a row with the different Device ID and txt file?     But I guess I cannot simply copy paste this code 5 times in a row  :-\   What would be the correct code?

Again , many many thanks,
Cor



« Last Edit: August 22, 2013, 02:24:26 pm by Cor »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #7 on: August 22, 2013, 04:03:24 pm »
Quote
It seems that blue iris macro-option can only read from a local HD and not read the file from the network. Can the file be written to a computer on the LAN?
That is not so easy. It depends on what facilities your other system has. You may be better finding a way to get your remote machine to pull the file from Vera's web server.

Quote
Is it possible to add text via the LUUP code as well?, Ideally it would be  nice if the digits would be fetched and next to it "degrees C" would be written.
That bit is easy:
Code: [Select]
file:write(temperature .. " degrees C\n")
Quote
But I guess I cannot simply copy paste this code 5 times in a row
If you are reading different temperature variables and writing to different files then you would, basically, paste the code five times and edit the device ID and file name.

Offline Brientim

  • Sr. Hero Member
  • ******
  • Posts: 2497
  • Karma: +78/-7
Re: Getting data from a sensor in a file
« Reply #8 on: August 22, 2013, 04:24:10 pm »
If you wanted to visualise the data recorded directly in vera, you could also write to a variable container.  The  VC app allows you to capture 5 variables which seems to also meet the requirements.

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Getting data from a sensor in a file
« Reply #9 on: August 23, 2013, 12:07:20 am »
@ RexBeckett,  The machine which runs Blue iris is an Intel I5 on the same LAN (computer is 10.0.0.20 , vera3 is 10.0.0.10). I can install a programm there to pull the file from vera ..... But how? , are there programms for that,or can that be done with a (simple) batch file? For the moment I was only able to copy the txt-file to the computer via winscp.

Tonight when I am back behind the computer I will try the "degrees C"  ... many thanks.

@ Brientim: thanks for the suggestion , doing it via the LUUP code is almost complete, I think I will continue using this ....  but that VC APP,I cannot not find that in the app list , it is a vera plugin?

Cor

Offline Brientim

  • Sr. Hero Member
  • ******
  • Posts: 2497
  • Karma: +78/-7
Re: Getting data from a sensor in a file
« Reply #10 on: August 23, 2013, 12:35:23 am »
You can find the app here:
http://apps.mios.com/plugin.php?id=1458

The forum is here:
http://forum.micasaverde.com/index.php?topic=9022.0

Using this you will still need to use luup to write to the variable container.

Offline SOlivas

  • Sr. Member
  • ****
  • Posts: 282
  • Karma: +1/-1
Re: Getting data from a sensor in a file
« Reply #11 on: August 23, 2013, 01:33:53 am »
Cor,
If you are using winscp, check out this on scripting it:

http://winscp.net/eng/docs/scripting

And here is an example of how to automate it:

http://blog.skufel.net/2011/12/automating-file-transfer-via-sftp-i-ftps-using-winscp/
« Last Edit: August 23, 2013, 01:37:31 am by SOlivas »
Vera 3 (1.5.622) / 9x GE/Jasco 45609 / 2x GE/Jasco 45612 / 2x GE/Jasco 45614 / 1x MIMO Lite
1x Twine (http://forum.micasaverde.com/index.php/topic,15617.0.html), DSC Security System, Honeywell  YTH8320ZW1007 Thermostat, 1x Fortrezz WWA-01, 1x CA9000 Wireless PIR Sensor

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #12 on: August 23, 2013, 03:57:12 am »
I think WinSCP is going to be your easiest route as @SOlivas suggests. You already have a connection profile established so a simple script to change local and remote directories and get the remote file should take care of it.
 
BTW: You could make your display string more compact by replacing degrees with the symbol:

Code: [Select]
file:write(temperature .. " \176C\n")
« Last Edit: August 23, 2013, 04:24:58 am by RexBeckett »

Offline Cor

  • Hero Member
  • *****
  • Posts: 1249
  • Karma: +8/-4
Re: Getting data from a sensor in a file
« Reply #13 on: August 24, 2013, 06:24:15 pm »
Slowly I am getting there.

@Brientim: cool plugin , I will not use it for this project , but I can use it for something else .... thanks for the tip.
@ SOlivas & RexBeckett ...... pffff it's difficult;

I am trying that script http://blog.skufel.net/2011/12/automating-file-transfer-via-sftp-i-ftps-using-winscp/ , But I need to change it a bit;
This is the script from that website:
Code: [Select]
1 option batch continue
2 option confirm off
3 open lab-net-01
4 lcd C:\FTP
5 synchronize both -delete
6 synchronize both C:\FTP /
7 exit

As I understand it, it syncs both ways....not good ,I need to sync it from remote to local.

I changed here and there some bits,
These are some options of the scipts: http://winscp.net/eng/docs/scriptcommand_synchronize#syntax
Code: [Select]
option batch continue
option confirm off
open vera3
lcd C:\blueiris\macro\files
synchronize local -delete
synchronize local C:\blueiris\macro\files /www\blueiris
exit

It is working  , but I am not sure if it is correct,can someone take a look at it (line 5 and 6)?
I want the files from vera to the local directory C:\blueiris\macro\files ( what does the line "synchronize local -delete" do? )

The other issue I have ,is the directory in vera. I created the directory blueiris in vera www\blueiris
( see attachment) , but when I run the LUUP code , the oatemp.txt is not created;
Luup code:
Code: [Select]
local devID = 122
local temperature = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",devID)
local file = io.open("/www/blueiris/oatemp.txt", "w")
file:write(temperature .. " \176C\n")
file:close()

Is there something wrong with the directory I created? or something else?
When I remove </blueiris>, it works and the txt file is created in the www directory, but I need it in a seperate directory since the script wants to copy otherwise all files.

Many thanks again ,
Cor
« Last Edit: August 24, 2013, 06:31:19 pm by Cor »

Offline RexBeckett

  • Beta Testers
  • Master Member
  • *****
  • Posts: 3891
  • Karma: +483/-12
Re: Getting data from a sensor in a file
« Reply #14 on: August 24, 2013, 07:01:36 pm »
To get a single file from Vera, try this:
Code: [Select]
option batch continue
option confirm off
open vera3
lcd C:\blueiris\macro\files
cd /www
get oatemp.txt
exit

How did you create your directory in /www? I just tried it out and it works just fine. I created the directory by clicking F7 - Create Directory and then changed the pathname in the Lua accordingly. Using a folder would allow you to synchronize multiple files with a single command but it wouldn't be any quicker than putting multiple get commands in the script. Personally, I would use individual get <filename> lines in the WinSCP script.

Edit: A new thought: Did you refresh WinSCP's display after running the Vera scene? A new file will not show-up in the list unless you click on the refresh icon or hit ctrl-R.

You don't need to delete the Vera files with the WinSCP script as they are overwritten whenever the Vera script runs. There is probably more overhead in deleting and recreating a file than in just rewriting its only record.
 
« Last Edit: August 24, 2013, 08:23:00 pm by RexBeckett »