@@ -42,3 +42,33 @@ int cfgreadpinamount(void) {
|
|||||||
pins = config_lookup(&cfg, "pins");
|
pins = config_lookup(&cfg, "pins");
|
||||||
return config_setting_length(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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -3,3 +3,8 @@ void cfgdestroy(void);
|
|||||||
void cfgreinit(void);
|
void cfgreinit(void);
|
||||||
int cfgreadpinnumber(int element);
|
int cfgreadpinnumber(int element);
|
||||||
int cfgreadpinamount(void);
|
int cfgreadpinamount(void);
|
||||||
|
|
||||||
|
typedef struct sensornode {
|
||||||
|
char *ip;
|
||||||
|
struct sensornode *next;
|
||||||
|
}
|
||||||
|
@@ -5,6 +5,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "sendmysql.h"
|
#include "sendmysql.h"
|
||||||
#include "configreader.h"
|
#include "configreader.h"
|
||||||
|
#include "restcurl.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
if (wiringPiSetup () == -1)
|
if (wiringPiSetup () == -1)
|
||||||
@@ -18,8 +19,8 @@ int main(void) {
|
|||||||
|
|
||||||
cfginit();
|
cfginit();
|
||||||
|
|
||||||
sensor data;
|
sensor data; //data of the raspberrypi
|
||||||
|
data.node_id = 0; //Node ID 0 --> RaspberryPi
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < cfgreadpinamount(); i++) {
|
for (i = 0; i < cfgreadpinamount(); i++) {
|
||||||
data.pin = cfgreadpinnumber(i);
|
data.pin = cfgreadpinnumber(i);
|
||||||
@@ -27,6 +28,25 @@ int main(void) {
|
|||||||
printf("Pin: %i, %.2f, %.2f\n", data.pin, data.temperature, data.humidity);
|
printf("Pin: %i, %.2f, %.2f\n", data.pin, data.temperature, data.humidity);
|
||||||
insertData(&data);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -5,9 +5,9 @@ VERSION = 1.0
|
|||||||
CC = cc
|
CC = cc
|
||||||
CFLAGS = -Wall -O3 -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` -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)
|
dht22: $(OBJ)
|
||||||
$(CC) $(CFLAGS) -o humidityPi $(OBJ) $(LDFLAGS)
|
$(CC) $(CFLAGS) -o humidityPi $(OBJ) $(LDFLAGS)
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#include "restcurl.h"
|
#include "restcurl.h"
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static struct url_data {
|
static struct url_data {
|
||||||
size_t size;
|
size_t size;
|
||||||
@@ -64,13 +65,18 @@ static char *handle_url(char *url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int getRestSensor(sensor *sensor) {
|
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) {
|
if (data) {
|
||||||
cJSON *jsondata = cJSON_Parse(data);
|
cJSON *jsondata = cJSON_Parse(data);
|
||||||
cJSON *temperature = cJSON_GetObjectItemCaseSensitive(jsondata, "temperature");
|
cJSON *temperature = cJSON_GetObjectItemCaseSensitive(jsondata, "temperature");
|
||||||
cJSON *humidity = cJSON_GetObjectItemCaseSensitive(jsondata, "humidity");
|
cJSON *humidity = cJSON_GetObjectItemCaseSensitive(jsondata, "humidity");
|
||||||
|
cJSON *id = cJSON_GetObjectItemCaseSensitive(jsondata, "id");
|
||||||
sensor->temperature = (float)temperature->valuedouble;
|
sensor->temperature = (float)temperature->valuedouble;
|
||||||
sensor->humidity = (float)humidity->valuedouble;
|
sensor->humidity = (float)humidity->valuedouble;
|
||||||
|
sensor->node_id = id->valueint;
|
||||||
|
|
||||||
//free
|
//free
|
||||||
cJSON_Delete(jsondata);
|
cJSON_Delete(jsondata);
|
||||||
|
@@ -1 +1,6 @@
|
|||||||
pins=[7];
|
pins=[7];
|
||||||
|
|
||||||
|
|
||||||
|
sensornodes = ( { ip = "192.168.0.101";},
|
||||||
|
{ ip = "192.168.0.102";}
|
||||||
|
);
|
||||||
|
Reference in New Issue
Block a user