diff --git a/README.md b/README.md index 309c9ad..6e0d72d 100644 --- a/README.md +++ b/README.md @@ -13,14 +13,14 @@ Create database: `create database dhtstats;` Use database: `use dhtstats` -Create new table: `CREATE TABLE stats (id MEDIUMINT NOT NULL AUTO_INCREMENT, 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, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id) );` -Show all entries in table: `select * from stats` +Show all entries in table: `select * from stats;` ## Dependencies ### Raspberry Pi -`sudo apt-get install libmysqlclient-dev libmysqld-dev mysql libconfig-dev` +`apt install mariadb-server mariadb-client libmariadbclient-dev libmariadbclient-dev-compat libconfig-dev wiringpi (-s)` ### ESP-8266 [aRest](https://github.com/marcoschwartz/aREST) diff --git a/RaspberryPi/dht22.h b/RaspberryPi/dht22.h index 0789d2b..4bda4be 100644 --- a/RaspberryPi/dht22.h +++ b/RaspberryPi/dht22.h @@ -9,6 +9,8 @@ extern "C" { typedef struct { int pin; + int node_id; + char ip[16]; //15 digits + '\0' float humidity; float temperature; } sensor; diff --git a/RaspberryPi/makefile b/RaspberryPi/makefile index c675513..924e28e 100644 --- a/RaspberryPi/makefile +++ b/RaspberryPi/makefile @@ -2,15 +2,15 @@ VERSION = 1.0 -CC = /usr/bin/gcc -CFLAGS = -Wall -g -D_GNU_SOURCE -D_REENTRANT -DVERSION=\"$(VERSION)\" `mysql_config --cflags` +CC = cc +CFLAGS = -Wall -O3 -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` OBJ = main.o dht22.o sendmysql.o configreader.o dht22: $(OBJ) - $(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS) + $(CC) $(CFLAGS) -o humidityPi $(OBJ) $(LDFLAGS) %.o: %.c $(CC) $(CFLAGS) -c $< diff --git a/RaspberryPi/restcurl.c b/RaspberryPi/restcurl.c index 8d1c8b6..4de9533 100644 --- a/RaspberryPi/restcurl.c +++ b/RaspberryPi/restcurl.c @@ -1 +1,65 @@ - +#include "restcurl.h" +#include + +static struct url_data { + size_t size; + char* data; +}; + +static size_t write_data(void *ptr, size_t size, size_t nmemb, struct url_data *data) { + size_t index = data->size; + size_t n = (size * nmemb); + char* tmp; + + data->size += (size * nmemb); + + tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */ + + if(tmp) { + data->data = tmp; + } else { + if(data->data) { + free(data->data); + } + fprintf(stderr, "Failed to allocate memory.\n"); + return 0; + } + + memcpy((data->data + index), ptr, n); + data->data[data->size] = '\0'; + + return size * nmemb; +} + +char *handle_url(char *url) { + CURL *curl; + + struct url_data data; + data.size = 0; + data.data = malloc(4096); /* reasonable size initial buffer */ + if(NULL == data.data) { + fprintf(stderr, "Failed to allocate memory.\n"); + return NULL; + } + + data.data[0] = '\0'; + + CURLcode res; + + curl = curl_easy_init(); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); + res = curl_easy_perform(curl); + if(res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + } + + curl_easy_cleanup(curl); + + } + return data.data; +} + diff --git a/RaspberryPi/restcurl.h b/RaspberryPi/restcurl.h index 0993e84..3f55277 100644 --- a/RaspberryPi/restcurl.h +++ b/RaspberryPi/restcurl.h @@ -3,6 +3,6 @@ #include "dht22.h" -//define functions here +char *handle_url(char *url); #endif diff --git a/RaspberryPi/sendmysql.c b/RaspberryPi/sendmysql.c index 04e7a30..9c5b1a9 100644 --- a/RaspberryPi/sendmysql.c +++ b/RaspberryPi/sendmysql.c @@ -42,11 +42,12 @@ void insertData(sensor *s) { char *pquerystring = NULL; if (-1 == asprintf(&pquerystring, - "INSERT INTO stats (pin, humidity, temperature) VALUES (%i, %f, %f)", s->pin, s->humidity, s->temperature)) { + "INSERT INTO stats (node_id, pin, humidity, temperature) VALUES (%i, %i, %f, %f)", s->node_id, s->pin, s->humidity, s->temperature)) { perror("asprintf() failed"); } else { if (mysql_query(conn, pquerystring)) { fprintf(stderr, "%s\n", mysql_error(conn)); + free(pquerystring); exit(1); } }