Advertisement
mikecmills2

GroveStreams Python 2.7 Hello World 1

Nov 26th, 2013
3,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.74 KB | None | 0 0
  1. #GroveStreams.com Python version 2.7 Feed Example
  2. #Demonstrates uploading two stream feeds within a URL and URL
  3. # encoding
  4.  
  5. #This example uploads two stream feeds, random temperature and humidity
  6. # samples every 10 seconds.
  7. #A full "how to" guide for this example can be found at:
  8. # https://www.grovestreams.com/developers/getting_started_helloworld_python.html
  9. #It relies and the GroveStreams API which can be found here:
  10. # https://www.grovestreams.com/developers/api.html#1
  11.  
  12. # License:
  13. # Copyright 2014 GroveStreams LLC.
  14. # Licensed under the Apache License, Version 2.0 (the "License");
  15. # you may not use this file except in compliance with the License.
  16. # You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  17. #
  18. # Unless required by applicable law or agreed to in writing, software
  19. # distributed under the License is distributed on an "AS IS" BASIS,
  20. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. # See the License for the specific language governing permissions and
  22. # limitations under the License.
  23.  
  24. #GroveStreams Setup:
  25.  
  26. #* Sign Up for Free User Account - https://www.grovestreams.com
  27. #* Create a GroveStreams organization
  28. #* Enter the GroveStreams org uid under "GroveStreams Settings" below
  29. #*    (Can be retrieved from a GroveStreams organization:
  30. #*     Tools toolbar button - View Organization UID)
  31. #* Enter the GroveStreams api key under "GroveStreams Settings" below  
  32. #*    (Can be retrieved from a GroveStreams organization:
  33. #*     click the Api Keys toolbar button,
  34. #*     select your Api Key, and click View Secret Key)
  35.  
  36. import time
  37. import datetime
  38. import httplib
  39. import random
  40. import urllib
  41.  
  42.  
  43. if __name__ == '__main__':
  44.    
  45.     #GroveStreams Settings
  46.     api_key = "YOUR_SECRET_API_KEY_HERE"    #Change This!!!
  47.    
  48.     component_id = "sensor1 - hello world"
  49.     base_url = '/api/feed?'
  50.    
  51.     conn = httplib.HTTPConnection('www.grovestreams.com')
  52.    
  53.     while True:
  54.        
  55.         temperature_val = random.randrange(-10, 40)
  56.         humidity_val = random.randrange(0, 100)
  57.        
  58.         #Upload the feed
  59.         try:    
  60.             #Let the GS servers set the sample times. Encode parameters
  61.             url = base_url + urllib.urlencode({'compId' : component_id,
  62.                                                'temperature' : temperature_val,
  63.                                                'humidity' : humidity_val})
  64.            
  65.             #Alternative URL that includes the sample time
  66.             #now = datetime.datetime.now()
  67.             #sample_time = int(time.mktime(now.timetuple())) * 1000
  68.             #url = base_url + urllib.urlencode({'compId' : component_id, 'time' : sample_time, 'temperature' : temperature_val, 'humidity' : humidity_val})
  69.            
  70.             #Alternative URL that uses the stream order to determine where
  71.             # to insert the samples
  72.             #url = base_url + urllib.urlencode({'compId' : component_id, 'data' : [temperature_val, humidity_val]}, True)
  73.            
  74.             #The api_key token can be passed as URL parameters or as a cookie.
  75.             # We've chosen to pass it as a cookie to keep the URL length small as
  76.             # some devices have a URL size limit
  77.             headers = {"Connection" : "close", "Content-type": "application/json",
  78.                        "Cookie" : "api_key="+api_key}
  79.            
  80.             #GS limits feed calls to one per 10 seconds per outward facing router IP address
  81.             #Use the ip_addr and headers assignment below to work around this
  82.             # limit by setting the below to this device's IP address
  83.             #ip_addr = "192.168.1.72"
  84.             #headers = {"Connection" : "close", "Content-type": "application/json", "X-Forwarded-For": ip_addr, "Cookie" : "org="+org+";api_key="+api_key}
  85.            
  86.             print('Uploading feed to: ' + url)
  87.            
  88.             conn.request("PUT", url, "", headers)
  89.            
  90.             #Check for errors
  91.             response = conn.getresponse()
  92.             status = response.status
  93.            
  94.             if status != 200 and status != 201:
  95.                 try:
  96.                     if (response.reason != None):
  97.                         print('HTTP Failure Reason: ' + response.reason + ' body: ' + response.read())
  98.                     else:
  99.                         print('HTTP Failure Body: ' + response.read())
  100.                 except Exception:
  101.                     print('HTTP Failure Status: %d' % (status) )
  102.        
  103.         except Exception as e:
  104.             print('HTTP Failure: ' + str(e))
  105.        
  106.         finally:
  107.             if conn != None:
  108.                 conn.close()
  109.        
  110.         #Pause for ten seconds
  111.         time.sleep(10)
  112.  
  113.     # quit
  114.     exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement