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:
Ren Boting
Date:
Wed Aug 23 14:08:23 2017 +0900
Revision:
1:b194c28a1670
Parent:
0:a9bcda5b678a
Refined the application and update logo.

Who changed what in which revision?

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