More sensor information in struct; added handle_url; new node_id column in sendmysql

This commit is contained in:
2017-08-26 15:02:15 +02:00
parent 2efcbce437
commit 740589b158
6 changed files with 76 additions and 9 deletions

View File

@@ -9,6 +9,8 @@ extern "C" {
typedef struct {
int pin;
int node_id;
char ip[16]; //15 digits + '\0'
float humidity;
float temperature;
} sensor;

View File

@@ -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 $<

View File

@@ -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;
}

View File

@@ -3,6 +3,6 @@
#include "dht22.h"
//define functions here
char *handle_url(char *url);
#endif

View File

@@ -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);
}
}