single influxdb query per payload

This commit is contained in:
King Kévin 2015-11-10 12:19:42 +01:00
parent b583481c4f
commit 5ec80702c0
1 changed files with 18 additions and 47 deletions

View File

@ -71,6 +71,7 @@ int main(int argc, char** argv){
uint8_t payload[32]; // buffer to save the payload
uint8_t size = radio.getDynamicPayloadSize();
radio.read(&payload,size);
/*
printf("got %d bytes:",size);
for (uint8_t i=0; i<size; i++) {
@ -78,10 +79,11 @@ int main(int argc, char** argv){
}
printf("\n");
*/
// got through payload
uint8_t id = 0; // the meter id (0 is myself, used for unknown source)
float voltage, current, power, energy; // the values coming from the meter
CURLcode res = CURLE_OK; // curl response
char post[128] = {0}; // string to submit data to DB using POST request
uint8_t i = 0; // index within the data
while (i+2<size) {
uint8_t type = payload[i]; // type of IE
@ -91,67 +93,26 @@ int main(int argc, char** argv){
case 0: // id
if (length==1) {
id = payload[i+2];
printf("meter: %d\n",id);
}
break;
case 1: // voltage
if (length==4) {
float value = 0;
memcpy(&value,&payload[i+2],length);
printf("voltage: %f V\n",value);
if (curl) {
snprintf(post, sizeof(post), "voltage,meter=%d value=%f", id, value);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
//res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
memcpy(&voltage,&payload[i+2],length);
}
break;
case 2: // current
if (length==4) {
float value = 0;
memcpy(&value,&payload[i+2],length);
printf("current: %f A\n",value);
if (curl) {
snprintf(post, sizeof(post), "current,meter=%d value=%f", id, value);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
//res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
memcpy(&current,&payload[i+2],length);
}
break;
case 3: // power
if (length==4) {
float value = 0;
memcpy(&value,&payload[i+2],length);
printf("power: %f W\n",value);
if (curl) {
snprintf(post, sizeof(post), "power,meter=%d value=%f", id, value);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
//res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
memcpy(&power,&payload[i+2],length);
}
break;
case 4: // energy
if (length==4) {
float value = 0;
memcpy(&value,&payload[i+2],length);
printf("energy: %f Wh\n",value);
if (curl) {
snprintf(post, sizeof(post), "energy,meter=%d value=%f", id, value);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
//res = curl_easy_perform(curl);
if(res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
memcpy(&energy,&payload[i+2],length);
}
break;
default:
@ -162,8 +123,18 @@ int main(int argc, char** argv){
i = size; // end the loop
}
}
printf("meter: %d, voltage: %f V, current: %f A, power: %f W, energy: %f Wh\n",id,voltage,current,power,energy);
if (curl) {
char post[128*4] = {0}; // string to submit data to DB using POST request
snprintf(post, sizeof(post), "voltage,meter=%d value=%f\ncurrent,meter=%d value=%f\npower,meter=%d value=%f\nenergy,meter=%d value=%f\n", id, voltage, id, current, id, power, id, energy);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);
res = curl_easy_perform(curl);
if (res!= CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
}
usleep(10000); // wait 10ms before request if data is available again (the IRQ signal is not used)
usleep(100000); // wait 100ms before request if data is available again (the IRQ signal is not used)
}
if (curl) {