Welcome to Thunderheads
|In part one of this project log, I showed you how to connect an Arduino serial project to an XBee® ZB moduleand how to create code for a ConnectPort® X gatewaythat would allow you to send ASCII serial commands over a ZigBee network. Now we are going to look at using theiDigi® Developer Cloud and iDigi web services to send serial commands to the Arduino project from a mobile web site.||About the Author —
Mark Geller began his career at Digi as a web and Flash programmer. Over the years Mark’s focus has shifted towards the creation of iDigi applications and demos, bringing him to his current position as an iDigi Application Developer.
Before I could start sending serial commands, I first had to connect my device to the iDigi Developer Cloud. You can create your own iDigi Developer Cloud account for free. Visit www.idigi.com/getstarted to set up your account.
I logged into my developer account and clicked the “Devices” link in the navigation menu on the left side of the page to see a list of devices associated with my account. To add a new device, I clicked the blue “+” icon in the devices toolbar to start the iDigi devices discovery utility, selected my gateway from the list of devices and then clicked the “OK” button to associate my gateway to my iDigi account. I waited a minute and then clicked the refresh button (the button with two yellow arrows on it) in the devices toolbar to make sure my gateway was able to connect to the iDigi Developer Cloud.
Before building a web site that uses iDigi web services, I like to test my web service calls in the iDigi Manager Pro™ web services console. It is a convenient way to verify that my device is working as expected and to try out web service calls without writing any code.
Since I’m using iDigi® Dia to send serial commands, I needed to test the “channel_set” SCI callback web service. A channel set command has two parameters:
- 1) The channel name formatted as “device.channel”
- 2) The value that you want to set
I used the default channel and device names when I created the iDigi Dia python code, so the channel that I’m sending commands to is “xbee_serial_terminal0.write”.
|I clicked the “Web Services Console” link in the navigation menu on the left side of the page to open the web services console. I clicked the “SCI Targets” button in the web services console toolbar and selected my gateway from the “Add Targets” drop down and clicked the “Add” button so that code generated by the web services console would target my gateway. This step isn’t necessary, but it does make testing web service calls a little easier.||Share Your Ideas —
The iDigi Applications Team wants to how you’re using the iDigi Device Cloud®. Send us your applications, story requests or questions. Contact us at firstname.lastname@example.org.
I selected “Examples > SCI > Python Callback” from the web services console toolbar to generate the xml needed to send SCI callbacks. In the xml file I changed the target attribute on the do_command element from “rci_callback_example” to “idigi_dia”. I replaced the “ping” text with “”. This xml element tells iDigi to send the serial command of “abc” to the xbee_serial_terminal0.write channel on the gateway, which in turn will send the “abc” serial command to the Arduino project. I clicked the “Send” button on the web services console to send the channel_set command, entered my iDigi username and password when prompted, and then waited for a 200 response (200 is the HTTP code for success) to show up in the “Web Services Responses” window. When I sent the “abc” serial command, the first 3 LEDs on my Arduino project turned on, letting me know that the hardware was properly configured.
I decided to use Google App Engine to create a mobile web site for this project. Google App Engine is free to use for small web sites and has great documentation and tutorials to help you get started. I used the PyDev Eclipse plug-in to write the Google App Engine code. I used the Google App Engine launcher that is included in the App Engine SDK to create a new project name “mg-serial2″. In Eclipse, I selected “File > New > Other…”, selected “PyDev Google App Engine Project” from the new project wizard to create a corresponding PyDev project for my “mg-serial2″ web application. You can find documentation online from Google and PyDev on how to create Google App Engine projects.
My Google App Engine project started with three files:
The first two files contain application and database configuration information and they didn’t need to be modified for this project. The main.py file is responsible for matching URLs from incoming requests and routing them to the correct request handler class. Each request handler class has methods for handling the different kinds of HTTP requests such as get and post requests.
I created a templates folder to store the markup for the HTML form. I like to keep all my page templates in a templates folder to keep the project tidy. I added an index.html file with the following markup:
<title>iDigi Serial Communication Demo</title>
<meta name=”viewport” content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;” />
<h3>Send Serial Command</h3>
<form action=”/” method=”POST”>
<p><label for=”command”>Serial Command:</label><br/>
<input type=”text” name=”command” id=”command” /></p>
<p><input type=”submit” value=”Send” /></p>
The markup for the template page is pretty simple. The viewport meta tag in the head of the HTML document will make this form render at full size on mobile devices. The form has a single text input for ASCII serial commands and a submit button.
Most of the programming for this project is found in the main.py file. The code for this file is available for download from the Digi web site so I will just highlight my changes rather than printing out every line of code:
- Constants were added at the top of the file for my iDigi username and password, the device id for my ConnectPort X gateway, the channel name for my channel_set command, the URL for the iDigi SCI callback web service, and a template string for the xml file I send with my web service requests.
- The get method was modified so that it renders the HTML form from the templates folder.
- A static method was added to the MainHandler class, It is called “send_serial_command” and is responsible for sending the “channel_set” command to the iDigi Developer Cloud.
- I also added a post method to the MainHandler class that reads the serial command from the HTML form, calls the “send_serial_command” static method, then redirects back to the HTML form.
The “send_serial_command” method first creates a basic authentication header by base 64 encoding my iDigi username and password. It then adds the device id, channel name and serial command to the xml template for my channel_set command. It uses the Google App Engine urlfetch library to send an authenticated HTTP POST request containing the xml for the channel_set command to the iDigi Developer Cloud. Any result returned from the iDigi Developer Cloud is then returned to whatever code calls the “send_serial_command” method.
The final step for this project was to test it locally using the Google App Engine launcher utility before deploying it to the Google servers. I sent the command “fa” from the HTML form, which turned off all but the first LED on my Arduino project. Everything worked as expected so I again used the launcher utility to deploy my web application. You can find project deployment instructions on the Google App Enginesite.
Source Code for Google App Engine Project
It is simple to set up a mobile web page that sends ASCII serial commands to remote devices using the iDigi Developer Cloud and iDigi Dia. You can download the source code for my Google App Engine project, simply click here.
Digi’s team of application development experts can help you save valuable time and resources by developing a custom cloud-based remote device management solution specific to your needs. More >
Provide key performance indicators (KPIs) and full drill-down analytics for real-time information
- Personalize dashboard and reports
- Share information and collaborate on operations data
- Integrate tools with other backend systems
Enable Users to remotely access information to instantly ascertain the status of operations
- Eliminate unnecessary site visits
- Troubleshoot issues
- Increase productivity
Maintain service schedules, power usage and routines for your valuable assets
- Send alerts before outage occurs
- Manage asset rotation
- Increase profitability
New — The iDigi Gateway Development Kit is designed to make it easy to setup a ZigBee network, upload a custom iDigi Device Integration Application and provide seamless connectivity to the iDigi Device Cloud for Web services integration to standard business applications over the Internet. More >
iDigi Garden — We’ve been providing updates on the iDigi Garden throughtout the Spring and Summer months and now we are almost ready to bring in the produce.
Not only is the iDigi Garden a great source of fresh tomatoes and other vegetables, but it’s also a great way to demonstrate the viability of Digi’s wireless solutions.
- XBee modules bring wireless connectivity to sensors, regulators and other devices
- ConnectPort X gateways provides two-way communication with the XBee-enabled devices
- The iDigi Device Cloud enables on-demand, anywhere access and device management
Follow Digi on Twitter, Facebook and YouTube.