diff --git a/README.md b/README.md index 4adfdbe..c74452c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Create database: `create database dhtstats;` Use database: `use dhtstats` -Create new table: `CREATE TABLE stats (id MEDIUMINT NOT NULL AUTO_INCREMENT, node_id int not null, pin int not null, humidity FLOAT NOT NULL, temperature FLOAT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) );` +Create new table: `CREATE TABLE stats (id MEDIUMINT NOT NULL AUTO_INCREMENT, node_id int not null, pin int not null, humidity FLOAT NOT NULL, temperature FLOAT NOT NULL, isoutside int not null, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) );` Show all entries in table: `select * from stats;` diff --git a/RaspberryPi/configreader.c b/RaspberryPi/configreader.c index 365d60a..7127dca 100644 --- a/RaspberryPi/configreader.c +++ b/RaspberryPi/configreader.c @@ -57,11 +57,12 @@ int cfgreadsensornodes(sensornode *nodes, int nodecount) { /* Only output the record if all of the expected fields are present. */ const char *ip; - - if (!config_setting_lookup_string(sensornodesetting, "ip", &ip)) + int outside; + if (!(config_setting_lookup_string(sensornodesetting, "ip", &ip) && config_setting_lookup_int(sensornodesetting, "outside", &outside))) continue; strcpy(currentNode->ip, ip); + currentNode->isoutside = outside; //if (i < count - 1) { //stop at last item currentNode->next = malloc(sizeof(sensornode)); currentNode = currentNode->next; diff --git a/RaspberryPi/configreader.h b/RaspberryPi/configreader.h index a75b66c..0d317af 100644 --- a/RaspberryPi/configreader.h +++ b/RaspberryPi/configreader.h @@ -4,6 +4,7 @@ typedef struct sensornode { char ip[50]; + int isoutside; //Inside = 0; outside = 1 struct sensornode *next; } sensornode; diff --git a/RaspberryPi/dht22.h b/RaspberryPi/dht22.h index 4bda4be..1db8821 100644 --- a/RaspberryPi/dht22.h +++ b/RaspberryPi/dht22.h @@ -10,7 +10,8 @@ extern "C" { typedef struct { int pin; int node_id; - char ip[16]; //15 digits + '\0' + char ip[50]; //IP or name can be used + int isoutside; float humidity; float temperature; } sensor; diff --git a/RaspberryPi/main.c b/RaspberryPi/main.c index be5e8f9..7b295ad 100644 --- a/RaspberryPi/main.c +++ b/RaspberryPi/main.c @@ -8,6 +8,20 @@ #include "restcurl.h" #include + +void freeNodeLinkedList(sensornode *node) { + void *victim; + if (node != NULL) { + while (node) { + victim = node; + node = node->next; + free(victim); + } + } +} + + + int main(void) { if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; @@ -37,23 +51,27 @@ int main(void) { printf("Reading Sensornodes done Nodecount: %i\n", nodecount); sensor nodedata; //Should be a linked list for further data usage sensornode *currentNode = &nodes; - + nodedata.pin = 0; //This data isn't relevant for a sensornode printf("First IP: %s\n", nodes.ip); for (i = 0; i <= nodecount; i++) { //start rest requests strcpy(nodedata.ip, currentNode->ip); getRestSensor(&nodedata); + nodedata.isoutside = currentNode->isoutside; //copy the boolean value printf("Acquiring rest response\n"); - printf("Temperature: %f, Humidity: %f, NodeID: %i\n", nodedata.temperature, nodedata.humidity, nodedata.node_id); + printf("Temperature: %f, Humidity: %f, NodeID: %i, Outside: %s\n", nodedata.temperature, nodedata.humidity, nodedata.node_id, nodedata.isoutside ? "Yes" : "No"); //Insert data into database - + printf("Insert data into stats table\n"); + insertData(&nodedata); currentNode = currentNode->next; } + //free + //freeNodeLinkedList(nodes.next); cfgdestroy(); return 0; } diff --git a/RaspberryPi/restcurl.c b/RaspberryPi/restcurl.c index d60ce1e..60d8452 100644 --- a/RaspberryPi/restcurl.c +++ b/RaspberryPi/restcurl.c @@ -88,6 +88,7 @@ int getRestSensor(sensor *sensor) { //free cJSON_Delete(jsondata); + free(data); return 1; } return 0; diff --git a/RaspberryPi/sendmysql.c b/RaspberryPi/sendmysql.c index 9c5b1a9..900310d 100644 --- a/RaspberryPi/sendmysql.c +++ b/RaspberryPi/sendmysql.c @@ -42,7 +42,7 @@ void insertData(sensor *s) { char *pquerystring = NULL; if (-1 == asprintf(&pquerystring, - "INSERT INTO stats (node_id, pin, humidity, temperature) VALUES (%i, %i, %f, %f)", s->node_id, s->pin, s->humidity, s->temperature)) { + "INSERT INTO stats (node_id, pin, humidity, temperature, isoutside) VALUES (%i, %i, %f, %f, %i)", s->node_id, s->pin, s->humidity, s->temperature, s->isoutside)) { perror("asprintf() failed"); } else { if (mysql_query(conn, pquerystring)) { diff --git a/RaspberryPi/settings.cfg b/RaspberryPi/settings.cfg index ea0fade..2136cd1 100644 --- a/RaspberryPi/settings.cfg +++ b/RaspberryPi/settings.cfg @@ -1,6 +1,6 @@ pins=[7]; -sensornodes = ( { ip = "192.168.0.101";}, - { ip = "192.168.0.102";} +sensornodes = ( { ip = "192.168.0.101"; outside = 0;}, + { ip = "192.168.0.102"; outside = 1;} );