More sensor information in struct; added handle_url; new node_id column in sendmysql
This commit is contained in:
@@ -13,14 +13,14 @@ Create database: `create database dhtstats;`
|
|||||||
|
|
||||||
Use database: `use 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
|
## Dependencies
|
||||||
|
|
||||||
### Raspberry Pi
|
### 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
|
### ESP-8266
|
||||||
[aRest](https://github.com/marcoschwartz/aREST)
|
[aRest](https://github.com/marcoschwartz/aREST)
|
||||||
|
@@ -9,6 +9,8 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int pin;
|
int pin;
|
||||||
|
int node_id;
|
||||||
|
char ip[16]; //15 digits + '\0'
|
||||||
float humidity;
|
float humidity;
|
||||||
float temperature;
|
float temperature;
|
||||||
} sensor;
|
} sensor;
|
||||||
|
@@ -2,15 +2,15 @@
|
|||||||
|
|
||||||
|
|
||||||
VERSION = 1.0
|
VERSION = 1.0
|
||||||
CC = /usr/bin/gcc
|
CC = cc
|
||||||
CFLAGS = -Wall -g -D_GNU_SOURCE -D_REENTRANT -DVERSION=\"$(VERSION)\" `mysql_config --cflags`
|
CFLAGS = -Wall -O3 -D_GNU_SOURCE -D_REENTRANT -DVERSION=\"$(VERSION)\" `mysql_config --cflags`
|
||||||
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
|
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
|
||||||
LDFLAGS = -lwiringPi -lconfig `mysql_config --libs`
|
LDFLAGS = -lwiringPi -lconfig `mysql_config --libs`
|
||||||
|
|
||||||
OBJ = main.o dht22.o sendmysql.o configreader.o
|
OBJ = main.o dht22.o sendmysql.o configreader.o
|
||||||
|
|
||||||
dht22: $(OBJ)
|
dht22: $(OBJ)
|
||||||
$(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS)
|
$(CC) $(CFLAGS) -o humidityPi $(OBJ) $(LDFLAGS)
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(CC) $(CFLAGS) -c $<
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
@@ -1 +1,65 @@
|
|||||||
|
#include "restcurl.h"
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,6 @@
|
|||||||
|
|
||||||
#include "dht22.h"
|
#include "dht22.h"
|
||||||
|
|
||||||
//define functions here
|
char *handle_url(char *url);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -42,11 +42,12 @@ void insertData(sensor *s) {
|
|||||||
char *pquerystring = NULL;
|
char *pquerystring = NULL;
|
||||||
|
|
||||||
if (-1 == asprintf(&pquerystring,
|
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");
|
perror("asprintf() failed");
|
||||||
} else {
|
} else {
|
||||||
if (mysql_query(conn, pquerystring)) {
|
if (mysql_query(conn, pquerystring)) {
|
||||||
fprintf(stderr, "%s\n", mysql_error(conn));
|
fprintf(stderr, "%s\n", mysql_error(conn));
|
||||||
|
free(pquerystring);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user