single influxdb query per payload
This commit is contained in:
parent
b583481c4f
commit
5ec80702c0
|
@ -71,6 +71,7 @@ int main(int argc, char** argv){
|
||||||
uint8_t payload[32]; // buffer to save the payload
|
uint8_t payload[32]; // buffer to save the payload
|
||||||
uint8_t size = radio.getDynamicPayloadSize();
|
uint8_t size = radio.getDynamicPayloadSize();
|
||||||
radio.read(&payload,size);
|
radio.read(&payload,size);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
printf("got %d bytes:",size);
|
printf("got %d bytes:",size);
|
||||||
for (uint8_t i=0; i<size; i++) {
|
for (uint8_t i=0; i<size; i++) {
|
||||||
|
@ -78,10 +79,11 @@ int main(int argc, char** argv){
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// got through payload
|
// got through payload
|
||||||
uint8_t id = 0; // the meter id (0 is myself, used for unknown source)
|
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
|
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
|
uint8_t i = 0; // index within the data
|
||||||
while (i+2<size) {
|
while (i+2<size) {
|
||||||
uint8_t type = payload[i]; // type of IE
|
uint8_t type = payload[i]; // type of IE
|
||||||
|
@ -91,67 +93,26 @@ int main(int argc, char** argv){
|
||||||
case 0: // id
|
case 0: // id
|
||||||
if (length==1) {
|
if (length==1) {
|
||||||
id = payload[i+2];
|
id = payload[i+2];
|
||||||
printf("meter: %d\n",id);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1: // voltage
|
case 1: // voltage
|
||||||
if (length==4) {
|
if (length==4) {
|
||||||
float value = 0;
|
memcpy(&voltage,&payload[i+2],length);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 2: // current
|
case 2: // current
|
||||||
if (length==4) {
|
if (length==4) {
|
||||||
float value = 0;
|
memcpy(¤t,&payload[i+2],length);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3: // power
|
case 3: // power
|
||||||
if (length==4) {
|
if (length==4) {
|
||||||
float value = 0;
|
memcpy(&power,&payload[i+2],length);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 4: // energy
|
case 4: // energy
|
||||||
if (length==4) {
|
if (length==4) {
|
||||||
float value = 0;
|
memcpy(&energy,&payload[i+2],length);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -162,8 +123,18 @@ int main(int argc, char** argv){
|
||||||
i = size; // end the loop
|
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) {
|
if (curl) {
|
||||||
|
|
Loading…
Reference in New Issue