aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 12:19:42 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-10 12:19:42 +0100
commit5ec80702c058e71185d55812981c6d335c0b06a0 (patch)
tree1961a7a46029bea87977c381f94d6f8bd665f6db
parentb583481c4fd078ab68e68843e43acc4e69e50f44 (diff)
single influxdb query per payload
-rw-r--r--rpi/spark_counter_receiver.cpp65
1 files changed, 18 insertions, 47 deletions
diff --git a/rpi/spark_counter_receiver.cpp b/rpi/spark_counter_receiver.cpp
index 370de59..da1d671 100644
--- a/rpi/spark_counter_receiver.cpp
+++ b/rpi/spark_counter_receiver.cpp
@@ -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) {