Saturday, January 18, 2014

Code for Xively Egg Feed

So, I'm going to jump ahead a little bit. Because I'm lazy / short on time and so forth.

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.

Wednesday, January 15, 2014

Air Quality Egg -- Part 3

For this part we're going to open up the Arduino IDE, take a look at the settings, open up the Base Station code and update the Base Station Egg.

Open your Arduino IDE and choose the appropriate file:


Using the IDE to update an Arduino is well documented. When you open the .ino file the IDE will open all the of the project files. You do need to set the Arduino as "Arduino Uno". The link in the previous post covers how to do all that but basically you'll need to choose the board you want:


Now that we're to this point it's time to check some things. First, let's compile the Egg Base Station code (AQEBase.ino project). There's a checkmark that verifies the code.


After you click this wait for a bit while the code is compiled. Then check the results. Unless something is terribly wrong you'll see:


Now that that's done go ahead and upload the sketch (what Arduino calls it's projects) to your Egg Base Station. Just click the arrow.


You'll see the progress in the bottom of the window.


And if your device is actually connected on the correct COM port you'll see success!


My experience has been that if you check the serial ports listed under Tools > Serial Ports you'll see the correct port listed. If you have more than one FTDI or COM device you may need to unplug the others to find out which is your Egg.


To do the Remote Station Egg you'll have to take the sensor board off. With all pin-and-header boards be careful. Slowly and gently rock the board ever so slightly while pulling up.

Also I should of told you this before but you don't have to have your Egg plugged into AC power when you have the FTDI cable plugged in. The Egg will pull power from USB. I know at least one other person realized that the power adapter is removed when you take the shield off of the Remote Station Egg. That's OK.

To update the Remote Station do the same stuff we did above only using the AQERemote.ino project sketch.

Next I'm going to lay out a bit of a project plan for what I'm doing with my Eggs. We'll connect the Egg data to your own Xively account and start by writing some Python code to pull that data for your own use. After that the project gets a bit more ambitious but should be fun.


-------

Tuesday, January 14, 2014

Air Quality Egg -- Part Two

Now we start to get into some details.  For this part we're going to cover quite a bit.

  • Connect the Egg (both the Base and the Remote Stations) to my computer using FTDI cable
  • Get the AQE code from GitHub and upload that software onto both Eggs using the Arduino IDE
  • Look at the serial output
  • Hack the Egg, just a bit
Connecting the Egg to FTDI

What is FTDI?
FTDI is a company called "Future Technology Devices International". It's also the name of a chipset (from that company) that converts serial RS-232 connections to USB. This is really handy because computers have USB connections and boards like the Arduino, the Nanode -- even network devices like Cisco and Juniper switches and routers -- all have serial connections.

The ends of the FTDI cable I use look like this:

The first thing to do is hook up the FTDI cable to the computer and then the Egg. Actually we'll have to hook up the cable to Nanode boards for both the Base Station and Remote Station Eggs.

You can watch the videos from Wicked Devices to see how to update the main boards of your Egg.

Setup and configuration
Downloading the Arduino environment and the Egg code from Github
Reprogramming your device
[These videos are best found at the Wicked Devices AQE FAQ page: AQE FAQ.]

If you look at the "How do I update my egg" section you'll also see there's a special bit about using an ISP to update the "extra" things attached to your Egg's Nanode boards. I'll cover some of that later, too. Mostly to walk through the instructions so people can get another point of view. For now we're going to keep it simple though. Feel free to read up on that of course!

I'll try to cover details here that might help. In the future I plan on rewriting a lot of this as a How-To guide after taking a look at what is confusing about the process for newcomers.

The USB end plugs into one of your computer's USB ports. You may or may not see some information about "installing new hardware". When you do you can see what COM port the cable is assigned to. I will describe more about figuring out how your cable is assigned to a communications serial port later -- I'll describe both Ubuntu and Windows 7.

As it turns out during the next steps as long as you're only using a single FTDI cable guessing (using the Arduino IDE) which port to use is as easy as anything. Feel free to Google for more details -- be aware that sometimes the COM port will show up oddly in Windows device manager.

Now plug the cable into the Egg's Base Station Nanode board. Remember from the first post:

It's important that the right color be on the proper side of the connection. The writing is small so I've enlarged it in the next picture. The label on the board says "black" and that's the side the black wire needs to be on.



Now your Egg is connected to your computer!

The next part is to get the Arduino IDE working. Best to just head to Arduino -- Getting Started and follow their instructions for your platform.

For the moment I'm also going to assume you also followed the videos from Wicked Devices for getting their code from GitHub. For clarity I'm going to go over what files to use to get started though.

Get the code from the Air Quality Egg page [currently at https://github.com/jmsaavedra/Air-Quality-Egg]. Look for the "Download Zip" link.

Unzip the files somewhere you want to work with them. Once that's done take a look:

First off the folders for the project:

As noted for starters you'll only use one of the two folders. The AQEBase folder is for the Base Station Egg -- the one plugged into Ethernet. The AQERemote is for the Remote Station Egg that has all the sensors connected to it.

In the next steps we'll use the code in each of those folders. Here's the files in the AQEBase folder. Note you'll be opening the .ino file with your Arduino IDE.

And the same for the AQERemote:

In the next post I'll show the Arduino IDE and go over the few steps it takes to update your Egg. Again, I'm going to assume you watched the videos above. At a later time I may post details to help clarify confusing info.

-------------------------


Saturday, January 11, 2014

Air Quality Egg -- Part One

In an effort to keep stuff going I'm going to post little snippets and cap it all off with a kind of big review.

The Air Quality Egg (AQE) is definitely a hacker toy. There's a lot of stuff to play with once you get past the really basic results you get "out of the box". I actually started immediately by hacking it so I can't say for sure that it worked out of the box.

It takes me a little longer to get to the meat of the topic but I want to record my journey as much as anything. So sorry if a lot of this is a bit too obvious or simple. Hopefully, though, as I get more of the code and stuff that I've already put together out here this could be a good starting place for novice hackers. Face it, with electronics I'm still pretty newbie.

The AQE is built on the Nanode -- an Arduino compatible microcontroller. It has a built-in Ethernet because Nanode stands for "network application node".

Part One: Unpack
After all the stuff was unpacked I did a quick glance of the boards.

Figure 1: Overview

The "Egg Base Station" is the part that connects to the Internet and receives reports from the "Egg Remote Station" via radio signals. The "Egg Remote Station" has all the sensors that collect data.

My setup includes NO2, CO, Temperature, Humidity and Dust sensors.

Like I said, I took a really fast glance at the boards.
Figure 2: Egg Base Station
Figure 3: Remote Station and Dust Sensor (connected to Egg Remote Station)
Part Two: Hook it to My Computer and Update the Egg's Software (next)





Monday, January 6, 2014

Random Python of the Morning

x = range(-10,10)
y = []

a = 3 # the positive or negative curvature
h = 0 # horizontal offset
k = 11 # vertical offset

# y = a * (xi – h)**2 + k
# y = a * xi ** 2 + b * xi + c

for xi in x:
    y.append(a * (xi + h)** 2 + k)

pylab.plot(x,y)

grid()
xlabel('$x$')
ylabel('$y$')
title('Parabolic Equation')

New Year, New Stuff

I'll be updating now with my work with Air Quality Egg. Although this will be my focus for awhile it's going to involve a lot of things -- programming Arduinos, Python data analysis, tying to an online data service and of course coming to understand more about the sensors and device I'm using.

Hopefully this will give me a focus for blogging, too. I've already started hacking it so I have a few observations I can share.