Thingspeak & EmonCMS

14 replies [Last post]
AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012

Brett,

Would you consider adding support for Thingspeak - Its an opensource Xively alternative.

https://thingspeak.com/

API Reference - https://uk.mathworks.com/help/thingspeak/api-reference.html

thanks

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Andrew,this should be doable

Andrew,

this should be doable with a plugboard script. 

I am worried about the direction xively is heading too so I might have a crack at this myself when time allows. 

I'll see if I can knock up a script at the weekend

 

Garry 

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Hi Garry,That sounds like a

Hi Garry,

That sounds like a plan

Thanks

Andeew

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Gary,I can give you some

Gary,

I can give you some hellp.  My free time at the moment is stil consumed with work.
Companies don't seem to realize that laying people off may reduce expenses however it does not reduce the work !

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks Brett, I'm sure you

Thanks Brett, I'm sure you will be able to tidy my efforts up a bit when I'm done.

I now have a script uploading data to thing speak, will stress test it and post at weekend if all remains well.

thingspeak does look good, especially the ability to multi plot, etc.

I understand your work issues, I'm going through a bit of that myself. Who needs hobbies and play time anyhow :(

 

Garry

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks Brett, I'm sure you

Thanks Brett, I'm sure you will be able to tidy my efforts up a bit when I'm done.

I now have a script uploading data to thing speak, will stress test it and post at weekend if all remains well.

thingspeak does look good, especially the ability to multi plot, etc.

I understand your work issues, I'm going through a bit of that myself. Who needs hobbies and play time anyhow :(

 

Garry

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Sorry, posting from ipad so

Sorry, posting from ipad so file is posted long hand in post - not attached.

please treat as beta and change API key before use. Also setup your fields in thing speak beforehand.

any probs/questions let me know!

attached is applet to upload to thing speak  

 

garry

 

 

--[[ 

    xAP Caching and Thingspeak interface

--]]

 

module(...,package.seeall)

 

require("xap")

require("pl.stringx").import()

require("pl.pretty")

socket = require("socket")

 

info={

   version="1.0", description="xAP to Thingspeak applet"

}

 

local fields = {

{source = "dbzoo.RaspPi.Plugboard:HouseAveTemp",class = "xAPBSC.event",section = "input.state",value = "text"},

{source = "dbzoo.RaspPi.Controller:1wire.1",class = "xAPBSC.event",section = "input.state",value = "text"}

}

 

local APIKey = "xxxxxxxxxxxxxx"

 

local vfs = {

   filter={},

   data={}

}

for i,k in ipairs(fields) do

vfs.filter[i] = xap.Filter{["xap-header"] = { 

   source=fields[i]["source"],

   class=fields[i]["class"] }}

end

 

function updateCache(frame)

for i,k in ipairs(fields) do

if frame:getValue("xap-header","source") == fields[i]["source"] then

vfs.data[i] = frame:getValue(fields[i]["section"],fields[i]["value"])

end

end

end

 

function send()

local host, port = "api.thingspeak.com", 80

client = socket.tcp()

client:connect(host, port)

 

local fieldtext = ""

for i,k in ipairs(fields) do

if vfs.data[i] then 

fieldtext = fieldtext.."&field"..i.."="..vfs.data[i]

else end

end

 

client:send("GET /update?api_key="..APIKey..fieldtext

        .. " HTTP/1.1\r\n"

        .. "Host: api.thingspeak.com\r\n"

        .. "Connection: close\r\n"

        .. "Accept: */*\r\n"

        .. "User-Agent: Mozilla/4.0 (compatible;  Lua; Windows NT 5.1)\r\n"

        .. "\r\n")

 

end

 

function init()

   for _,k in pairs(vfs.filter) do

      k:callback(updateCache)

   end

 

xap.Timer(send, 60):start(false)

end

 

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Excellent work Garry :)my

Excellent work Garry :)

my quick test feed for the 1Wires - https://thingspeak.com/channels/87676

I had to create the channels / fields first to access an additional API key for writing to the channel but after this the feed data appeared

thanks

Andrew

 

AndrewJ
Offline
United Kingdom
Joined: 22 Nov 2012
Garry,I hope you dont mind, I

Garry,

I hope you dont mind, I made a few modifications to your script to allow it to be used with OpenEnergyMonitors EmonCMS http://www.emoncms.org

Emoncms contains some very nice built in apps for historical power logging plus a matching android app.

As above set you own API key / Endpoints / Update timer.

Thanks

Andrew

AttachmentSize
emoncmsApplet.lua 2.4 KB
garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Not at all. Glad it was of

Not at all. Glad it was of some use. 

kim
Offline
Throwleigh, United Kingdom
Joined: 1 Dec 2010
Help sought

I am using the lua script for reporting to Thingspeak, but it doesnt seem to work with on/off states from the pir sensor.I can find the code where it is converted for xively 

 

int now = 0;
 if(strcasecmp("on",value) == 0) { // Issue 27
 f = 1;
 now = 1; // A boolean state should never be cached.
 } else if(strcasecmp("off",value) == 0) {
 f = 0;
 

now = 1;

 

 

But I don't know how to convert this to lua.  Could an expert assist me?

brett
Offline
Providence, United States
Joined: 9 Jan 2010
I think you need an amendment

I think you need an amendment to Gary's code to handle on/off (this code is untested)

function updateCache(frame)
 for i,k in ipairs(fields) do
   if frame:getValue("xap-header","source") == fields[i]["source"] then
     vfs.data[i] = frame:getValue(fields[i]["section"],fields[i]["value"])
     if vfs.data[i] == "on" then vfs.data[i] = 1 end
     if vfs.data[i] == "off" then vfs.data[i] = 0 end
   end
 end
end

kim
Offline
Throwleigh, United Kingdom
Joined: 1 Dec 2010
Thank you...

I have tried this but still not appearing.  I can see the result coming through on xfx viewer and the message gets through to xively (https://personal.xively.com/feeds/2128585885 - feeds 7 and 8) but not on thingspeak (which I have made public for now https://thingspeak.com/channels/399026  - same feeds ).  Attached is my applet with amendment.

In case it was a latching problem I tried changing both on and off to 1's and also tried changing 1 to "1" but neither works.

At the moment I am using ftp for the changes and just rebooting the machine.  Maybe next I shall try stopping and starting the applet using ssh and see if I get any errors.

Unless anyone else has any bright idea? .....

Kim

AttachmentSize
Thingspeak2Applet.lua 2.55 KB
kim
Offline
Throwleigh, United Kingdom
Joined: 1 Dec 2010
how irritating is this...

just after I posted I did a final check of what was ftp'd and it was the original file without amendment.  So it DOES work exactly as posted by Brett.

So much for doing lots of checks before posting.  Sorry.

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Thanks For the modification

Thanks for the modification Brett. 

I only use it for charting temps so hadn't run into this issue.

Glad it's working for you Kim.

Garry

Hardware Info