Ligne 3 : |
Ligne 3 : |
| {{traduction}} | | {{traduction}} |
| | | |
− | [https://github.com/UniPiTechnology/evok https://github.com/UniPiTechnology/evok] | + | == Introduction == |
| + | Voici différentes informations concernant l'API Evok tel que publié sur le [https://github.com/UniPiTechnology/evok GitHub d'Evok]. |
| + | |
| + | == Rest API == |
| + | === HTTP Get === |
| + | To get a state of a device HTTP GET request can be send to the evok |
| + | |
| + | GET /rest/DEVICE/CIRCUIT |
| + | |
| + | or |
| + | |
| + | GET /rest/DEVICE/CIRCUIT/PROPERTY |
| + | |
| + | Where DEVICE can be substituted by any of these: 'relay', 'di' or 'input', 'ai' or 'analoginput, 'ao' or 'analogoutput', 'sensor', CIRCUIT is the number of circuit (in case of 1Wire sensor, it is its address) corresponding to the number in your configuration file and PROPERTY is mostly 'value'. |
| + | |
| + | Simple example using wget to get status of devices: |
| + | |
| + | * wget -qO- http://your.pi.ip.address/rest/all <br />returns status of all devices configured in evok.conf |
| + | * wget -qO- http://your.pi.ip.address/rest/relay/1 <br />returns status of relay with circuit nr. 1 |
| + | * wget -qO- http://your.pi.ip.address/rest/relay/1/value <br />returns whether the relay 1 is on or of (1/0) |
| + | * wget -qO- http://your.pi.ip.address/rest/ao/1/value <br />returns the value of analog output |
| + | * wget -qO- http://your.pi.ip.address/rest/ai/1/value <br />returns the value of analog input |
| + | |
| + | === HTTP Post === |
| + | To control a device, all requests must be sent by HTTP POST. Here is a small example of controlling a relay: |
| + | |
| + | * wget -qO- http://your.pi.ip.address/rest/relay/3 --post-data='value=1' <br />sets relay on |
| + | * wget -qO- http://your.pi.ip.address/rest/relay/3 --post-data='value=0' <br />sets relay off |
| + | * wget -qO- http://your.pi.ip.address/rest/ao/1 --post-data='value=5' <br />set AO to 5V |
| + | |
| + | |
| + | == WebSocket == |
| + | Register your client at ws://your.unipi.ip.address/ws to receive status messages. Once it is connected, you can also send various commands to the UniPi All messages in websocket are sent in JSON string format, eg. {"dev":"relay", "circuit":"1", "value":"1"} to set Relay 1 On. Check the wsbase.js in www/js/ folder to see example of controlling the UniPi using websocket. |
| + | |
| + | === Python using JsonRPC === |
| + | You can also control the UniPi using Python library jsonrpclib. See the list of all available methods below. |
| + | |
| + | <syntaxhighlight lang="python"> |
| + | from jsonrpclib import Server |
| + | s=Server("http://your.pi.ip.address/rpc") |
| + | s.relay_set(1,1) |
| + | s.relay_get(1) |
| + | s.relay_set(1,0) |
| + | s.relay_get(0) |
| + | s.ai_get(1) |
| + | </syntaxhighlight> |
| + | |
| + | === Python using WebSocket === |
| + | |
| + | <syntaxhighlight lang="python"> |
| + | import websocket |
| + | import json |
| + | |
| + | url = "ws://your.unipi.ip.address/ws" |
| + | |
| + | def on_message(ws, message): |
| + | obj = json.loads(message) |
| + | dev = obj['dev'] |
| + | circuit = obj['circuit'] |
| + | value = obj['value'] |
| + | print message |
| + | |
| + | def on_error(ws, error): |
| + | print error |
| + | |
| + | def on_close(ws): |
| + | print "Connection closed" |
| + | |
| + | #receiving messages |
| + | ws = websocket.WebSocketApp(url, on_message = on_message, on_error = on_error, on_close = on_close) |
| + | ws.run_forever() |
| + | |
| + | #sending messages |
| + | ws = websocket.WebSocket() |
| + | ws.connect(url) |
| + | ws.send('{"cmd":"set","dev":"relay","circuit":"3","value":"1"}') |
| + | ws.close() |
| + | </syntaxhighlight> |
| + | |
| + | === Perl using JsonRPC |
| + | |
| + | A simple example of controlling the UniPi via RPC use JSON::RPC::Client; |
| + | |
| + | <nowiki>use JSON::RPC::Client; |
| + | |
| + | my $client = new JSON::RPC::Client; |
| + | my $url = 'http://your.pi.ip.address/rpc'; |
| + | |
| + | $client->prepare($url, ['relay_set']); |
| + | $client->relay_set(1,1);</nowiki> |
| + | |
| + | There is also a [https://metacpan.org/pod/AnyEvent::WebSocket::Client websocket client library for Perl] to get more control. |
| + | |
| + | == Liste des périphériques disponibles == |
| + | * '''relay''' - Relais |
| + | * '''input''' ou '''di''' - Entrée digitale |
| + | * '''ai''' - Entrée analogique |
| + | * '''ao''' - Sortie analogique |
| + | * '''ee''' - eeprom de la carte |
| + | * '''sensor''' - Senseur 1wire |
| + | |
| + | Le reste peut être trouvé dans le fichier {{fname|devices.py}} |
| + | |
| + | == Liste des méthodes disponibles == |
| + | Les méthodes ci-dessous (et plus encore) peuvent être trouvé dans les fichiers source evok.py ou owclient.py. |
| + | |
| + | * Entrées digitales |
| + | ** {{fname|input_get(circuit)}} - get all information of input by circuit number |
| + | ** {{fname|input_get_value(circuit)}} - get actual state f input by circuit number, returns 0=off/1=on |
| + | ** {{fname|input_set(circuit)}} - sets the debounce timeout |
| + | * Relais |
| + | ** {{fname|relay_get(circuit)}} - get state of relay by circuit number |
| + | ** {{fname|relay_set(circuit, value)}} - set relay by circuit number according value 0=off, 1=on |
| + | ** {{fname|relay_set_for_time(circuit, value, timeout)}} - set relay by circuit number according value 0=off, 1=on for time(seconds) timeout |
| + | * Entrée analogique |
| + | ** {{fname|ai_get(circuit)}} - get value of analog input by circuit number |
| + | ** {{fname|input_get |
| + | * Sortie analogique |
| + | ** {{fname|ao_set_value(circuit, value)}} - set the value(0-10) of Analog Output by circuit number |
| + | * Bus 1-Wire |
| + | ** {{fname|owbus_scan(circuit)}} - force to scan 1Wire network for new devices |
| + | * Senseurs 1-Wire |
| + | ** {{fname|sensor_get(circuit)}} - returns all information in array [value, is_lost, timestamp_of_value, scan_interval] of sensor by given circuit or 1Wire address |
| + | ** {{fname|sensor_get_value(circuit)}} - returns value of a circuit by given circuit or 1Wire address |
| + | |
| {{UniPi-TRAILER}} | | {{UniPi-TRAILER}} |