Create an Xively account and add a device following their instructions (https://xively.com). I can cover this later but I think it's pretty well covered all over the place.
Next, add some code to your Egg Base Station code.
Add the following code to your Arduino project. I put this function into the sensors.ino file of the AQEBase.ino project.
At the bottom just paste the following -- be sure to add your own Feed and API key:
// Comment
char xively_website[] PROGMEM = "api.xively.com";
#define FEED "11111111" // Feed ID
#define APIKEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // Put Your APIKEY here
Stash xively_stash;
void sendXively(char *channel, int value){
Serial.println(F("Sending data to Xively"));
Serial.print(F("Data Type: "));
Serial.write(channel);
Serial.println(" ");
Serial.print(F("Data Value: "));
Serial.println(value);
// by using a separate stash,
// we can determine the size of the generated message ahead of time
byte sd = xively_stash.create();
xively_stash.print(channel);
xively_stash.print(",");
xively_stash.println(value);
xively_stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"X-PachubeApiKey: $F" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
xively_website, PSTR(FEED), xively_website, PSTR(APIKEY), xively_stash.size(), sd);
// send the packet - this also releases all stash buffers once done
ether.tcpSend();
Serial.println(F("Data sent"));
}
The code above is drawn from: Wicked Device: Nanode Gateway Remote Tutorial 3
Next here's some python you can use to pull your data. In my case I created a feed using the basic labels from the Egg so they come through as "CO", "CO_raw", "CO_r0", etc. I also have a dust sensor. Once you have the feed set up you'll have your Feed ID and API keys (see above). I recommend creating another API key that is read-only -- use this in your python script and you can share the code with others without fear of anything other than someone reading your data.
import json
import urllib
import httplib
xivelyhost = 'api.xively.com'
feedurl = '/v2/feeds/1703619548'
feedid = '170361954'
apikey = 'EsTjqWQlHyj69kBKBYbtoanldlKxAb7HalVxzW5ef2V5x296' # Read-Only Key
#
# Example 1: Pulling JSON Feed from my read-only APIKey URL
#
#params = urllib.urlencode({'datastream': 'CO,NO2,Dust,Temperature,Humidity'))
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "application/json", "X-ApiKey": apikey}
http = httplib.HTTPConnection(xivelyhost)
http.request("GET", feedurl , '', headers)
response = http.getresponse()
print(response.status, response.reason)
eggdata = json.load(response)
http.close()
print("Example 1")
#print(eggdata)
#for datastream in eggdata['datastreams']:
# print("{")
# for key in datastream:
# print key,datastream[key]
# print("}")
datastream_keys = ('CO','Dust','NO2','Temperature','Humidity')
eggdict = {datastream['id'] : datastream['current_value'] for datastream in eggdata['datastreams'] if (datastream['id'] in datastream_keys)}
print(eggdict)
I provide this code here in the hopes it's helpful. There's not a lot of great documentation on interacting with Xively in a simple way. Frankly the Xively API library is just an ugly way to go about reading the data (see my pastebin: http://pastebin.com/y8RDP3Wi). I'll cover more on this code later -- and we'll expand on it. I wanted to get some more technical stuff out here, though, for those tired of the really simple introduction and overview.
No comments:
Post a Comment