From eca544a42117b0aafaad9d6bb501faba04b76375 Mon Sep 17 00:00:00 2001 From: structix Date: Sun, 27 Aug 2017 16:31:46 +0200 Subject: [PATCH] Bringinh all the pieces together. #2 #7 --- RaspberryPi/configreader.c | 30 ++++++++++++++++++++++++++++++ RaspberryPi/configreader.h | 5 +++++ RaspberryPi/main.c | 26 +++++++++++++++++++++++--- RaspberryPi/makefile | 4 ++-- RaspberryPi/restcurl.c | 8 +++++++- RaspberryPi/settings.cfg | 5 +++++ 6 files changed, 72 insertions(+), 6 deletions(-) diff --git a/RaspberryPi/configreader.c b/RaspberryPi/configreader.c index 9756b32..7266925 100644 --- a/RaspberryPi/configreader.c +++ b/RaspberryPi/configreader.c @@ -42,3 +42,33 @@ int cfgreadpinamount(void) { pins = config_lookup(&cfg, "pins"); return config_setting_length(pins); } + +int cfgreadsensornodes(sensornode *nodes, int nodecount) { + sensornode *currentNode = nodes; + + config_setting_t *sensornodes = config_lookup(&cfg, "sensornodes"); + if (sensornodes != NULL) { + int count = nodecount = config_setting_length(sensornodes); //Read amount of sensornodes and save it in nodecount + int i; + + for(i = 0; i < count; ++i) { + config_setting_t *sensornodesetting = config_setting_get_elem(sensornodes, i); + + /* Only output the record if all of the expected fields are present. */ + const char *ip; + + if (!config_setting_lookup_string(sensornodesetting, "ip", &ip)) + continue; + + currentNode->ip = ip; + //if (i < count - 1) { //stop at last item + currentNode->next = malloc(sizeof(sensornode)); + currentNode = currentNode->next; + //} + + } + return 1; + + } + +} diff --git a/RaspberryPi/configreader.h b/RaspberryPi/configreader.h index 269454d..9360ce3 100644 --- a/RaspberryPi/configreader.h +++ b/RaspberryPi/configreader.h @@ -3,3 +3,8 @@ void cfgdestroy(void); void cfgreinit(void); int cfgreadpinnumber(int element); int cfgreadpinamount(void); + +typedef struct sensornode { + char *ip; + struct sensornode *next; +} diff --git a/RaspberryPi/main.c b/RaspberryPi/main.c index ce44be4..0afaeca 100644 --- a/RaspberryPi/main.c +++ b/RaspberryPi/main.c @@ -5,6 +5,7 @@ #include #include "sendmysql.h" #include "configreader.h" +#include "restcurl.h" int main(void) { if (wiringPiSetup () == -1) @@ -18,8 +19,8 @@ int main(void) { cfginit(); - sensor data; - + sensor data; //data of the raspberrypi + data.node_id = 0; //Node ID 0 --> RaspberryPi int i; for (i = 0; i < cfgreadpinamount(); i++) { data.pin = cfgreadpinnumber(i); @@ -27,6 +28,25 @@ int main(void) { printf("Pin: %i, %.2f, %.2f\n", data.pin, data.temperature, data.humidity); insertData(&data); } - + + sensornode nodes; //Settings of ESP8266 sensor nodes + int nodecount; + cfgreadsensornodes(&nodes, nodecount); //read settings + sensor nodedata; //Should be a linked list for further data usage + sensornode *currentNode = nodes; + + + for (i = 0; i < nodecount; i++) { + //start rest requests + nodedata.ip = currentNode->ip; + getRestSensor(&nodedate); + + printf("Temperature: %f, Humidity: %f, NodeID: %i", nodedata.temperature, nodedata.humidity, nodedata.node_id); + + currentNode = currentNode->next; + } + + + return 0; } diff --git a/RaspberryPi/makefile b/RaspberryPi/makefile index 924e28e..e9bf5ce 100644 --- a/RaspberryPi/makefile +++ b/RaspberryPi/makefile @@ -5,9 +5,9 @@ VERSION = 1.0 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` +LDFLAGS = -lwiringPi -lconfig `mysql_config --libs` -lcurl -OBJ = main.o dht22.o sendmysql.o configreader.o +OBJ = main.o dht22.o sendmysql.o configreader.o cJSON.o restcurl.o calculate.o dht22: $(OBJ) $(CC) $(CFLAGS) -o humidityPi $(OBJ) $(LDFLAGS) diff --git a/RaspberryPi/restcurl.c b/RaspberryPi/restcurl.c index 620df23..42c5e03 100644 --- a/RaspberryPi/restcurl.c +++ b/RaspberryPi/restcurl.c @@ -1,5 +1,6 @@ #include "restcurl.h" #include +#include static struct url_data { size_t size; @@ -64,13 +65,18 @@ static char *handle_url(char *url) { } int getRestSensor(sensor *sensor) { - char *data = handle_url(sensor->ip); + char url[25]; + url = "http://"; + strcat(url, sensor->ip); + char *data = handle_url(url); if (data) { cJSON *jsondata = cJSON_Parse(data); cJSON *temperature = cJSON_GetObjectItemCaseSensitive(jsondata, "temperature"); cJSON *humidity = cJSON_GetObjectItemCaseSensitive(jsondata, "humidity"); + cJSON *id = cJSON_GetObjectItemCaseSensitive(jsondata, "id"); sensor->temperature = (float)temperature->valuedouble; sensor->humidity = (float)humidity->valuedouble; + sensor->node_id = id->valueint; //free cJSON_Delete(jsondata); diff --git a/RaspberryPi/settings.cfg b/RaspberryPi/settings.cfg index c760cfb..ea0fade 100644 --- a/RaspberryPi/settings.cfg +++ b/RaspberryPi/settings.cfg @@ -1 +1,6 @@ pins=[7]; + + +sensornodes = ( { ip = "192.168.0.101";}, + { ip = "192.168.0.102";} +);