-
Notifications
You must be signed in to change notification settings - Fork 8
IonoWeb
This library provides functions to control and monitor Iono Ethernet via a HTTP-based API.
Import the library in your sketch:
#include <IonoWeb.h>
The library provides the following methods:
IonoWeb.begin(int port)
Start the Web server on the specified port number.
IonoWeb.processRequest()
This method must be called periodically (inside the loop()
function) in order to serve the incoming HTTP requests.
IonoWeb.subscribe(unsigned long stableTime, float minVariation, char *host, int port, char *command, uint8_t mode1, uint8_t mode2, uint8_t mode3, uint8_t mode4)
This method can be used to enable push notifications to monitor iono's state: iono will trigger a HTTP request to a remote host whenever a pin changes state.
After calling this method, every time a pin changes state and it is stable for a time equal to stableTime
(in milliseconds), iono will perform a HTTP request to the specified port
of the specified host
. For inputs read as voltage or current the call will be triggered only if the value changes of more than minVariation
.
The parameters mode1
...mode4
specify how to read input 1 ... 4: a value of '1' means read as digital (i.e. DI1
...DI4
), '2' means read as voltage (i.e. AV1
...AV4
) and '3' means read as current (i.e. AI1
...AI4
).
The HTTP request will have the following format:
http://<host>:<port><command>?<pin>=<val>
For instance, after calling:
IonoWeb.subscribe(100, 0.1, "192.168.0.200", 8080, "/bar", 1, 1, 2, 3);
if input DI1
goes high and stays high for 100ms, iono will perform a HTTP request equivalent to this:
http://192.168.0.200:8080/bar?DI1=1
if the voltage on AV3
changes of a value greater or equal to 0.1V (e.g. from 5.3V to 5.4V), iono will perform a HTTP request equivalent to this:
http://192.168.0.200:8080/bar?AV3=5.40
The HTTP API provides for the following three actions:
Making a HTTP request to this path, like the following:
http://192.168.0.100/api/state
will return a JSON object with the following format:
{
"DO1": <do1_val>,
"DO2": <do2_val>,
"DO3": <do3_val>,
"DO4": <do4_val>,
"DO5": <do5_val>,
"DO6": <do6_val>,
"I1": {
"D": <di1_val>,
"V": <av1_val>,
"I": <ai1_val>
},
"I2": {
"D": <di2_val>,
"V": <av2_val>,
"I": <ai2_val>
},
"I3": {
"D": <di3_val>,
"V": <av3_val>,
"I": <ai3_val>
},
"I4": {
"D": <di4_val>,
"V": <av4_val>,
"I": <ai4_val>
},
"I5": {
"D": <di5_val>
},
"I6": {
"D": <di6_val>
}
}
which reports the current value of iono's pins.
An HTTP request to this path with a set of pairs <pin_name>=<val>
as parameters can be used to control iono's outputs.
Here is a few examples:
http://192.168.0.100/api/set?DO1=1
http://192.168.0.100/api/set?DO1=0&DO4=f
http://192.168.0.100/api/set?DO1=0&DO2=1&DO3=0&DO4=1&DO5=0&DO6=0&AO1=7.3
For digital outputs the allowed values are '0' (low), '1' (high) or 'f' (flip the current state); for analog ones, the allowed values are floating numbers from 0.0 to 10.0.
This HTTP request must be used following this format:
http://192.168.0.100/api/subscribe?st=<time>&mv=<var>&host=<host>&port=<port>&cmd=<command>&mode1=<mode1>&mode2=[d|v|i]&mode3=[d|v|i]&mode4=[d|v|i]
The above request corresponds to a call to the following method:
IonoWeb.subscribe(<time>, <var>, <host>, <port>, <command>, <mode1>, <mode2>, <mode3>, <mode4>);
where <mode1>
...<mode4>
are set to '1', '2' or '3' if the corresponding parameters are respectively set to 'd', 'v' or 'i'
For instance, this request:
http://192.168.1.243/api/subscribe?mv=0.1&st=100&host=192.168.0.200&port=8080&cmd=/bar&mode1=d&mode2=d&mode3=v&mode4=i
corresponds to a call to:
IonoWeb.subscribe(100, 0.1, "192.168.0.200", 8080, "/bar", 1, 1, 2, 3);
The response will contain a JSON object with the current state as in /api/state.
The subscription will timeout after 60 seconds, so it must be renewed periodically.
Sfera Labs - www.sferalabs.cc
Apps
Other resources
Libraries API