Pachube applet

11 replies [Last post]
brett
Offline
Providence, United States
Joined: 9 Jan 2010

More of a proof of concept that anything useful.

--[[
    Pachube push via LUA
--]]

module(...,package.seeall)

require("xap")
require('ltn12')
local socket = require("socket")
local http = require("socket.http")

info={
   version="1.0", description="XAP LUA to Pachube"
}

local pachubeID = 13058
local pachubeKey = "xxxxx"
local pachubeURL = "http://api.pachube.com/v2/feeds/"
local pachubeTbl = {
  ["dbzoo.cheese.jeenode:3.temp"] = 1,
  ["dbzoo.cheese.jeenode:4.temp"] = 2
}

function pachubeDispatch(frame)
  local id = pachubeTbl[frame:getValue("xap-header","source")]
  local value = frame:getValue("input.state","text")

  local json_data = string.format(
[[{
"version":"1.0.0",
"datastreams": [ {"id":"%s", "current_value":"%s"}]
}]], id, value)

  local response_body = {}
  local response, status, header = http.request{
                method = "PUT",
                url = pachubeURL .. pachubeID,
                headers = {
                           ["Content-Type"] = "application/json",
                           ["Content-Length"] = string.len(json_data),
                           ["X-PachubeApiKey"] = pachubeKey
                          },
                source = ltn12.source.string(json_data),
                sink = ltn12.sink.table(response_body)
              }
end

function init()
  local f
  for k in pairs(pachubeTbl) do
     f = xap.Filter()
     f:add("xap-header","source",k)
     f:add("xap-header","class","xAPBSC.event")
     f:callback(pachubeDispatch)
  end
end

mark_baldwin
Offline
Blackburn, United Kingdom
Joined: 19 May 2012
Strange endpoint name

Why cheese? :)

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Cheese indeed.  This is the

Cheese indeed.  This is the name of a x86 linux box where i'm running xap-serial with a base node plug into it.  My Livebox has a 915Mhz basenode and Cheese has a 868Mhz basenode.

I'm going to replace the 868Mhz basenode and xap-serial on cheese with a standalone Nanode Ethernet gateway soon.

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Bit of help, please

Hi Brett

 

Just took a look at this and tried to mod it for a couple of my ID's on the new Xively site....I appear to have not understood the mechanics and I can't get anything to show on the site....I've attached my moded version for you to hopefully point out where I'm going wrong...

Hope all is well with you and yours in sunny Auz....

regards

 

Elliot

AttachmentSize
CXPushtoXively.lua 1.48 KB
brett
Offline
Providence, United States
Joined: 9 Jan 2010
Elliot are you doing this for

Elliot are you doing this for your own amusement?   You know that xap-xively which is built in does all this for you.

The LUA code I posted is just something I did for fun its not required for feeding xively.

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Have a need!

Hi Brett...

Thanks for responding - and yes, I do realise that you've already put Xively posting to multiple Feeds into the HAH...but, for whatever reason it takes an inordinate amount of time just to add one more line to the Xively table - and I keep getting corruption of my backups, so I wanted a programatic means of setting up alternate Feed posting.

 

As an example, I've got 7 lines being posted to in my Xively table and to add one more line takes upto 8 mins!(from hitting the save button to getting control back)...

 

Many times (at least one in three) when I do a backup the data gets corupted and I lose most of the Xively table - fortunately I have a 'Master Backup' which is good, so I revert back to that and have to re-enter my additons again!...

 

Also - the piece of code looked tempting and I need more practice in lua so I thought I'd have a go!

 

Thanks...

Elliot

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Extends the in-built Xively feeder

Hi Brett

My other problem is that I'm posting to several Xively Feeds, each with their own API key and ID layout.  I couldn't see how to attach an alternative API key in the HAH so I was looking to this bit of code to perform my multi-feed requirement....if I can get my head round the lua script!  Still struggling, I'm afraid...any help greatly appreciated...

EJ

brett
Offline
Providence, United States
Joined: 9 Jan 2010
With Pachube (and I assume

With Pachube (and I assume Xively) you're account only needs on API ID.  What changes is the FEED number.

Each SET of things you want to group together you give them the same FEED ID.  In Xively you pre-create those group and obtain the feed number which you can then insert in the WEB GUI for the xively UI page.

If you want to use the LUA code and you have separate accounts each with their own login API ID then just clone the LUA script and have multiple copies running.  That'll work too.

Brett

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Tried that!

Hi Brett

 

I'd realised the first bit (and am happily posting to my main Xively Feed from within the HAH - although I still have  problems with scrambled backup ini files whenever I try and add new data to the tables and then save a backup!!).... and that's why the lua looked so useful, 'cos I have three other Xively Feeds, each with their own API Key and ID structure, that I'd like to post to.

 

My original question stands - I've moded the original lua for my own criteria, but nothing is being posted...can you see if you can spot what I'm doing wrong (the code is attached to my first post).

 

Thanks for all your efforts - the HAH and LUA are an absolute beaut!

 

Cheers....EJ

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Follow on

Also - as a follow on from the PUT lua, and as a bit of a preemptive strike  -  a GET lua structure might be very useful as well...I'll have a look at the code supplied for the recovery of graphs from Xively, that'll give me a starting point unless you have a more structured code segment that you'd prefer we use?

 

EJ

EJ-Ambient
Offline
Ringwood, United Kingdom
Joined: 5 Aug 2011
Doh!!!!!

Such a simple thing.....I'm an idiot!...I forgot to rename the lua with Applet at the end....it now seems to be working...sorry to have been a pain!

 

Anyway, I can concentrate on the GET side now.....deep joy!

 

Thanks (from a very red-faced EJ)

brett
Offline
Providence, United States
Joined: 9 Jan 2010
DOH.

DOH.

Hardware Info