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

Files at this revision

API Documentation at this revision

Comitter:
Ren Boting
Date:
Wed Aug 23 14:08:23 2017 +0900
Parent:
0:a9bcda5b678a
Commit message:
Refined the application and update logo.

Changed in this revision

app.py Show annotated file Show diff for this revision Revisions of this file
views/index.hbs Show annotated file Show diff for this revision Revisions of this file
--- a/app.py	Wed Apr 26 03:13:23 2017 +0000
+++ b/app.py	Wed Aug 23 14:08:23 2017 +0900
@@ -1,8 +1,12 @@
-import mbed_connector_api                 # mbed Device Connector library
-import pybars                             # use to fill in handlebar templates
-from   flask             import Flask    # framework for hosting webpages
-from   flask_socketio     import SocketIO, emit,send,join_room, leave_room
-from   base64             import standard_b64decode as b64decode
+import mbed_connector_api             # mbed Device Connector library
+import pybars                         # use to fill in handlebar templates
+from   flask          import Flask    # framework for hosting webpages
+from   flask_socketio import SocketIO
+from   flask_socketio import emit
+from   flask_socketio import send
+from   flask_socketio import join_room
+from   flask_socketio import leave_room
+from   base64         import standard_b64decode as b64decode
 import os
 
 app = Flask(__name__)
@@ -17,12 +21,12 @@
 
 @app.route('/')
 def index():
-    # get list of endpoints, for each endpoint get the pattern (/3201/0/5853) value
+    # get list of endpoints, for each endpoint get resources
     epList = connector.getEndpoints().result
     for index in range(len(epList)):
-        print "ren01 Endpoint Found: ",epList[index]['name']
-        print "ren01 epList[index]", epList[index]
-        print "end ren01", index
+        print "Endpoint Found: ",epList[index]['name']
+        print "epList[index]", epList[index]
+        print "end", index
         e_h = connector.getResourceValue(epList[index]['name'],"/3304/0/5700")
         e_t = connector.getResourceValue(epList[index]['name'],"/3303/0/5700")
         e_p = connector.getResourceValue(epList[index]['name'],"/3323/0/5700")
@@ -31,6 +35,7 @@
         epList[index]['humidity_value'] = e_h.result
         epList[index]['tempereture_value'] = e_t.result
         epList[index]['pressure_value'] = e_p.result
+    
     print "Endpoint List :",epList
     # fill out html using handlebar template
     handlebarJSON = {'endpoints':epList}
@@ -42,7 +47,7 @@
 
 @socketio.on('connect')
 def connect():
-    print('connect ')
+    print('Connect')
     join_room('room')
 
 @socketio.on('disconnect')
@@ -52,8 +57,8 @@
 
 @socketio.on('subscribe_to_presses')
 def subscribeToPresses(data):
-    print('222 subscribe_to_presses: ',data)
-    # Subscribe to all changes of resource /3303/0/5700 (button presses)
+    print('subscribe_to_presses: ',data)
+    # Subscribe to all changes of resources
     e_h = connector.putResourceSubscription(data['endpointName'],'/3304/0/5700')
     e_t = connector.putResourceSubscription(data['endpointName'],'/3303/0/5700')
     e_p = connector.putResourceSubscription(data['endpointName'],'/3323/0/5700')
@@ -89,8 +94,7 @@
 
 @socketio.on('get_presses')
 def getPresses(data):
-    # Read data from GET resource /3200/0/5501 (num button presses)
-   # print("ren02>,  get_presses ",data)
+    # Read data from GET resources
     e_h = connector.getResourceValue(data['endpointName'],"/3304/0/5700")
     e_t = connector.getResourceValue(data['endpointName'],"/3303/0/5700")
     e_p = connector.getResourceValue(data['endpointName'],"/3323/0/5700")
@@ -110,20 +114,26 @@
 # 'notifications' are routed here, handle subscriptions and update webpage
 def notificationHandler(data):
     global socketio
-
     print "\r\nNotification Data Received : %s" %data['notifications']
     notifications = data['notifications']
     tmp=""
     for thing in notifications:
-        if  thing["path"]=="/3304/0/5700":  tmp +="e_h="+b64decode(thing["payload"])+";"
-        elif thing["path"]=="/3303/0/5700":  tmp +="e_t="+b64decode(thing["payload"])+";"
-        elif thing["path"]=="/3323/0/5700":  tmp +="e_p="+b64decode(thing["payload"])+";"
+        if  thing["path"]=="/3304/0/5700":
+            tmp +="e_h="+b64decode(thing["payload"])+";"
+        elif thing["path"]=="/3303/0/5700":
+            tmp +="e_t="+b64decode(thing["payload"])+";"
+        elif thing["path"]=="/3323/0/5700":
+            tmp +="e_p="+b64decode(thing["payload"])+";"
+    
     stuff = {"endpointName":thing["ep"],"value":tmp+"end"}
     print ("990 Emitting :",stuff)
     socketio.emit('SetTemp',stuff)
 
 if __name__ == "__main__":
-    connector.deleteAllSubscriptions()                            # remove all subscriptions, start fresh
-    connector.startLongPolling()                                # start long polling connector.mbed.com
-    connector.setHandler('notifications', notificationHandler)     # send 'notifications' to the notificationHandler FN
+    # remove all subscriptions, start fresh
+    connector.deleteAllSubscriptions()
+    # start long polling connector.mbed.com
+    connector.startLongPolling()
+    # send 'notifications' to the notificationHandler FN
+    connector.setHandler('notifications', notificationHandler)
     socketio.run(app,host='0.0.0.0', port=8080)
--- a/views/index.hbs	Wed Apr 26 03:13:23 2017 +0000
+++ b/views/index.hbs	Wed Aug 23 14:08:23 2017 +0900
@@ -18,7 +18,7 @@
   <body>
     <div class="row margin">
       <div class="small-12 columns">
-        <div><img src="static/img/ARMmbedLogo.png" alt="ARM mbed"></div>
+        <div><img src="static/img/ARMmbedLogo.jpg" alt="ARM mbed"></div>
       </div>
     </div>
     <div class="blueband">