# 109 Lab4 - GPS tracking using OM2M, node-red and app inventor 2 ![](https://i.imgur.com/jicpvVz.png =500x) ###### tags: `109 物聯網核心網路技術` --- [TOC] --- ## oneM2M 架構圖 ![](https://i.imgur.com/PN6k37P.png) ![](https://i.imgur.com/185HO9u.png) ![](https://i.imgur.com/jpehtHy.png) Mca: the interface point between AE and CSE which provides the applications access to the common services. Mcc: the reference point between two CSEs. Mcn: the reference point between CSE and underlying NSE like transport and connectivity services. Mcc’: the interface between two M2M service providers. IN: Infrastructure Node NoDN: Non-oneM2M Device Node ## A. Pre-work * Start the OM2M IN-CSE * Start the OM2M MN-CSE * Start node-red * Start App Inventor 2 :::danger Lab4-1 ::: :::warning 因為 MN-CSE 可以透過 redirector(MCC) 到 IN-CSE 所以這邊 127.0.0.1:8282 可以存取到 IN-CSE 的資料 ::: ## B. Create a MN-AE with Node-RED. ### 1. Application :::info 在 mn-cse 上新增一個 Application resource ::: ![](https://i.imgur.com/3q4EKR5.png =600x) #### Properties * **Platform** : Destination CSE. ![](https://i.imgur.com/oQdnwZ1.png =450x) * **Application** : AE Id (application name). ![](https://i.imgur.com/DdyD5QH.png =450x) * **Point of Access** : PoA URL. * **Announce** : whether this resource is announced to other CSE or not. * **Labels** : labels to describe this application. ![](https://i.imgur.com/MDEmxhb.png =600x) #### om2m resource tree ![](https://i.imgur.com/HSK1hhD.png =400x) --- ### 2. Container :::info Creates a Container resource to store content instances. ::: ![](https://i.imgur.com/fJ9YcqU.png =600x) #### Properties * **Platform** : Destination CSE. (選剛剛新增的 **"mn-cse"**) * **Application** : AE Id (application name) (選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**) * **Container** : Name of the container (choose among DESCRIPTOR, DATA, or input your own container’s name). <!-- ![](https://i.imgur.com/PbilTc1.png =500x) --> * ![](https://i.imgur.com/E6OMrbR.png =500x) #### om2m resource tree * ![](https://i.imgur.com/fQ7gqJZ.png =400x) --- ### 3. ContentInstance :::info Creates a ContentInstance resource to store application’s data. ::: ![](https://i.imgur.com/TPc5WAE.png =600x) #### Properties * Platform: Destination CSE. (選剛剛新增的 **"mn-cse"**) * Application: AE Id (application name). (選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**) * Container: Name of the container. (填入剛剛新增的 **"DATA"**) * Labels: The data you want to store. You must describe three fields: Type (str, int, bool, etc.), Name (the name of the attribute), Value (the value of the attribute). * ![](https://i.imgur.com/e90yr9F.png =700x) #### om2m resource tree ![](https://i.imgur.com/uI83Z5O.png =400x) ![](https://i.imgur.com/0OwlGom.png =500x) --- ### 4. NotificationHandler(用http in取代) om2m 1.3 訂閱時會先確認要通知的位置是否存在,但這顆node不太好用 ("NotificationHandler"會把port佔住,要重啟node-red才能進行後面的測試) 建議使用node-red內建的 **"http in"**,或者直接開啟http server 這邊用 **input ->"http in"** 來示範 ![](https://i.imgur.com/npcBGoV.png =500x) 1. 將第一顆node(http in)改成以下內容 ![](https://i.imgur.com/zHkDbPe.png =500x) 2. 中間那顆是個http response,在訂閱時需要回傳訊息,讓om2m知道這個位置存在 --- ### 5. Subscription Creates a Subscription source. ![](https://i.imgur.com/nAKCZIW.png =500x) * Platform: Destination CSE. (選剛剛新增的 **"mn-cse"**) * Application: AE Id (application name). (選剛剛新增的 **"LOCATION_GATEWAY_APPLICATION"**) * Container: Name of the container you want to subscribe to. (填入剛剛新增的 **"DATA"**) * Path: URL of the webservice that will handle the notification of this subscription. ![](https://i.imgur.com/d4AfTzU.png =500x) * om2m resource tree ![](https://i.imgur.com/CxbxdN4.png =400x) ![](https://i.imgur.com/5L5wLAq.png =600x) --- ## C. 測試&處理Notification :::warning 把剛才用來確認訂閱的flow稍微改一下 ![](https://i.imgur.com/ncqRD8R.png) ::: * "switch" node ![](https://i.imgur.com/gooZH9a.png =500x) * 取出狀態 ```javascript= var data = msg.payload["m2m:sgn"].nev[0].rep[0]["m2m:cin"][0].con[0]; msg.payload = data return msg; ``` ![](https://i.imgur.com/s2y0Dy0.png =550x) * 設定object ```javascript= var New = {latitude:null,longitude:null}; New.latitude = msg.payload.obj.str[0].$.val; New.longitude = msg.payload.obj.str[1].$.val; msg.payload = New; return msg; ``` ![](https://i.imgur.com/HoekteP.png =550x) --- ## D. 在MN-AE新增一個處理建立om2m resource任務的功能 ![](https://i.imgur.com/GpOwCza.png) * http in node ![](https://i.imgur.com/ltwMSm9.png =500x) * set payload and headers ![](https://i.imgur.com/DjyNPUD.png =600x) ```javascript= /*jshint multistr: true */ var data = '<m2m:cin xmlns:m2m="http://www.onem2m.org/xml/protocols">\ <cnf>message</cnf>\ <con>\ &lt;obj&gt;\ &lt;str name=&quot;latitude&quot; val=&quot;'+msg.payload.latitude+'&quot;/&gt;\ &lt;str name=&quot;longitude&quot; val=&quot;'+msg.payload.longitude+'&quot;/&gt;\ &lt;/obj&gt;\ </con>\ </m2m:cin>'; msg.payload = data; msg.headers = {}; msg.headers['X-M2M-Origin'] = 'admin:admin'; msg.headers['Content-Type'] = 'application/xml;ty=4'; return msg; ``` 格式參考 : https://wiki.eclipse.org/OM2M/one/REST_API#Create_a_data_contentInstance * post to mn-cse ![](https://i.imgur.com/LPHerXV.png =600x) --- ## E. Sending Location Data to MN-AE using App inventor 2 1. Create a new project and draw the components in the GUI as shown below. ![](https://i.imgur.com/ataXrwt.png =350x) 2. In the blocks area, draw the corresponding objects for: ![](https://i.imgur.com/nUnItDt.png =550x) ![](https://i.imgur.com/f9cJXxw.png =550x) --- :::danger Lab4-2 ::: ## F. Getting Location Data from IN-AE using App Inventor 2 1. Use node-red to create resources in in-cse * create ae(application) ![](https://i.imgur.com/B2CslRT.png =500x) ![](https://i.imgur.com/RXF1qic.png =500x) ![](https://i.imgur.com/x4Qd0Zv.png =600x) ![](https://i.imgur.com/R0ZQg9G.png =400x) ![](https://i.imgur.com/lFkyBzC.png =500x) --- * create container ![](https://i.imgur.com/fOihXPr.png =500x) ![](https://i.imgur.com/GNLBOtu.png =500x) ![](https://i.imgur.com/xYMDZdI.png =400x) ![](https://i.imgur.com/G6bm2FX.png =500x) --- * create ContentInstance ![](https://i.imgur.com/1813zcg.png =500x) ![](https://i.imgur.com/KpBm6tW.png =600x) ![](https://i.imgur.com/MIVpc3d.png =400x) ![](https://i.imgur.com/CiMeLtI.png =500x) --- * handle the request from app inventor (om2m 自行編譯版) ![](https://i.imgur.com/KsSAJiJ.png =600x) 第四條 第二顆 node ![](https://i.imgur.com/TymKYB9.png =500x) 第四條 第四顆 node ![](https://i.imgur.com/vA3PTTQ.png =500x) 第四條 第六顆 node ![](https://i.imgur.com/AiVrWjT.png =700x) * handle the request from app inventor (om2m 下載版) * 在 `[get]/locations` node 後,新增一function node ```xml= msg.headers = {}; msg.headers['X-M2M-Origin'] = 'admin:admin'; return msg; ``` * `get from in cse` node 取消勾選 Use authentication 2. create a new project and draw the components in the GUI as shown below. ![](https://i.imgur.com/B1HU11t.png =300x) 3. In the blocks area, draw the corresponding objects for: ![](https://i.imgur.com/rTSZA9F.png =550x) ![](https://i.imgur.com/gHNNoE8.png =550x) ![](https://i.imgur.com/V8FBz0C.png =550x)