Basic Web Services Example using DIA

From Digi Developer

Jump to: navigation, search

This simple Web Services example uses the built-in "Hello World" DIA software. DIA runs on your Digi communications gateway (ConnectPort X) and is an application (written in Python) that connects devices (both physical and virtual) to presentations (external resources like this example).

The example code is 100% AJAX, a combination of HTML and Javascript that includes a well-known call (XmlHttpRequests) to interact with web services.

Please note that this example only works when the HTML file is loaded from the local filesystem (not a web-server) as XmlHttpRequests are not allowed to accessed any other server than the server which served the HTML file. Traditionally when writing a web application which accesses a device attached to Device Cloud, one must have the web application server access Device Cloud on the web-browser's behalf.

Step 1
Include the "Hello World" configuration information for DIA to utilized on your Digi gateway

## Hello World Digi Device Acquisition Framework Configuration File 
 # The template device driver; creates a demonstration device
 # containing a counter and a set of channels implementing a simple
 # adder.
   - name: hello
     driver: devices.hello_world_device:HelloWorldDevice
         update_rate: 1.0

 # Create a new console instance on TCP port 4146.  It can be connected
 # two by using any telnet client.
 - name: console0
   driver: presentations.console.console:Console
       type: tcp
       port: 4146

 # Create a web presentation instance.  When running on a PC this will
 # start a new web-server on a default port
 # When running on a Digi device this presentation will "extend" the
 # web-server built in to the Digi device with a new page named
 # "digi_daq.html".  
 - name: web0
   driver: presentations.web.web:Web
       page: idigi_dia.html
       port: 8081
 # The RCIHandler allows for channel dumps, querying a channel, and setting
 # a channel.  The RCIHandler parses an incoming XML formatted message
 # to determine what you wish to do.  The messages are shown below.  If
 # an error is encountered, the request you sent will have a child error
 # element specifying the error information.        
 - name: rci_handler
   driver: presentations.rci.rci_handler:RCIHandler
       target_name: idigi_dia

This includes the virtual connector / driver (hello_world_device), and three presentations for interacting with the virtual driver: one via telnet (to port 4146), one via a simple web page (IP_address_of_gateway/idigi_dia.html) and one via a Web Services call to what is called an RCI (Remote Command Interface), using a target of idigi_dia.

Step 2
Ensure that you have a Device Cloud account and that your ConnectPort-X gateway is actively connected. For additional information, visit and visit the "resources" page.

Step 3
Restart your gateway and ensure that DIA is running. You can automate DIA startup via the "Pythons->Autostart" screen in the Digi ConnectPort X webUI administration utility.

Step 4
Test your DIA configuration locally by pointing your laptop to the ConnectPort-X's local IP address. This means that you need to be locally attached for this particular test. type "IP_Address_of_ConnectPort-X/idigi_dia.html" into your web browser. You should receive a web-UI that will display the three samples available to the virtual connector/driver: prefix_string, suffix_string, xtended_string (a concatenation of the first two samples).

Step 5
Copy the following code into a text file and save on your laptop. Open via Firefox, Safari or Chrome. IE works, but you'll need to add a javascript function to allow it to recognize a few AJAX methods. Upon running, you'll need to include:

  • Your Digi ConnectPort-X gateway address (just the last two octets - e.g. 00409DFF-FF382CF3)
  • URL of the Device Cloud Connectivity Server (the URL where your Digi ConnectPort-X product is connected - found from Device Cloud when you are logged in)
  • username: Your Device Cloud username
  • password: Your Device Cloud password

Download the AJAX code for your PC-Browser here ->

Below you find an extended version of the above AJAX code. The changes are:

- I added the option to get a specific channel ("channel get" beside "channel_dump")
- I added a debug option to display the RCI answer
- I used it with my CPX and added for info my ana.yml file for information 
- I read all values from a complete device by "get all", or "get one" to read only one channel value
here are the files: ->‎
Personal tools
Wiki Editing