7.1 Lab: Uploading Sensor Data to Visualization Platforms

In this lab we will upload the data that we gather from the sensors to the xively.com sensor data broker platform. First we will create an account and a device in xively.com. Then we will write a json file and we will send it using curl to xively. We will need an API endpoint url and an API key that will be used as parameters of curl. After we have verified that we can send data to xively.com, we will configure the XBees and build the circuit.

If we want to use the "escaped" mode of operation, we must set the API mode to 2. We also need to especify "escaped=True" in our python program. We will write a python program that receives the data from the XBees, writes a json file, and finally invokes curl with the required paramters to send the data to xively.

Now our sensor data i publicly available on the internet. It can be consulted by others and used by other applications.

Congratulations for getting this far. We have discovered some of the possibilities of Wireless Sensor Networks and are now ready to undertake more ambitious projects.

Don't forget to stay in touch with the community and share your new adventures in Wireless Sensor Networks. And please help those that are beginning in their first their first hands on experience with Wireless Sensor Networks.



cURL request to upload test data to Xively:

curl --request PUT --data-binary @feed_update_payload.json --header "X-ApiKey:insert-your-API-key-here" --verbose https://api.xively.com/v2/feeds/insert-your-ID-here

feed_update_payload.json

{
  "version":"1.0.0",
  "datastreams":[
      {"id":"one",
       "current_value":"10.00"},
      {"id":"two",
       "current_value":"20.00"},
      {"id":"three",
       "current_value":"30.00"}
  ]
}

Example code to upload data to Xively with Python:

# Derived from code by Alejandro Andreu
import commands
import json
import serial
import time
import os
from serial import SerialException
from xbee import ZigBee

print 'Asynchronously printing data from remote XBee'

serial_port = serial.Serial('/dev/ttyUSB0', 9600)

def print_data(data):
    """
    This method is called whenever data is received.
    Its only argument is the data within the frame.
    """
    print data['samples'][0].keys()[0]

    # Create a JSON file and fill it with the received samples
    json_data = {}
    json_data['version'] = '0.2'
    json_data['datastreams'] = ()
    json_data['datastreams'] = json_data['datastreams'] + ({'id': data['samples'][0].keys()[0], 'current_value': str(data['samples'][0].values()[0])},)
    # Add more datastreams if needed
    with open('Xbee2Xively.json', mode='w') as f:
        json.dump(json_data, f, indent = 4)
    # Upload information to Xively. Use your own Api Key and feed identifier
    os.popen('curl --request PUT --data-binary @Xbee2Xively.json --header "X-ApiKey:insert-your-API-key-here" --verbose https://api.xively.com/v2/feeds/insert-your-ID-here')

zigbee = ZigBee(serial_port, escaped = True, callback = print_data)

time.sleep(300)

zigbee.halt();
serial_port.close()

Activity

Adding more sensors to your project or creating and application that uses the data that we store in xively.com are just two examples of how to extend the example project. Use your imagination to create your own personal project and tell us about it in the comments.

Don't forget to apply for the badge when you are done!

Uploading Sensor Data and Visualization

View Projects 2014 Edition


Comments

comments powered by Disqus