Posting to Sensor-cloud.com with Python for Android

sensor-cloud.com allows you to write your sensor data to the cloud and access the data from browsers anywhere in the world by you or your Facebook friends. Sensors are social too!

Learn:

  • Access sensor-cloud.com API

Requirements

  • The example programs can be run from the smartphone or emulator (using RemoteControl).
  • The Bluetooth Starter Kit is required as the light sensor and switch is used in the example program.

Log the Light Intensity to sensor-cloud.com

    Steps to Get Started
  1. Goto sensor-cloud.com and join the site using your Facebook login
  2. Goto My Configuration to generate the API Secret
  3. Download sensor-cloud.py to the smartphone or your PC/emulator
  4. Ensure you have network access as the program will write to the server at sensor-cloud.com
  5. Check the you have a GPS signal using the Google Map app. For the emulator, do check with the GPS example to find how to set the mock geolocation.
  6. You should be familiar with read light example first.
  7. Run the program. Enter the API Key and Secret as per step 2. The timer interval is set to 5 mins.
  8. Your data will be available to you and your friends on www.sensor-cloud.com
  9. Hold the switch on the Light Application Adaptor for at least one second to end the program.

sensor-cloud.py

import math import urllib import datetime from socket import * import time import android class _FancyURLopener(urllib.FancyURLopener): # This class handles basic auth, providing user and password # when required by http response code 401 def __init__(self, usr, pwd): # Set user/password for http and call base class constructor urllib.FancyURLopener.__init__(self) self.usr = usr self.pwd = pwd def prompt_user_passwd(self, host, realm): # Basic auth callback return (self.usr,self.pwd) def get_GPS(): lat = '1.347343' lng = '103.775859' droid = android.Android() droid.startLocating() print "reading GPS ..." event = droid.eventWaitFor('location',10000).result if event['name'] == "location": try: lat = "%10.6f" % event['data']['gps']['latitude'] lng = "%10.6f" % event['data']['gps']['longitude'] except KeyError: lat = "%10.6f" % event['data']['network']['latitude'] lng = "%10.6f" % event['data']['network']['longitude'] print 'lat: ' + lat + ' lng: ' + lng droid.stopLocating() return lat, lng print "start logging" user = raw_input('\nApi Key: ') psw = raw_input('\nApi Secret: ') # Create a customized urlopener for handling authentication request urlopener = _FancyURLopener(user,psw) lat, lng = get_GPS() m = emant.Emant300() m.Open() print m.HwId() # send data running = True t_delay = 300 while running: d = datetime.datetime.utcnow() print "Acquire Data" volt, binval = m.ReadAnalog(emant.Emant300.AIN0,emant.Emant300.COM) lux = int(1333 * volt) print d.strftime("%Y-%m-%d %H:%M:%S")+" : "+str(lux) try: params = urllib.urlencode({'sensorid': '00000001', 'datetime': d.strftime("%Y-%m-%d %H:%M:%S"),'lat': lat,'long': lng,'data': lux,'unit': 'Lux'}) f = urlopener.open("http://api.sensor-cloud.com",params) # response contents = f.read() print contents urlopener.close() except: print "IO error" print "delay .... hold switch to stop" i = 0 sw_state = True while (i<t_delay): sw_state = m.ReadDigitalBit(3) if not sw_state: running = False i = t_delay time.sleep(1) i = i + 1 m.Close() print "end logging"

Scan the script to your smartphone using the Test EMANT380 app or download to PC/Emulator

sensor-cloud.zip