{"id":305,"date":"2016-03-14T13:00:52","date_gmt":"2016-03-14T17:00:52","guid":{"rendered":"http:\/\/salzlechner.com\/dev\/?p=305"},"modified":"2016-03-14T13:08:58","modified_gmt":"2016-03-14T17:08:58","slug":"interfacing-dataflex-internet-of-things-iot","status":"publish","type":"post","link":"http:\/\/salzlechner.com\/dev\/2016\/03\/14\/interfacing-dataflex-internet-of-things-iot\/","title":{"rendered":"Interfacing DataFlex &#8211; Internet of Things &#8211; IoT"},"content":{"rendered":"<h2>What is Internet of Things?<\/h2>\n<p>Essentially they are devices that can be sensed or controlled via the internet. From house lights to kitchen gadgets, lawn care, security systems to pretty much any device we can think of.<\/p>\n<p>Technically simply a device that is computer controlled and that computer inside the device is connected to a cloud interface that allows you to remotely monitor it and\/or control it.<\/p>\n<p>DataFlex?<\/p>\n<p>No, we are not going to run DataFlex on an embedded device that wouldn&#8217;t make sense. What we are going to do is to build an IoT device and then use DataFlex to monitor and control the device<\/p>\n<p>in this example I decided to create a small weather station device that allows me to monitor temperature, humidity, barometric pressure and other things such as wind speed, direction, rainfall, etc.<\/p>\n<h2>Particle Photon<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-308\" src=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1-300x169.jpg\" alt=\"photon1\" width=\"300\" height=\"169\" srcset=\"http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1-300x169.jpg 300w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1-768x432.jpg 768w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1-1024x576.jpg 1024w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1-1080x608.jpg 1080w, http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/03\/photon1.jpg 1920w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>For the device i chose a Particle Photon board. The Particle Photon is a prototyping board for IoT devices and comes with a cloud system already created for you. It is\u00a0essentially a combination of a ARM Cortex microcontroller, a Broadcom Wi-Fi chip some memory and a few other components.<\/p>\n<p>The Photon can be programmed using Wiring, C\/C++ or even ARM assembly.<\/p>\n<p>The great thing about the Photon (or the Electron which has a 3G modem instead of Wi-Fi) is that it includes the cloud service and cloud library.<\/p>\n<p>When you receive your photon you can use an app on your phone to help connect the device to your Wi-Fi and then register the device on the Particle Cloud.<\/p>\n<p>Once registered you can remotely control the device and\/or remotely query values from the device<\/p>\n<p>We are not going into detail on building the device here. We may do that in another blog. But we want to look at how we can monitor and control the device from a DataFlex application either\u00a0desktop or\u00a0web application.<\/p>\n<p>On the device we registered a cloud variable called winfo which is filled by the device with a JSON text containing the following<\/p>\n<p>Temperature<br \/>\nHumidity<br \/>\nBarometric Temperature<br \/>\nBarometric Pressure<\/p>\n<p>we could do other information as well but for our test this should be enough<\/p>\n<p>here is some example code on what that could look like on the Photon<\/p>\n<pre class=\"lang:default decode:true\">String winfo;\r\n\r\nvoid setup()\r\n{\r\n  Particle.variable(\"winfo\", &amp;winfo, STRING);\r\n\r\n    \/\/Initialize the I2C sensors and ping them\r\n    sensor.begin();\/\/This will print out which devices it has detected\r\n\r\n    sensor.setModeBarometer();\/\/Set to Barometer Mode\r\n\r\n    sensor.setOversampleRate(7); \/\/ Set Oversample rate\r\n\r\n    sensor.enableEventFlags(); \r\n}\r\n\r\nvoid loop()\r\n{\r\n      \/\/Get readings from all sensors\r\n      getWeather();\r\n\r\n      winfo = String::format(\"{ temp: %f, humidity: %f, baroTemp: %f, pascals: %f }\", tempf, humidity, baroTemp, pascals);\r\n}\r\n<\/pre>\n<p>the Photon automatically connects to the particle cloud when powered on.<\/p>\n<h2>Communicating with the Photon<\/h2>\n<p>Each photon device has a unique identifier. In addition when registered a security token is issued for the device as well. We can use a RESTful web api to communicate with the photon from our DataFlex application or any other application for that matter of course<\/p>\n<p>Similar to my blog post on calling RESTful services we build a simple view that can call a RESTful service using GET. This is essentially the same as typing the URL into the address bar of your favorite browser<\/p>\n<p>start with a simple view and then add the following to the top<\/p>\n<pre class=\"lang:default decode:true \">Use Windows.pkg\r\nUse DFClient.pkg\r\nUse cHttpTransfer.pkg\r\n\r\nDeferred_View Activate_oDataFlexAndIoT for ;\r\nObject oDataFlexAndIoT is a dbView\r\n    \/\/ create an HTTP Transfer object\r\n\r\n    Object oHTTPTransfer is a cHttpTransfer   \r\n        \/\/ the host points to the particle cloud\r\n        Set psRemoteHost to \"api.particle.io\"\r\n\r\n        \/\/ we need to use SSL\r\n        Set piRemotePort to rpHttpSSL\r\n        Set peTransferFlags to ifSecure\r\n\r\n        Set pbShowErrorDialog to True\r\n\r\n        \/\/ for now we simply show the data returned    \r\n        Procedure OnDataReceived String sContentType String sData\r\n            Showln sContentType \r\n            Showln \"-----------------------------------------------\"\r\n            Showln sData\r\n            Showln \"-----------------------------------------------\"\r\n            \r\n        End_Procedure\r\n    End_Object<\/pre>\n<p>the remote host is pointing to the particle cloud API.\u00a0We also have to use SSL to create a secure connection to the cloud API<\/p>\n<p>then for testing we simply show the data returned by the API<\/p>\n<p>The next thing is to call the cloud API<\/p>\n<p>we add a button to the view as follows<\/p>\n<pre class=\"lang:default decode:true\">    Object oGetWeatherBtn is a Button\r\n        Set Location to 13 28\r\n        Set Label to 'Weather'\r\n    \r\n        \/\/ fires when the button is clicked\r\n        Procedure OnClick\r\n            Integer iRetVal\r\n            \r\n            String sURL sDeviceID sAccessToken\r\n            Move \"###########\" to sDeviceID\r\n            Move \"###################\" to sAccessToken\r\n            \r\n            Move (\"\/v1\/devices\/\"+sDeviceID+\"\/winfo?access_token=\"+sAccessToken) to sURL\r\n            \r\n            Get HttpGetRequest of oHTTPTransfer sUrl to iRetVal\r\n        End_Procedure\r\n    \r\n    End_Object<\/pre>\n<p>the device id is the unique id for the device. The access token is a secure access token assigned by the API when the device is registered.<\/p>\n<p>the URL to query variables is as follows<\/p>\n<pre class=\"lang:default decode:true \">\/v1\/devices\/#######\/winfo?access_token=######<\/pre>\n<p>winfo is the name of the variable we are querying and of course the device id and access token have to be sent.<\/p>\n<p>Now we compile and run the program<\/p>\n<p>After clicking the button if the device is running and connected we will get the following data returned<\/p>\n<pre class=\"lang:default decode:true\">{\r\n  \"cmd\": \"VarReturn\",\r\n  \"name\": \"winfo\",\r\n  \"result\": \"{ temp: 78.786606, humidity: 43.598694, baroTemp: 77.112503, pascals: 101761.750000 }\",\r\n  \"coreInfo\": {\r\n    \"last_app\": \"\",\r\n    \"last_heard\": \"2016-03-14T16:29:58.548Z\",\r\n    \"connected\": true,\r\n    \"last_handshake_at\": \"2016-03-14T16:29:45.869Z\",\r\n    \"deviceID\": \"#########\",\r\n    \"product_id\": 6\r\n  }\r\n}<\/pre>\n<p>the data is JSON data and the field we are interested in is called &#8216;result&#8217;<\/p>\n<p>The data in the result field is JSON data<\/p>\n<pre class=\"lang:default decode:true \">{ \r\n    temp: 78.786606, \r\n    humidity: 43.598694, \r\n    baroTemp: 77.112503, \r\n    pascals: 101761.750000 \r\n}<\/pre>\n<p>and it reports all the values we prepared on the photon.<\/p>\n<p>Now our DataFlex application can use these values and show them on screen or save them in a database, etc.<\/p>\n\n\t\t<div class='author-shortcodes'>\n\t\t\t<div class='author-inner'>\n\t\t\t\t<div class='author-image'>\n\t\t\t<img src='http:\/\/salzlechner.com\/dev\/wp-content\/uploads\/sites\/2\/2016\/02\/mike5crop-566174_60x60.jpg' alt='' \/>\n\t\t\t<div class='author-overlay'><\/div>\n\t\t<\/div> \n\t\t<div class='author-info'>\n\t\t\tMichael Salzlechner is the CEO of StarZen Technologies, Inc.<\/p>\n<p>He was part of the Windows Team at Data Access Worldwide that created the DataFlex for Windows Product before joining <a href=\"http:\/\/starzen.com\">StarZen Technologies<\/a>. StarZen Technologies provides consulting services as well as custom Application development and third party products specifically for DataFlex developers<\/p>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A blog showing connecting DataFlex with IoT devices<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","ngg_post_thumbnail":0,"footnotes":""},"categories":[6,25,32,33],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-dataflex","category-hardware","category-internet-of-things","category-particle-photon"],"_links":{"self":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":4,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":307,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/posts\/305\/revisions\/307"}],"wp:attachment":[{"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/salzlechner.com\/dev\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}