From f4d1f248f74c93f82c24932b48e831b725c14fd9 Mon Sep 17 00:00:00 2001 From: structix Date: Thu, 7 Sep 2017 15:09:45 +0000 Subject: [PATCH] Building avaerages; Configreader database user and pw --- RaspberryPi/calculate.c | 4 +++ RaspberryPi/calculate.h | 1 + RaspberryPi/configreader.c | 16 +++++++++++ RaspberryPi/configreader.h | 2 ++ RaspberryPi/main.c | 31 ++++++++++++++++++++ RaspberryPi/sendmysql.c | 59 ++++++++++++++++++++++++++++++++++++++ RaspberryPi/sendmysql.h | 1 + RaspberryPi/settings.cfg | 4 +++ 8 files changed, 118 insertions(+) diff --git a/RaspberryPi/calculate.c b/RaspberryPi/calculate.c index caec724..1033703 100644 --- a/RaspberryPi/calculate.c +++ b/RaspberryPi/calculate.c @@ -77,6 +77,10 @@ float absoluteHumidity(sensor *sensor) { return af(sensor->humidity, sensor->temperature); } +float absoluteHumidityFloat(float temperature, float humidity) { + return af(humidity, temperature); +} + int compareSensors(sensor *inside, sensor *outside) { //returns 1 if window should be opened if (absoluteHumidity(inside) <= absoluteHumidity(outside)) return 1; diff --git a/RaspberryPi/calculate.h b/RaspberryPi/calculate.h index 14dc441..6e98a3a 100644 --- a/RaspberryPi/calculate.h +++ b/RaspberryPi/calculate.h @@ -4,6 +4,7 @@ #include "dht22.h" float absoluteHumidity(sensor *sensor); +float absoluteHumidityFloat(float temperature, float humidity); int compareSensors(sensor *inside, sensor *outside); #endif diff --git a/RaspberryPi/configreader.c b/RaspberryPi/configreader.c index 8560a9c..1a19fb6 100644 --- a/RaspberryPi/configreader.c +++ b/RaspberryPi/configreader.c @@ -91,3 +91,19 @@ int cfgreadcompareidvalue(int element) { return -1; } } + +char *cfgreadDatabaseUser(void) { + const char *databseuser; + if (!config_setting_lookup_string(cfg, "databaseuser", databaseuser)) { + continue; + } + return databaseuser; +} + +int cfgreadNodeRefreshRate(void) { + int refreshRate = 60; //Default value + if (!config_setting_lookup_int(cfg, "refreshRate", &refreshRate)) { + continue; + } + return refreshRate; +} diff --git a/RaspberryPi/configreader.h b/RaspberryPi/configreader.h index 190bb65..dd14c39 100644 --- a/RaspberryPi/configreader.h +++ b/RaspberryPi/configreader.h @@ -18,5 +18,7 @@ int cfgreadpinamount(void); int cfgreadsensornodes(sensornode *nodes, int nodecount); int cfgreadcompareidamount(void); //currently unused but will be necessary if there'll be any node filters int cfgreadcompareidvalue(int element); //same +char *cfgreadDatabaseUser(void); +int cfgreadNodeRefreshRate(void); #endif diff --git a/RaspberryPi/main.c b/RaspberryPi/main.c index 7b28a42..83c125e 100644 --- a/RaspberryPi/main.c +++ b/RaspberryPi/main.c @@ -88,6 +88,37 @@ int main(void) { } } + + //build averages from all indoor/all outdoor sensors + float avtemp_in = 0.0, avhum_in = 0.0; + float avtemp_out = 0.0, avhum_out = 0.0; + int inCount = 0, outCount = 0; + for (j = 0; j < nodecount; j++) { + if (nodedata[j].isoutside == 0) { + //This node is a indoor node + avtemp_in += nodedate[j].temperature; + avhum_in += nodedata[j].humidity; + inCount++; + } else { + //This node is a outdoor node + avtemp_out += nodedate[j].temperature; + avhum_out += nodedata[j].humidity; + outCount++; + } + + } + + //Do the math + float avgInsideTemperature = avtemp_in / inCount; + float avgInsideHumidity = avhum_in / inCount; + float avgOutsideTemperature = avtemp_in / outCount; + float avgOutsideHumidity = avhum_in / outCount; + + //Insert the results into the database + insertDataAverages(avgInsideTemperature, avgInsideHumidity, absoluteHumidityFloat(avgInsideTemperature, avgInsideHumidity)); + insertDataAverages(avgOutsideTemperature, avgOutsideHumidity, absoluteHumidityFloat(avgOutsideTemperature, avgOutsideHumidity)); + + //free //freeNodeLinkedList(nodes.next); cfgdestroy(); diff --git a/RaspberryPi/sendmysql.c b/RaspberryPi/sendmysql.c index f979d78..403f397 100644 --- a/RaspberryPi/sendmysql.c +++ b/RaspberryPi/sendmysql.c @@ -61,3 +61,62 @@ void insertData(sensor *s) { mysql_close(conn); } + + + + + +void insertDataAverages(float temperature, float humidity, float gm3) { + MYSQL *conn; + //MYSQL_RES *res; + //MYSQL_ROW row; + + char *server = "localhost"; + char *user = "dhtuser"; + char *password = "raspberry"; /* set me first */ + char *database = "dhtstats"; + + conn = mysql_init(NULL); + + /* Connect to database */ + if (!mysql_real_connect(conn, server, + user, password, database, 0, NULL, 0)) { + fprintf(stderr, "%s\n", mysql_error(conn)); + exit(1); + } + + /* + // send SQL query + if (mysql_query(conn, "show tables")) { + fprintf(stderr, "%s\n", mysql_error(conn)); + exit(1); + } + + res = mysql_use_result(conn); + + // output table name + printf("MySQL Tables in mysql database:\n"); + while ((row = mysql_fetch_row(res)) != NULL) + printf("%s \n", row[0]); + */ + + char *pquerystring = NULL; + //TODO adjust query string + if (-1 == asprintf(&pquerystring, + "INSERT INTO stats (node_id, pin, humidity, temperature, isoutside, gm3) VALUES (%i, %i, %f, %f, %i, %f)", s->node_id, s->pin, s->humidity, s->temperature, s->isoutside, absoluteHumidity(s))) { + perror("asprintf() failed"); + } else { + if (mysql_query(conn, pquerystring)) { + fprintf(stderr, "%s\n", mysql_error(conn)); + free(pquerystring); + exit(1); + } + } + + free(pquerystring); + pquerystring = NULL; + + /* close connection */ + //mysql_free_result(res); + mysql_close(conn); +} diff --git a/RaspberryPi/sendmysql.h b/RaspberryPi/sendmysql.h index 0c8087a..d00e85e 100644 --- a/RaspberryPi/sendmysql.h +++ b/RaspberryPi/sendmysql.h @@ -1,3 +1,4 @@ #include "dht22.h" void insertData(sensor *s); +void insertDataAverages(float temperature, float humidity, float gm3; diff --git a/RaspberryPi/settings.cfg b/RaspberryPi/settings.cfg index 73ecb42..ac41dde 100644 --- a/RaspberryPi/settings.cfg +++ b/RaspberryPi/settings.cfg @@ -6,3 +6,7 @@ sensornodes = ( { ip = "192.168.0.101"; outside = 0;}, ); #compare_ids = [0, 1]; //Compare 2 sensors +databaseUser = "dhtuser"; + + +//TODO: Set the refresh rate for an esp8266 manually through the settings file. This will be set by http post to the rest service \ No newline at end of file