This example presents field data from sensor BME280 connecting to cloud, mbed Device Connector.

Please read details at the link. https://github.com/soramame21/connector-api-python-quickstart_4BME280

Committer:
edamame22
Date:
Wed Apr 26 03:13:23 2017 +0000
Revision:
0:a9bcda5b678a
Child:
1:b194c28a1670
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:a9bcda5b678a 1 import mbed_connector_api # mbed Device Connector library
edamame22 0:a9bcda5b678a 2 import pybars # use to fill in handlebar templates
edamame22 0:a9bcda5b678a 3 from flask import Flask # framework for hosting webpages
edamame22 0:a9bcda5b678a 4 from flask_socketio import SocketIO, emit,send,join_room, leave_room
edamame22 0:a9bcda5b678a 5 from base64 import standard_b64decode as b64decode
edamame22 0:a9bcda5b678a 6 import os
edamame22 0:a9bcda5b678a 7
edamame22 0:a9bcda5b678a 8 app = Flask(__name__)
edamame22 0:a9bcda5b678a 9 socketio = SocketIO(app,async_mode='threading')
edamame22 0:a9bcda5b678a 10
edamame22 0:a9bcda5b678a 11 if 'ACCESS_KEY' in os.environ.keys():
edamame22 0:a9bcda5b678a 12 token = os.environ['ACCESS_KEY'] # get access key from environment variable
edamame22 0:a9bcda5b678a 13 else:
edamame22 0:a9bcda5b678a 14 token = "ChangeMe" # replace with your API token
edamame22 0:a9bcda5b678a 15
edamame22 0:a9bcda5b678a 16 connector = mbed_connector_api.connector(token)
edamame22 0:a9bcda5b678a 17
edamame22 0:a9bcda5b678a 18 @app.route('/')
edamame22 0:a9bcda5b678a 19 def index():
edamame22 0:a9bcda5b678a 20 # get list of endpoints, for each endpoint get the pattern (/3201/0/5853) value
edamame22 0:a9bcda5b678a 21 epList = connector.getEndpoints().result
edamame22 0:a9bcda5b678a 22 for index in range(len(epList)):
edamame22 0:a9bcda5b678a 23 print "ren01 Endpoint Found: ",epList[index]['name']
edamame22 0:a9bcda5b678a 24 print "ren01 epList[index]", epList[index]
edamame22 0:a9bcda5b678a 25 print "end ren01", index
edamame22 0:a9bcda5b678a 26 e_h = connector.getResourceValue(epList[index]['name'],"/3304/0/5700")
edamame22 0:a9bcda5b678a 27 e_t = connector.getResourceValue(epList[index]['name'],"/3303/0/5700")
edamame22 0:a9bcda5b678a 28 e_p = connector.getResourceValue(epList[index]['name'],"/3323/0/5700")
edamame22 0:a9bcda5b678a 29 while not e_p.isDone() or not e_t.isDone or not e_h.isDone :
edamame22 0:a9bcda5b678a 30 None
edamame22 0:a9bcda5b678a 31 epList[index]['humidity_value'] = e_h.result
edamame22 0:a9bcda5b678a 32 epList[index]['tempereture_value'] = e_t.result
edamame22 0:a9bcda5b678a 33 epList[index]['pressure_value'] = e_p.result
edamame22 0:a9bcda5b678a 34 print "Endpoint List :",epList
edamame22 0:a9bcda5b678a 35 # fill out html using handlebar template
edamame22 0:a9bcda5b678a 36 handlebarJSON = {'endpoints':epList}
edamame22 0:a9bcda5b678a 37 comp = pybars.Compiler()
edamame22 0:a9bcda5b678a 38 tmp_src = open("./views/index.hbs",'r').read()
edamame22 0:a9bcda5b678a 39 source = unicode(tmp_src, "utf-8")
edamame22 0:a9bcda5b678a 40 template = comp.compile(source)
edamame22 0:a9bcda5b678a 41 return "".join(template(handlebarJSON))
edamame22 0:a9bcda5b678a 42
edamame22 0:a9bcda5b678a 43 @socketio.on('connect')
edamame22 0:a9bcda5b678a 44 def connect():
edamame22 0:a9bcda5b678a 45 print('connect ')
edamame22 0:a9bcda5b678a 46 join_room('room')
edamame22 0:a9bcda5b678a 47
edamame22 0:a9bcda5b678a 48 @socketio.on('disconnect')
edamame22 0:a9bcda5b678a 49 def disconnect():
edamame22 0:a9bcda5b678a 50 print('Disconnect')
edamame22 0:a9bcda5b678a 51 leave_room('room')
edamame22 0:a9bcda5b678a 52
edamame22 0:a9bcda5b678a 53 @socketio.on('subscribe_to_presses')
edamame22 0:a9bcda5b678a 54 def subscribeToPresses(data):
edamame22 0:a9bcda5b678a 55 print('222 subscribe_to_presses: ',data)
edamame22 0:a9bcda5b678a 56 # Subscribe to all changes of resource /3303/0/5700 (button presses)
edamame22 0:a9bcda5b678a 57 e_h = connector.putResourceSubscription(data['endpointName'],'/3304/0/5700')
edamame22 0:a9bcda5b678a 58 e_t = connector.putResourceSubscription(data['endpointName'],'/3303/0/5700')
edamame22 0:a9bcda5b678a 59 e_p = connector.putResourceSubscription(data['endpointName'],'/3323/0/5700')
edamame22 0:a9bcda5b678a 60 while not e_h.isDone() or not e_t.isDone() or not e_p.isDone():
edamame22 0:a9bcda5b678a 61 None
edamame22 0:a9bcda5b678a 62 if e_h.error:
edamame22 0:a9bcda5b678a 63 print("Error e_h 21: ",e_h.error.errType, e_h.error.error, e.raw_data)
edamame22 0:a9bcda5b678a 64 elif e_t.error:
edamame22 0:a9bcda5b678a 65 print("Error e_t 21: ",e_t.error.errType, e_t.error.error, e_t.raw_data)
edamame22 0:a9bcda5b678a 66 elif e_p.error:
edamame22 0:a9bcda5b678a 67 print("Error e_p 21: ",e_p.error.errType, e_p.error.error, e_p.raw_data)
edamame22 0:a9bcda5b678a 68 else:
edamame22 0:a9bcda5b678a 69 print("Subscribed Successfully!")
edamame22 0:a9bcda5b678a 70 emit('subscribed-to-presses')
edamame22 0:a9bcda5b678a 71
edamame22 0:a9bcda5b678a 72 @socketio.on('unsubscribe_to_presses')
edamame22 0:a9bcda5b678a 73 def unsubscribeToPresses(data):
edamame22 0:a9bcda5b678a 74 print('unsubscribe_to_presses: ',data)
edamame22 0:a9bcda5b678a 75 e_h = connector.deleteResourceSubscription(data['endpointName'],'/3304/0/5700')
edamame22 0:a9bcda5b678a 76 e_t = connector.deleteResourceSubscription(data['endpointName'],'/3303/0/5700')
edamame22 0:a9bcda5b678a 77 e_p = connector.deleteResourceSubscription(data['endpointName'],'/3323/0/5700')
edamame22 0:a9bcda5b678a 78 while not e_h.isDone() or not e_t.isDone() or not e_p.isDone():
edamame22 0:a9bcda5b678a 79 None
edamame22 0:a9bcda5b678a 80 if e_h.error:
edamame22 0:a9bcda5b678a 81 print("Error e_h 22: ",e_h.error.errType, e_h.error.error, e.raw_data)
edamame22 0:a9bcda5b678a 82 elif e_t.error:
edamame22 0:a9bcda5b678a 83 print("Error e_t 22: ",e_t.error.errType, e_t.error.error, e_t.raw_data)
edamame22 0:a9bcda5b678a 84 elif e_p.error:
edamame22 0:a9bcda5b678a 85 print("Error e_p 22: ",e_p.error.errType, e_p.error.error, e_p.raw_data)
edamame22 0:a9bcda5b678a 86 else:
edamame22 0:a9bcda5b678a 87 print("Unsubscribed Successfully!")
edamame22 0:a9bcda5b678a 88 emit('unsubscribed-to-presses',{"endpointName":data['endpointName'],"value":'True'})
edamame22 0:a9bcda5b678a 89
edamame22 0:a9bcda5b678a 90 @socketio.on('get_presses')
edamame22 0:a9bcda5b678a 91 def getPresses(data):
edamame22 0:a9bcda5b678a 92 # Read data from GET resource /3200/0/5501 (num button presses)
edamame22 0:a9bcda5b678a 93 # print("ren02>, get_presses ",data)
edamame22 0:a9bcda5b678a 94 e_h = connector.getResourceValue(data['endpointName'],"/3304/0/5700")
edamame22 0:a9bcda5b678a 95 e_t = connector.getResourceValue(data['endpointName'],"/3303/0/5700")
edamame22 0:a9bcda5b678a 96 e_p = connector.getResourceValue(data['endpointName'],"/3323/0/5700")
edamame22 0:a9bcda5b678a 97 while not e_p.isDone() or not e_t.isDone or not e_h.isDone :
edamame22 0:a9bcda5b678a 98 None
edamame22 0:a9bcda5b678a 99 if e_h.error :
edamame22 0:a9bcda5b678a 100 print("Error e_h: ",e_h.error.errType, e_h.error.error, e_h.raw_data)
edamame22 0:a9bcda5b678a 101 elif e_t.error :
edamame22 0:a9bcda5b678a 102 print("Error e_t: ",e_t.error.errType, e_t.error.error, e_t.raw_data)
edamame22 0:a9bcda5b678a 103 elif e_p.error :
edamame22 0:a9bcda5b678a 104 print("Error e_p: ",e_p.error.errType, e_p.error.error, e_p.raw_data)
edamame22 0:a9bcda5b678a 105 else:
edamame22 0:a9bcda5b678a 106 dataT_to_emit = {"endpointName":'multiple_data', "value":"e_t="+e_t.result+";e_h="+e_h.result+";e_p="+e_p.result}
edamame22 0:a9bcda5b678a 107 emit('SetTemp', dataT_to_emit)
edamame22 0:a9bcda5b678a 108
edamame22 0:a9bcda5b678a 109
edamame22 0:a9bcda5b678a 110 # 'notifications' are routed here, handle subscriptions and update webpage
edamame22 0:a9bcda5b678a 111 def notificationHandler(data):
edamame22 0:a9bcda5b678a 112 global socketio
edamame22 0:a9bcda5b678a 113
edamame22 0:a9bcda5b678a 114 print "\r\nNotification Data Received : %s" %data['notifications']
edamame22 0:a9bcda5b678a 115 notifications = data['notifications']
edamame22 0:a9bcda5b678a 116 tmp=""
edamame22 0:a9bcda5b678a 117 for thing in notifications:
edamame22 0:a9bcda5b678a 118 if thing["path"]=="/3304/0/5700": tmp +="e_h="+b64decode(thing["payload"])+";"
edamame22 0:a9bcda5b678a 119 elif thing["path"]=="/3303/0/5700": tmp +="e_t="+b64decode(thing["payload"])+";"
edamame22 0:a9bcda5b678a 120 elif thing["path"]=="/3323/0/5700": tmp +="e_p="+b64decode(thing["payload"])+";"
edamame22 0:a9bcda5b678a 121 stuff = {"endpointName":thing["ep"],"value":tmp+"end"}
edamame22 0:a9bcda5b678a 122 print ("990 Emitting :",stuff)
edamame22 0:a9bcda5b678a 123 socketio.emit('SetTemp',stuff)
edamame22 0:a9bcda5b678a 124
edamame22 0:a9bcda5b678a 125 if __name__ == "__main__":
edamame22 0:a9bcda5b678a 126 connector.deleteAllSubscriptions() # remove all subscriptions, start fresh
edamame22 0:a9bcda5b678a 127 connector.startLongPolling() # start long polling connector.mbed.com
edamame22 0:a9bcda5b678a 128 connector.setHandler('notifications', notificationHandler) # send 'notifications' to the notificationHandler FN
edamame22 0:a9bcda5b678a 129 socketio.run(app,host='0.0.0.0', port=8080)