Restserver update

This commit is contained in:
2017-09-14 22:01:00 +02:00
parent 9e1807fcb0
commit d75632c61d
7 changed files with 110 additions and 226 deletions

View File

@@ -1,89 +0,0 @@
#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;
}

View File

@@ -1,10 +0,0 @@
#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

View File

@@ -0,0 +1,14 @@
#!/bin/bash
apt-get install libmicrohttpd-dev libjansson-dev libcurl4-gnutls-dev libgnutls28-dev libgcrypt20-dev
git clone https://github.com/babelouest/ulfius.git
cd ulfius/
git submodule update --init
cd lib/orcania
make && sudo make install
cd ../yder
make && sudo make install
cd ../..
make
sudo make install

View File

@@ -1,3 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define U_DISABLE_CURL
#define U_DISABLE_WEBSOCKET
#include <ulfius.h>
#define PORT 8080
#define PREFIX "/test"
int callback_welcome (const struct _u_request * request, struct _u_response * response, void * user_data) {
ulfius_set_string_body_response(response, 200, "Welcome to the HumidityPi rest service!");
return U_CALLBACK_CONTINUE;
}
char * print_map(const struct _u_map * map) {
char * line, * to_return = NULL;
const char **keys, * value;
int len, i;
if (map != NULL) {
keys = u_map_enum_keys(map);
for (i=0; keys[i] != NULL; i++) {
value = u_map_get(map, keys[i]);
len = snprintf(NULL, 0, "key is %s, value is %s", keys[i], value);
line = o_malloc((len+1)*sizeof(char));
snprintf(line, (len+1), "key is %s, value is %s", keys[i], value);
if (to_return != NULL) {
len = strlen(to_return) + strlen(line) + 1;
to_return = o_realloc(to_return, (len+1)*sizeof(char));
if (strlen(to_return) > 0) {
strcat(to_return, "\n");
}
} else {
to_return = o_malloc((strlen(line) + 1)*sizeof(char));
to_return[0] = 0;
}
strcat(to_return, line);
o_free(line);
}
return to_return;
} else {
return NULL;
}
}
int callback_post_test (const struct _u_request * request, struct _u_response * response, void * user_data) {
char * post_params = print_map(request->map_post_body);
char * response_body = msprintf("Hello World!\n%s", post_params);
ulfius_set_string_body_response(response, 200, response_body);
o_free(response_body);
o_free(post_params);
return U_CALLBACK_CONTINUE;
}
int main(void) {
struct _u_instance instance;
// Initialize instance with the port number
if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) {
fprintf(stderr, "Error ulfius_init_instance, abort\n");
return(1);
}
instance.max_post_body_size = 1024;
// Endpoint list declaration
ulfius_add_endpoint_by_val(&instance, "GET", "/welcome", NULL, 0, &callback_welcome, NULL);
ulfius_add_endpoint_by_val(&instance, "POST", "/test", NULL, 0, &callback_post_test, NULL);
// Start the framework
if (ulfius_start_framework(&instance) == U_OK) {
printf("Start framework on port %d\n", instance.port);
// Wait for the user to press <enter> on the console to quit the application
getchar();
} else {
fprintf(stderr, "Error starting framework\n");
}
printf("End framework\n");
ulfius_stop_framework(&instance);
ulfius_clean_instance(&instance);
return 0;
}

View File

@@ -5,9 +5,9 @@ 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 = `mysql_config --libs` -lm
LDFLAGS = `mysql_config --libs` -lm -lulfius -lyder -lorcania
OBJ = main.o sendmysql.o calculate.o
OBJ = main.o
dht22: $(OBJ)
$(CC) $(CFLAGS) -o humidityserver $(OBJ) $(LDFLAGS)

View File

@@ -1,121 +0,0 @@
/* 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);
}

View File

@@ -1,4 +0,0 @@
#include "dht22.h"
void insertData(sensor *s);
void insertDataAverages(float temperature, float humidity, float gm3, int isoutside);