aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2015-11-08 17:00:17 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2015-11-08 17:00:17 +0100
commit938cfd04fc0632cb77e4f624560d85948266b949 (patch)
treeeb5b3d34ac1300c375a50c21fb57d815d8784660
parent8b98987d1d251d9e2e14be1e0d1c3cb4fc08a72e (diff)
rpi is a C++ file actually
-rw-r--r--rpi/Makefile4
-rw-r--r--rpi/spark_counter_receiver.cpp172
2 files changed, 174 insertions, 2 deletions
diff --git a/rpi/Makefile b/rpi/Makefile
index b2c4d79..03a6cc7 100644
--- a/rpi/Makefile
+++ b/rpi/Makefile
@@ -10,12 +10,12 @@ endif
# define all programs
PROGRAMS = spark_counter_receiver
-SOURCES = ${PROGRAMS:=.c}
+SOURCES = ${PROGRAMS:=.cpp}
all: ${PROGRAMS}
${PROGRAMS}: ${SOURCES}
- gcc ${CCFLAGS} -Wall -I../ -lrf24-bcm -lcurl $@.c -o $@
+ g++ ${CCFLAGS} -Wall -I../ -lrf24-bcm -lcurl -o $@ $<
clean:
rm -rf $(PROGRAMS)
diff --git a/rpi/spark_counter_receiver.cpp b/rpi/spark_counter_receiver.cpp
new file mode 100644
index 0000000..370de59
--- /dev/null
+++ b/rpi/spark_counter_receiver.cpp
@@ -0,0 +1,172 @@
+/* This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+/* Copyright (c) 2015 King Kévin <kingkevin@cuvoodoo.info> */
+/* the spark counter receives electricity measurements over radio
+ * they are then received using an nRF2L01+ transceiver
+ * to communicate with the transceiver the RF24 library is requiered
+ * they are then stored in an InfluxDB time series database using the HTTP API
+ * to send values to the database the curl library is required
+ */
+
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include <RF24/RF24.h> // http://tmrh20.github.io/RF24 library to communicate to the nRF24L01+
+#include <curl/curl.h> // curl library to send the measurement data to the influxDB.
+
+// Setup for RPi B1 GPIO 22 CE and CE0 CSN with SPI Speed @ 8Mhz
+RF24 radio(RPI_V2_GPIO_P1_22, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ);
+
+// nRF24L01+ addresses
+const uint8_t tx_addr[] = {1,'h','o','m','e'};
+const uint8_t rx_addr[] = {0,'h','o','m','e'};
+
+CURL *curl; // curl handle to post data to influxbd using the HTTP API
+
+int main(int argc, char** argv){
+
+ // configure influxdb connection
+ curl = curl_easy_init();
+ if(curl) {
+ curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8086/write?db=electricity");
+ }
+
+ // configure nRF24L01+ radio
+ radio.begin();
+ radio.setChannel(42);
+ radio.setPALevel(RF24_PA_MAX);
+ radio.setDataRate(RF24_1MBPS);
+ radio.setAutoAck(true);
+ radio.enableDynamicPayloads();
+ radio.setRetries(2,15);
+ radio.setCRCLength(RF24_CRC_8);
+ radio.openWritingPipe(tx_addr);
+ radio.openReadingPipe(1,rx_addr);
+ //radio.printDetails();
+
+ printf("wait for packet to arrive\n");
+ radio.startListening();
+ while (1) { // forever loop
+ while (radio.available()) {
+ time_t ltime; // calendar time
+ ltime=time(NULL); // get current cal time
+ char stime[64]; // do display the time
+ strftime(stime, sizeof(stime), "%F %T %z %Z", localtime(&ltime));
+ printf("%s: payload received\n",stime);
+ 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++) {
+ printf(" %02x",payload[i]);
+ }
+ printf("\n");
+*/
+ // got through payload
+ uint8_t id = 0; // the meter id (0 is myself, used for unknown source)
+ 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
+ uint8_t length = payload[i+1]; // length of IE
+ if (i+1+length<size) { // value is within data
+ switch (type) { // read type
+ 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));
+ }
+ }
+ }
+ 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));
+ }
+ }
+ }
+ 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));
+ }
+ }
+ }
+ 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));
+ }
+ }
+ }
+ break;
+ default:
+ printf("unknown type: %d\n",payload[i]);
+ }
+ i += 2+length; // got to next IE
+ } else { // value isn't within data
+ i = size; // end the loop
+ }
+ }
+ }
+ usleep(10000); // wait 10ms before request if data is available again (the IRQ signal is not used)
+ }
+
+ if (curl) {
+ curl_easy_cleanup(curl);
+ }
+}