Python-based SmartPlug Sensor Example

From Digi Developer

Jump to: navigation, search

Overview
This simple Web Services example uses the Digi Smartplug (remote power management) sensor (light/temperature/current), is written in Python, and displays the result of the most recently available samples via html. The Digi Smartplug contains a relay, so this example includes both the ability to view samples, as well as the option to "set" the relay to a power-on or power-off state. The DIA configuration file, when used with the DIA application running on a Digi ConnectPort gateway, defines the RCI presentation. The RCI presentation is a web service method. When you provide the proper credentials and target information, DIA will listen for web services requests to sample or set the power relay on the sensor.

Even if you don't have a Digi Smartplug you can still easily adapt this code for other sensors. The principle is the same. You are simply sending a web service request to Device Cloud in order to get or set sensor channels. If a sample is available, a "channel_get" (or channel_dump) request will return the most recent value.
Image:smartplug_screenshot01.png


Instructions:
Download the zip file Media:SmartPlug.zip, which includes the Python source and images. There are two (2) python versions. One that provides a basic "channel_dump" of all attached sensors, and another version which very specifically requests (channel_get) only the specific sensor and channels (light, temperature, current, power_on). Place the python code in your web server's "cgi-bin" directory and the images in the appropriate image location. You may need to change some of the image URL references. You will also want to change the references to the deviceID and possibly the Device Cloud connectivity server URL.

Code Flow:
The code flow is simple, the smartplug.py code draws an html form. You enter the required information and click "Go." The same smartplug.py or smartplugV2.py program is called, this time reading the form variables. A web service call to Device Cloud is now created and submitted. Device Cloud receives the request, validates (checks permission) and sends to the target device. The target device (Digi gateway), running DIA, has a handler (callback) which is listening for a web service call (RCI). Upon receiving a request, it either provides the "channel_dump," "channel_get," or "channel_set" information and response back up to Device Cloud. Device Cloud then forwards the response to the requester (your smartplug.py or smartplugV2.py program), which then parses the XML response and displays the information on your browser.

DIA Configuration:
Here's an example DIA configuration file (yml) that will work with this example. Copy, modify and use with your own DIA application.

# This configuration file is used to specify which components will be
# specified for use with the DIA Smartplug Demo.
# Essentially, we will be selecting the sources of information, and how
# that information is presented.  

# The devices section specifies settings for devices
devices:

# First we specify an XBee Device Manager.  Since the XBee is considered,
# a shared resource on our system, we need a device which can manage
# requests from other devices relating to the XBee.
  - name: xbee_device_manager
    driver: devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager

# Here we specify that we have a Digi Smart Plug as a device.  In the
# settings, we specify the parent XBee Device Manager for this device,
# as well as the extended address of the actual node that is associated
# to our gateway.  We then specify that we would like the Smart Plug
# to send up sensor readings once a second, and that when DIA starts,
# the outlet on the Smart Plug should be turned on.
  - name: smart_plug_1
    driver: devices.xbee.xbee_devices.xbee_rpm:XBeeRPM
    settings:
        xbee_device_manager: xbee_device_manager
        extended_address: "00:13:a2:00:40:34:0c:6c!"
        sample_rate_ms: 2000
        idle_off_seconds: 120
        default_state: "Off"
 
 
# The presentation section allows us to specify the ways in which
# information in our system is available.
presentations:        

# Here we define a standard console method.  Using this,
# someone can connect (with telnet in Windows for example),
# to get a quick and simple look at the current state of the
# system.  They can view the status of all channels, and set
# any channels that support being configured.
  - name: console
    driver: presentations.console.console:Console
    settings:
        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_dia.html".  See the file src/presentations/web/web.py form more
# information.
  - name: web0
    driver: presentations.web.web:Web
    settings:
        page: idigi_dia.html

# 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
    settings:
	target_name: digi_dia


Running the Demo
Call the Python code (smartplug.py) from your web browser. Fill in the blanks and click on "Go."

  • Username: Your Device Cloud username
  • Password: Your Device Cloud password


Conclusion
That's it! Pull the code apart. Experiment. Improve it. Everything's inside the one python code module. You'll find the web services function, the html display (near the end) and the XML for parsing the Device Cloud response. There's even a very simple graph builder, using 1x1 pixel colors to illustrate the light, temperature and current sample values.

Personal tools
Wiki Editing