Building avaerages; Configreader database user and pw
This commit is contained in:
@@ -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;
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include "dht22.h"
|
||||
|
||||
float absoluteHumidity(sensor *sensor);
|
||||
float absoluteHumidityFloat(float temperature, float humidity);
|
||||
int compareSensors(sensor *inside, sensor *outside);
|
||||
|
||||
#endif
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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();
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -1,3 +1,4 @@
|
||||
#include "dht22.h"
|
||||
|
||||
void insertData(sensor *s);
|
||||
void insertDataAverages(float temperature, float humidity, float gm3;
|
||||
|
@@ -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
|
Reference in New Issue
Block a user