We have moved at community.getvera.com

Author Topic: Writing sensor metrics to Ubidots  (Read 1080 times)

Offline dson

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Writing sensor metrics to Ubidots
« on: April 23, 2016, 08:11:21 pm »
Hi,

I'm new here, just got my first Vera (Plus) a few weeks ago and have had some fun writing my first scenes. I wanted to keep and graph my sensor readings - which Datamine2 is obviously great for. I did however want to have my readings online, especially to monitor things when away from home, so I spent some time getting Vera to write to Ubidots (www.ubidots.com). The choice to use Ubidots was purely down to personal preference - i looked at Thingspeak, but I don't like the fact it seems to be very closely linked with Mathworks, same thing for Xively which seems to have gone to a pure enterprise model after the Pachube / Letmein acquisition.

If anyone finds this useful my first go at a Ubidots update scene is as follows (code is maintained on https://github.com/dagsonstebo/Vera-Lua and the readme has a bit more information about configuration and usage):

Code: [Select]
--[[
Vera Lua scene for reading sensor variables and writing to Ubidots.

Configure Ubidots token in variables as well as all sensor readings in the
VERAVARS array, and use code in the Luup section of the Vera scene.
--]]

-- Variables
local UBIURL = "http://things.ubidots.com/api/v1.6/variables/"
local HEADER = "\"Content-Type: application/json\""
local UBITOKEN = "UbidotsTokenHere"
local FAILVAL = 0

-- Populate all variables to be read.
local VERAVARS = {
  { ID=10, NAME="KTTEMP", VARID="UbidotsVariableID1", SVC="urn:upnp-org:serviceId:TemperatureSensor1",  VAL="CurrentTemperature" },
  { ID=11, NAME="LRTEMP", VARID="UbidotsVariableID2", SVC="urn:upnp-org:serviceId:TemperatureSensor1",  VAL="CurrentTemperature" },
  { ID=12, NAME="KTHUMID", VARID="UbidotsVariableID3", SVC="urn:micasaverde-com:serviceId:HumiditySensor1",  VAL="CurrentLevel" },
  { ID=13, NAME="LRHUMID", VARID="UbidotsVariableID4", SVC="urn:micasaverde-com:serviceId:HumiditySensor1",  VAL="CurrentLevel" },
  { ID=14, NAME="KTLIGHT", VARID="UbidotsVariableID5", SVC="urn:micasaverde-com:serviceId:LightSensor1",  VAL="CurrentLevel" },
  { ID=15, NAME="LRLIGHT", VARID="UbidotsVariableID6", SVC="urn:micasaverde-com:serviceId:LightSensor1",  VAL="CurrentLevel" }
}

for k,v in pairs(VERAVARS) do
  -- Read each value.
  local RETVAL = luup.variable_get(v["SVC"], v["VAL"], v["ID"]) or FAILVAL
  -- Build curl command
  local UPDATEURL = UBIURL..v["VARID"].."/values/?token="..UBITOKEN
  local POSTDATA = "\'{\"value\":"..RETVAL.."}\'"
  -- Execute curl command.
  COMMAND = "curl -X POST -H "..HEADER.." -d "..POSTDATA.." "..UPDATEURL
  os.execute(COMMAND)
  -- Log each reading.
  luup.log("Ubidots: "..v["NAME"].." = "..RETVAL)
end

luup.log("Ubidots: update complete")

return true

Happy with any feedback on this. I'm fully aware the topic of writing plugins comes up on a regular basis in these forums and I'll consider this in the long run - whilst I've been doing IT and scripting for a couple decades I don't consider myself a developer so I'll take it one step at a time. A couple of things I've considered:
  • Use variable_watch to only write sensor readings when they are updated.
  • I also considered creating new sensor variables containing the Ubidots variable IDs, then loop over all variables (something like  luup.variable_get("ubidots", "ubivarid", #) ) and write each as required. I however found no way to to read each sensor reading using only the variable ID without knowing the service identifier, and whilst it seems you could potentially use luup.attr_get ('device_type', #) to parse and create this it doesn't seem particularly elegant.

If anyone has any suggestions I'd be very interested.

Regards,
Dag

Offline ConstantSphere

  • Beta Testers
  • Sr. Member
  • *****
  • Posts: 272
  • Karma: +16/-0
Re: Writing sensor metrics to Ubidots
« Reply #1 on: April 30, 2016, 01:25:04 pm »
Hi @dson - you know that it is possible to access dataMine2 remotely via the MCV forwarding servers. If you are on UI5 it's very straightforward, on UI7 you also need to install AltUI for it to work.
Vera Lite (UI5), 3xFibaro Universal Dimmers, Fibaro Roller Shutter, 2xFibaro Relay, 2xNorthQ Data Loggers, Secure Wall Thermostat, Secure Boiler Receiver, Remotec AC IR extender

Offline Mike Yeager

  • Hero Member
  • *****
  • Posts: 603
  • Karma: +11/-12
Re: Writing sensor metrics to Ubidots
« Reply #2 on: July 04, 2016, 12:06:54 pm »
How difficult would it be to modify this to post to Xively? I'm not familiar with Luup (Lua) code as of yet so I can't even begin to decode what you've written but I'm trying. Any further progress towards updating only when the sensor updates?