The PHP Widget Control API is a PHP script that resides on your own server. All you have to do is place the source code into a file named iobridge-proxy.php and call it from your own scripts and applications. The script acts as a proxy between your applications and the ioBridge widgets. You can use this script from anything that can GET a URL including same-site AJAX scripts, JavaScript, VisualBasic, Desktop Widgets, Python, PHP, Perl, etc.
All widget types are supported. If a widget returns a value, the values are also passed through the API.
A widget that has a single button and when it's pressed executes a single action falls into this category. All you have to do is pass the widgetID or the widget label to execute these widgets through the API.
Widgets that require a parameter to execute are variable widgets. You have to send the widgetID or label and the value in the URL. Works for variable pulse widgets, servo widgets, and serial widgets.
Some widgets have On/Off states. To execute the Off state pass the widget ID and the state 0. To execute the On state pass the widgetID and the state of 1. If you use the widget label method, you will have to get the URL for both states and hard code them into the switch/case section of the script. Use these settings for controlling digital outputs and X10 widgets.
If you want to get the state of an digital output / input or analog input, pass the widgetID or label and the script returns the scaled or labeled values. The scale is dependent on what the widget has been set to.
Before you get started, create widgets using the ioBridge Interface and note their widgetIDs. We will use these IDs with the API to control or access the associated widget.
Some parameters are required to use the API.
You can either send a widgetID or a widget label to control widgets. Using a widgetID is a little slower than hard setting the widget URLs with a widget label.
http://your-server.com/iobridge-proxy.php?widgetID=XXXXXXXXXX&value=2000
http://your-server.com/iobridge-proxy.php?widget=servo&value=2000
| Parameter | Usage |
|---|---|
| widgetID | WidgetID or your widget |
| widget | Case Label for quick access to associated widget |
| value | Pass a variable widget a value, like setting a servo position |
| state | Set to 0 for the Off state and 1 for the On state of an Output Control Widget |
| debug | Set to display widget URL, requires widgetID to also be passed |
If you choose to use the Widget Label method, you will have to obtain the widget URLs for each of your widgets. The URL is easy to obtain with the Proxy Script. This method is a bit more complex, but when it's all setup you gain 30-40% faster and the URLs will be easier to understand.
To get the URL that is associated widget, pass the widgetID and set the debug parameters from a web browser.
http://your-server.com/iobridge-proxy.php?widgetID=XXXXXXXXXX&debug=1
Returns:
http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX
The returned URL is a snap shot of the widget with the current settings. The URL is valid as long as it is being used regularly - sessionIDs expire with inactivity. If you make changes to the widget, you will have to obtain a new widget URL.
Once you have a valid widget URL you can hard code it to the Switch/Case section of the script, tag it with a label, and access it from the proxy by its label. We recommend adding a comment to the PHP script that has the orgignal widgetID so you can easily find it for later use.
<?php /** * ioBridge Proxy that forwards calls to your ioBridge widgets * * @version 1.1 July 3, 2009 - Added URL debug and "widget" select case by ioBridge Support * @version 1.0 June 21, 2009 - Original version Marc Fonteijn <marcfonteijn[ at ]gmail.com> * * @link http://www.iobridge.net/forum/index.php/topic,447.0.html Comments & suggestions * @license http://creativecommons.org/licenses/GPL/2.0/ Released under a Creative Commons GNU GPL License */ $widgetID = $_GET['widgetID']; $widget = $_GET['widget']; $state = $_GET['state']; $value = $_GET['value']; $debug = $_GET['debug']; $value = urldecode($value); if ($widgetID) { $url = "http://www.iobridge.com/interface/?actionID=RM_Widget&widgetID=" . $widgetID . "&dt=" . time(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1'); $response = curl_exec($ch); curl_close($ch); $start = strpos($response, "www.iobridge.com/interface/?actionID="); if(isset($state) && $state == "0") $start = strpos($response, "www.iobridge.com/interface/?actionID=", $start + 1); $end = strpos($response, ";", $start); $widgetUrl = "http://" . substr($response, $start, $end - $start - 4); if ($debug) { echo $widgetUrl; exit; } } else { switch ($widget) { case "toggle": $widgetUrl = "http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX"; break; case "analog": $widgetUrl = "http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX"; break; case "off": $widgetUrl = "http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX"; break; case "on": $widgetUrl = "http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX"; break; case "servo": $widgetUrl = "http://www.iobridge.com/interface/?actionID=RM_XXXXXXXXXXXX&sessionID=XXXXXXXXXXXXXXXXXXXX"; break; } } if ($widgetUrl) { $widgetUrl .= "&setValue=" . urlencode($value); $ch = curl_init($widgetUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1'); $response = curl_exec($ch); $start = strpos($response, "="); $end = strpos($response, ";", $start); echo substr($response, $start + 2, $end - $start - 3); } ?>
The PHP Proxy script is perfect for when you need to control the module without the use of JavaScript. Older mobile clients on a standard mobile phone can't process client side scripting, but they often support HTML.
You can use the proxy with links or HTML forms.
<html> <head> <title>PHP Proxy with Links</title> </head> <body> <a href="iobridge-proxy.php?widget=LED&value=Hello+World" target="iframe">Send Message to LCD</a> | <a href="iobridge-proxy.php?widget=servo&value=1000" target="iframe">Servo Left</a> | <a href="iobridge-proxy.php?widget=servo&value=2000" target="iframe">Servo Right</a> <iframe id="iframe" name="iframe" width="0" height="0" frameborder="0"></iframe> </body> </html>
<html> <head> <title>PHP Proxy with HTML Form</title> </head> <body> <form action="http://your-server.com/iobridge-proxy.php" method="get" target="iframe"> <textarea row="2" cols="16" id="value" name="value"></textarea><br /> <input type="hidden" name="widget" value="serial" /> <input type="submit" value="Submit" /> </form> <iframe id="iframe" name="iframe" width="0" height="0" frameborder="0"></iframe> </body> </html>