forked from openhab/openhab1-addons
-
Notifications
You must be signed in to change notification settings - Fork 4
Samples REST
dakalff edited this page Nov 26, 2013
·
3 revisions
Examples for accessing REST API
Please see below sample code for accessing openhab's REST API.
Accessing REST API via jquery (tested with jquery 2.0 and Chrome v26.0
Read state of an item:
function getState()
{
var request = $.ajax
({
type : "GET",
url : "http://192.168.100.21:8080/rest/items/MyLight/state"
});
request.done( function(data)
{
console.log( "Success: Status=" + data );
});
request.fail( function(jqXHR, textStatus )
{
console.log( "Failure: " + textStatus );
});
}
Set state of an item:
function setState( txtNewState )
{
var request = $.ajax
({
type : "PUT",
url : "http://192.168.100.21:8080/rest/items/MyLight/state",
data : txtNewState,
headers : { "Content-Type": "text/plain" }
});
request.done( function(data)
{
console.log( "Success" );
});
request.fail( function(jqXHR, textStatus )
{
console.log( "Failure: " + textStatus );
});
}
Send command to an item:
function sendCommand( txtCommand )
{
var request = $.ajax
({
type : "POST",
url : "http://192.168.100.21:8080/rest/items/MyLight",
data : txtCommand,
headers : { 'Content-Type': 'text/plain' }
});
request.done( function(data)
{
console.log( "Success: Status=" + data );
});
request.fail( function(jqXHR, textStatus )
{
console.log( "Failure: " + textStatus );
});
}
Accessing REST API via cURL. cURL is useful on shell scripts (Win/Linux/OS X) or e.g. on Automator (OS X).
Get state:
curl http://192.168.100.21:8080/rest/items/MyLight/state
Set state:
curl --header "Content-Type: text/plain" --request PUT --data "OFF" http://192.168.100.21:8080/rest/items/MyLight/state
Send command:
curl --header "Content-Type: text/plain" --request POST --data "ON" http://192.168.100.21:8080/rest/items/MyLight
Simple PHP function to set a switch state using the REST interface.
Set state:
function doPostRequest($item, $data) {
$url = "http://192.168.1.121:8080/rest/items/" . $item;
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => $data //http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
Example function use:
doPostRequest("doorbellSwitch", "ON");
If the post was successful the function will return the state you set, EG above returns "ON"
Installation
Community
- Support
- News Archive
- Presentations
- How to Contribute
- IDE Setup
- How to Implement a Binding
- How to Implement an Action
- Projects using openHAB
- User Interfaces
- Classic UI
- iOS Client
- Android Client
- GreenT UI
- CometVisu
- Bindings
- Asterisk Binding
- Astro Binding
- Bluetooth Binding
- Comfo Air Binding
- CUL Binding
- CUPS Binding
- digitalSTROM Binding
- DMX512 Binding
- EnOcean Binding
- Epson Projector Binding
- Exec Binding
- Fritz!Box Binding
- Fritz AHA Binding
- GPIO Binding
- HDAnywhere binding
- Heatmiser Binding
- Homematic Binding
- HTTP Binding
- IHC / ELKO Binding
- Insteon Hub Binding
- Insteon PLM Binding
- Ir-Trans Binding
- KNX Binding
- Koubachi Binding
- MAX!Cube-Binding
- MiLight Binding
- Modbus TCP Binding
- MPD Binding
- MQTT Binding
- MQTTitude binding
- Neohub Binding (Preview)
- Netatmo Binding
- Network Health Binding
- Nibe Heatpump Binding
- Nikobus Binding
- Novelan/Luxtronic Heatpump Binding
- NTP Binding
- One-Wire Binding
- Onkyo AV Receiver Binding
- Open Energy Monitor Binding
- OpenPaths presence detection binding
- OpenSprinkler Binding
- OSGi Configuration Admin Binding
- Philips Hue Binding
- Piface Binding
- Pioneer-AVR-Binding
- Plugwise Binding
- PLCBus Binding
- Pulseaudio Binding
- RFXCOM Binding
- Samsung TV Binding
- Serial Binding
- Snmp Binding
- Squeezebox Binding
- System Info Binding
- Somfy URTSI II Binding
- Sonos Binding
- Swegon ventilation Binding
- TCP/UDP Binding
- Tellstick Binding
- TinkerForge Binding
- VDR Binding
- Velleman-K8055-Binding
- Wake-on-LAN Binding
- Withings Binding
- XBMC Binding
- xPL Binding
- Z-Wave Binding
- Persistence
- db4o Persistence
- rrd4j Persistence
- MySQL Persistence
- MongoDB Persistence
- Sen.Se Persistence
- Cosm Persistence
- Logging Persistence
- Exec Persistence
- MQTT Persistence
- Automation
- Scripts
- Rules
- Actions
- Misc
- REST-API
- Security
- Google Calendar Support
- Twitter Action
- Service Discovery
- Dropbox Bundle
Samples
- Item definitions
- Sitemap definitions
- Binding configurations
- Rules
- REST Examples
- Tips & Tricks
- FAQ
- XSLT Transforms
- Scripts
- Integration with other applications
- Syntax highlighting for external editors
- Update-Scripts
- Samples-Comfo-Air-Binding
- Samples WAC Binding
Release Notes