Interfacing custom applications with the Flexs Q4
The Flexs Q4 is an open device with support for a wide array of protocols allowing easy and flexible interfacing with almost any platform or environment.
Each method is outlined below in detail.
HTTP API
The HTTP API allows for reading input values, setting relay output states and modifying all configuration settings such as analog input gain, logic rules, etc.
Examples:
READ ANALOG INPUT CH1 (REGISTER 1) Value
GET /cmd.shtm?cmd=getreg&
reg=1
READANALOG INPUT CH8 (REGISTER 8) Value
GET /cmd.shtm?cmd=getreg&
reg=8
READ RELAY #2 (Register 10) Output Status
GET /cmd.shtm?cmd=getreg&
reg=10
SET LOAD SWICH #2 (Register 10) to 1 (ON)
GET /cmd.shtm?cmd=setreg&
reg=10&val=1
SET LOAD SWICH #2 (Register 10) to 0 (OFF)
GET /cmd.shtm?cmd=setreg®=10&val=0
Authentication
The Flexs Q4 uses ‘BASIC AUTH’ authentication, if you are using a password on your device these headers must be added to your request, if you are making the requests in your web browser it should automatically prompt for a username and password
SNMP
SNMP V1 is supported only for reading analog input values and relay output states. SNMP is compatible with a wide array of opensource monitoring, data logging and visualization software including Cacti, Zabbix and more!
The SNMP community is modified on the ‘Configuration’ tab and optionally can be set blank to disable the snmp service.
To get the voltage on input channel 2 (register #2) the following command can be used.
snmpget -v1 -c “public” 162.216.184.183 1.3.6.1.4.1.4128.2
iso.3.6.1.4.1.4128.1 = STRING: “12.9867”
To get the status of relay output 1 (register #9) the following command can be used.
snmpget -v1 -c “public” 162.216.184.183 1.3.6.1.4.1.4128.9
iso.3.6.1.4.1.4128.9 = STRING: “1”
UDP
Data can be streamed via UDP from the Flexs Q4 device when enabled on the ‘Configuration’ tab, a valid endpoint URL is required which can include an IP Address or hostname.
Data is streamed to the specified endpoint in a packed struct of the following composition. Standard Little Indian, 32Bit IEE754 Float
typedef struct
{
uint64_t uid; // 64bit unsigned integer, uuid of device
char hash[32];// SHA256 hash of data + password + uid
float data[128]; //register data
} Registers;
If you are using a language other than ‘C’ or ‘C++’ the bytes come in with the following structure:
Bytes 0-7: Unsigned 64 bit integer containing Device UUID
Bytes 8-31: Byte array containing SHA256 hash of data + password + uuid
Bytes 32-35: 32bit single precision float representation of Register 0’s Value
Bytes 36-39: 32bit single precision float representation of Register 1 (Analog Input Ch 1) ‘s value
… +126 More Registers
The Flexs Q4 also listens for UDP datagrams on port 8001 which can be used to send commands to the Flexs Q4 for relay control.
Since outgoing udp stream packets originate from port 8001, a listening server can reply with a command after receiving a stream data packet, thus removing the need for port forwarding on the end device.
The format for a UDP Set Register Command Packet is detailed below
typedef struct
{
unsigned char hash[32]; // First write the device password to this array, then compute a sha256 bit hash of this entire structure and place the result inside this array
uint32_t reg; // register we are setting
float value; // Value to set register to
uint32_t timestamp; // 32Bit Unix Epoch Timestamp
} WREG_CMD;
When the Flexs Q4 receives a set register command it will create a checksum based on the same algorithm and compare against the received checksum, this prohibits unsecured access and insures that the values present in the array were not manipulated, the timestamp is also compared with the flexs Q4’s time and must be within 100 seconds or the command will be ignored.
An example C++ listener / control service is available free of charge upon request.
SERVER POST
Analog Input values, relay output states and other data can be sent to a server as a post request, this method allows you to easily process the data with PHP, .NET, CGI Scripting or other server side scripting languages.
Requests are made to the streaming endpoint url specified on the configuration page, an IPv4 address or hostname can be used.
Data is sent in the JSON format with a unique device identifier and an array containing the values of each of the registers.
EXAMPLE POST DATA:
{
“uid”:3249352961,
“registers”:[0,0.0018,13,0.0033,0.001,0.0043,0.00092,0.0011,-3.1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,23,23,34,30,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6.4e+03,21,22,5,9,9,3.9e+04,1e+03] }
Example PHP Listener script which logs the value of analog input channel 1 (register 1) to a text file on the server named log.txt, logging to MYSQL or other database types could easily be accomplished from this point
<?php
$json = json_decode ( file_get_contents(“php://input”));
$fh = fopen(“log.txt”, ‘a’) or die(“can’t open file”);
fwrite($fh, $json->registers[1] . “\n”);
?>