Pushing Data to Data.SparkFun.com
This Tutorial is Retired!
This tutorial covers concepts or technologies that are no longer current. It's still here for you to read and enjoy, but may not be as useful as our newest tutorials.
Raspberry Pi (Python)
The multi-talented Raspberry Pi has no shortage of tools for posting data to web servers. You could use a shell script and cURL, similar to what we did on the Yun, but in this example we'll use Python, which pairs nicely with the Pi's RPi.GPIO module.
If you haven't used Python on the Pi to read-and-write to I/O pins, check out our Raspberry gPIo tutorial. This tutorial leans on that to read the status of a couple input pins.
Example Circuit
Our simple example circuit changes a little bit in this case. The Pi doesn't have analog inputs, so we can't plug the photocell in, but we'll still use a button and switch:
The button is connected to the Pi's pin 22 (that's the Broadcom chip pin), and the switch output is connected to pin 23.
If you have a Pi Wedge, it makes the job of wiring to the Pi much easier.
Example Script
Right-click and Save Link As to download the example python script, or copy and paste from below. We've named our file phant-raspi.py
, if you're making the file on your own. Place it in an easily-accesible directory on your Pi.
import RPi.GPIO as GPIO import time import httplib, urllib import socket server = "data.sparkfun.com" publicKey = "6JZbNolApzF4om2l9yYK" privateKey = "Ww0vPW1yrkUNDqWPV9jE" fields = ["light", "switch", "name"] buttonPin = 22 switchPin = 23 myname = socket.gethostname() GPIO.setmode(GPIO.BCM) GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) print("Here we go! Press CTRL+C to exit") try: while 1: if not (GPIO.input(buttonPin)): print("Sending an update!") data = {} data[fields[0]] = time.strftime("%A %B %d, %Y %H:%M:%S %Z") data[fields[1]] = GPIO.input(switchPin) data[fields[2]] = myname params = urllib.urlencode(data) headers = {} headers["Content-Type"] = "application/x-www-form-urlencoded" headers["Connection"] = "close" headers["Content-Length"] = len(params) headers["Phant-Private-Key"] = privateKey c = httplib.HTTPConnection(server) c.request("POST", "/input/" + publicKey + ".txt", params, headers) r = c.getresponse() print r.status, r.reason time.sleep(1) except KeyboardInterrupt: GPIO.cleanup()
Now open up the terminal, and navigate to the directory where your script lives, and run it like this:
pi@raspberrypi ~/code/phant-pi $ sudo python phant-raspi.py
With the script running, when you connect the button pin (22) to ground it should send an update. A handful of messages will appear each time a message is sent. You want to see "200 OK" after the "Sending an Update!" message. That should indicate that the HTTP POST was succesfully sent.
To customize it to your own Phant stream, edit the public, private, and field keys in this section:
################# ## Phant Stuff ## ################# server = "data.sparkfun.com" # base URL of your feed publicKey = "6JZbNolApzF4om2l9yYK" # public key, everyone can see this privateKey = "Ww0vPW1yrkUNDqWPV9jE" # private key, only you should know fields = ["light", "switch", "name"] # Your feed's data fields
You may also want to modify how the data for those fields is gathered. That happens near the very beginning of the lone if
statement. Field 0 ("light") contains the current time/date, field 1 ("switch") contains the status of our switch, and field 2 ("name") contains the localhost name of your Pi.