u2_usb: add influxdb support

This commit is contained in:
King Kévin 2020-01-09 11:21:02 +01:00
parent 6f7a7a42c3
commit b579959a47
1 changed files with 46 additions and 5 deletions

View File

@ -26,6 +26,8 @@
#include <string.h>
#include <stdarg.h>
#include <hidapi/hidapi.h>
#include <netdb.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
static void die(const char* format, ...)
@ -104,10 +106,38 @@ int main(int argc, char* argv[])
int rc; // return code
char str[200]; // general strings
wchar_t wstr[100]; // wide strings (used by hidapi)
bool debug = false; // print debug information
bool debug = false;
if (2 == argc && 0 == strcmp("-d", argv[1])) {
debug = true;
if (argc >= 2) {
if (0 == strcmp(argv[1],"-h")) {
printf("%s [-d|-h] [host port]\n", argv[0]);
printf("connect to WEB/WITRN/QWay U2 over USB and output measurements as CSV\n\n");
printf("\t-h\tprint help\n");
printf("\t-d\tenable debug output (disables CSV output)\n");
printf("\thost port\tinfluxDB UDP host and port to send voltage/current measurements to\n");
exit(0);
} if (0 == strcmp(argv[1],"-d")) {
debug = true;
}
}
// UDP socket to send data to influxdb
int influxdb_fd = 0;
struct addrinfo* res = 0;
if (argc >= 3) {
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags = AI_ADDRCONFIG;
if (0 != getaddrinfo(argv[argc -2], argv[argc - 1], &hints, &res)) {
die("failed to resolve remote socket address\n");
}
influxdb_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
if (0 == influxdb_fd) {
die("could not create UDP socket\n");
}
}
rc = hid_init();
@ -156,8 +186,11 @@ int main(int argc, char* argv[])
printf("timer (in s),super fast timer,fast timer,timer,slow timer,VBUS voltage 1 (in V),VBUS current 1 (in A),VBUS current 2 (in A),VBUS current 3 (in A),VBUS power (in W),D+ voltage (in V),D- voltage (in V),internal temperature (in °C),external temperature 8in °C),VBUS voltage 2 (in V),VBUS current 4 (in A)\n");
}
bool run = true;
struct u2_measurement_t meas;
bool run = true; // read from USB as long as true
struct u2_measurement_t meas; // to store the parsed measurement values
// line containing values for influxdb
char line[21 * 2];
int line_len;
while (run) {
rc = hid_read_timeout(handle, meas.payload, ARRAY_SIZE(meas.payload), 50); // wait for max 50 ms for a measurement
if (rc < 0) {
@ -251,8 +284,16 @@ int main(int argc, char* argv[])
printf("\n");
}
}
if (influxdb_fd) {
line_len = snprintf(line, sizeof(line), "voltage value=%.04f\ncurrent value=%.04f\n", meas.vbus_voltage1, meas.vbus_current1);
if (-1 == sendto(influxdb_fd, line, line_len, 0, res->ai_addr, res->ai_addrlen)) {
die("could not send UDP packet\n");
goto close;
}
}
}
close:
hid_close(handle);
handle = NULL;
rc = hid_exit();