Started rest server
This commit is contained in:
89
RestServer/calculate.c
Normal file
89
RestServer/calculate.c
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h> /* link against math lib */
|
||||||
|
#include "calculate.h"
|
||||||
|
|
||||||
|
#define R 8314.3 //J/(kmol*K) (universelle Gaskonstante)
|
||||||
|
#define mw 18.016 //kg/kmol (Molekulargewicht des Wasserdampfes)
|
||||||
|
|
||||||
|
/*
|
||||||
|
Bezeichnungen:
|
||||||
|
r = relative Luftfeuchte
|
||||||
|
T = Temperatur in °C
|
||||||
|
TK = Temperatur in Kelvin (TK = T + 273.15)
|
||||||
|
TD = Taupunkttemperatur in °C
|
||||||
|
DD = Dampfdruck in hPa
|
||||||
|
SDD = Sättigungsdampfdruck in hPa
|
||||||
|
|
||||||
|
Parameter:
|
||||||
|
a = 7.5, b = 237.3 für T >= 0
|
||||||
|
a = 7.6, b = 240.7 für T < 0 über Wasser (Taupunkt)
|
||||||
|
a = 9.5, b = 265.5 für T < 0 über Eis (Frostpunkt)
|
||||||
|
|
||||||
|
R* = 8314.3 J/(kmol*K) (universelle Gaskonstante)
|
||||||
|
mw = 18.016 kg/kmol (Molekulargewicht des Wasserdampfes)
|
||||||
|
AF = absolute Feuchte in g Wasserdampf pro m3 Luft
|
||||||
|
|
||||||
|
Formeln:
|
||||||
|
|
||||||
|
SDD(T) = 6.1078 * 10^((a*T)/(b+T))
|
||||||
|
DD(r,T) = r/100 * SDD(T)
|
||||||
|
r(T,TD) = 100 * SDD(TD) / SDD(T)
|
||||||
|
TD(r,T) = b*v/(a-v) mit v(r,T) = log10(DD(r,T)/6.1078)
|
||||||
|
AF(r,TK) = 10^5 * mw/R* * DD(r,T)/TK; AF(TD,TK) = 10^5 * mw/R* * SDD(TD)/TK
|
||||||
|
*/
|
||||||
|
|
||||||
|
static float sdd(float temperature) {
|
||||||
|
//select constant based on input temperature
|
||||||
|
float a = 7.5, b = 237.3;
|
||||||
|
if (temperature >= 0) {
|
||||||
|
a = 7.6;
|
||||||
|
b = 240.7;
|
||||||
|
}
|
||||||
|
return 6.1078 * pow(10, (a * temperature) / (b + temperature));
|
||||||
|
}
|
||||||
|
|
||||||
|
static float dd(float relativeHumidity, float temperature) {
|
||||||
|
return relativeHumidity / 100 * sdd(temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* CURRENTLY UNUSED
|
||||||
|
|
||||||
|
static float r(float temperature, float dewTemperature) {
|
||||||
|
// This is optional. Necessary with dew point measurements.
|
||||||
|
return 100 * sdd(dewTemperature) / sdd(temperature);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float v(float relativeHumidity, float temperature) {
|
||||||
|
log10(dd(relativeHumidity, temperature)/6.1078);
|
||||||
|
}
|
||||||
|
|
||||||
|
static float td(float relativeHumidity, float temperature) {
|
||||||
|
//select constant based on input temperature
|
||||||
|
float a = 7.5, b = 237.3;
|
||||||
|
if (temperature >= 0) {
|
||||||
|
a = 7.6;
|
||||||
|
b = 240.7;
|
||||||
|
}
|
||||||
|
return b * v(relativeHumidity, temperature) / (a - v(relativeHumidity, temperature));
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
static float af(float relativeHumidity, float temperature) {
|
||||||
|
float tk = temperature + 273.15;
|
||||||
|
return pow(10, 5) * mw/R * dd(relativeHumidity, temperature) / tk;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
10
RestServer/calculate.h
Normal file
10
RestServer/calculate.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef calculate_H
|
||||||
|
#define calculate_H
|
||||||
|
|
||||||
|
#include "dht22.h"
|
||||||
|
|
||||||
|
float absoluteHumidity(sensor *sensor);
|
||||||
|
float absoluteHumidityFloat(float temperature, float humidity);
|
||||||
|
int compareSensors(sensor *inside, sensor *outside);
|
||||||
|
|
||||||
|
#endif
|
1
RestServer/main.c
Normal file
1
RestServer/main.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
29
RestServer/makefile
Normal file
29
RestServer/makefile
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#Infos: http://www.ijon.de/comp/tutorials/makefile.html
|
||||||
|
|
||||||
|
|
||||||
|
VERSION = 1.0
|
||||||
|
CC = cc
|
||||||
|
CFLAGS = -Wall -g -D_GNU_SOURCE -D_REENTRANT -DVERSION=\"$(VERSION)\" `mysql_config --cflags`
|
||||||
|
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
|
||||||
|
LDFLAGS = -lwiringPi -lconfig `mysql_config --libs` -lcurl -lm
|
||||||
|
|
||||||
|
OBJ = main.o dht22.o sendmysql.o configreader.o cJSON.o restcurl.o calculate.o
|
||||||
|
|
||||||
|
dht22: $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) -o humiditypi $(OBJ) $(LDFLAGS)
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -r *.o
|
||||||
|
|
||||||
|
install:
|
||||||
|
sudo cp humiditypi /usr/bin
|
||||||
|
if [ ! -d /etc/humiditypi ]; then sudo mkdir /etc/humiditypi; fi
|
||||||
|
sudo cp settings.cfg /etc/humiditypi
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
sudo rm /usr/bin/humiditypi
|
||||||
|
rm -r /etc/humiditypi
|
121
RestServer/sendmysql.c
Normal file
121
RestServer/sendmysql.c
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
/* Simple C program that connects to MySQL Database server*/
|
||||||
|
#include <mysql/mysql.h>
|
||||||
|
//#define _GNU_SOURCE
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "calculate.h"
|
||||||
|
#include "sendmysql.h"
|
||||||
|
|
||||||
|
void insertData(sensor *s) {
|
||||||
|
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void insertDataAverages(float temperature, float humidity, float gm3, int isoutside) {
|
||||||
|
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;
|
||||||
|
if (-1 == asprintf(&pquerystring,
|
||||||
|
"INSERT INTO average_stats (humidity, temperature, gm3, isoutside) VALUES (%f, %f, %f, %i)", humidity, temperature, absoluteHumidityFloat(temperature, humidity), isoutside)) {
|
||||||
|
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);
|
||||||
|
}
|
4
RestServer/sendmysql.h
Normal file
4
RestServer/sendmysql.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include "dht22.h"
|
||||||
|
|
||||||
|
void insertData(sensor *s);
|
||||||
|
void insertDataAverages(float temperature, float humidity, float gm3, int isoutside);
|
Reference in New Issue
Block a user