New Structure

This commit is contained in:
2017-08-23 21:02:28 +02:00
parent 6243c087f5
commit 3482630ed8
10 changed files with 9 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
#include "configreader.h"
#include <libconfig.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
config_t cfg;
const config_setting_t *pins;
void cfginit(void) {
config_init(&cfg);
if (!config_read_file(&cfg, "./settings.cfg")) {
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
//return(EXIT_FAILURE);
}
}
void cfgdestroy(void) {
//free ram
config_destroy(&cfg);
}
void cfgreinit(void) {
cfgdestroy();
cfginit();
}
int cfgreadpinnumber(int element) {
pins = config_lookup(&cfg, "pins");
int length = config_setting_length(pins);
if (element < length) {
return config_setting_get_int_elem(pins, element);
} else {
printf("No pins configuration item found.\n");
return 7;
}
}
int cfgreadpinamount(void) {
pins = config_lookup(&cfg, "pins");
return config_setting_length(pins);
}

View File

@@ -0,0 +1,5 @@
void cfginit(void);
void cfgdestroy(void);
void cfgreinit(void);
int cfgreadpinnumber(int element);
int cfgreadpinamount(void);

105
RaspberryPi/dht22.c Normal file
View File

@@ -0,0 +1,105 @@
/*
* dht22.c:
* Simple test program to test the wiringPi functions
* Based on the existing dht11.c
* Amended by technion@lolware.net
*/
#include <wiringPi.h>
#include "dht22.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#define MAXTIMINGS 85
static int DHTPIN = 7;
static int dht22_dat[5] = {0,0,0,0,0};
static uint8_t sizecvt(const int read)
{
/* digitalRead() and friends from wiringpi are defined as returning a value
*< 256. However, they are returned as int() types. This is a safety function */
if (read > 255 || read < 0)
{
printf("Invalid data from wiringPi library\n");
exit(EXIT_FAILURE);
}
return (uint8_t)read;
}
static int read_dht22_dat(sensor *s)
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
DHTPIN = s->pin;
dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0;
// pull pin down for 18 milliseconds
pinMode(DHTPIN, OUTPUT);
digitalWrite(DHTPIN, HIGH);
delay(10);
digitalWrite(DHTPIN, LOW);
delay(18);
// then pull it up for 40 microseconds
digitalWrite(DHTPIN, HIGH);
delayMicroseconds(40);
// prepare to read the pin
pinMode(DHTPIN, INPUT);
// detect change and read data
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (sizecvt(digitalRead(DHTPIN)) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = sizecvt(digitalRead(DHTPIN));
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
dht22_dat[j/8] <<= 1;
if (counter > 16)
dht22_dat[j/8] |= 1;
j++;
}
}
// check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
// print it out if data is good
if ((j >= 40) &&
(dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) {
float t, h;
h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1];
h /= 10;
t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3];
t /= 10.0;
if ((dht22_dat[2] & 0x80) != 0) t *= -1;
//printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t );
s->humidity = h;
s->temperature = t;
return 1;
} else {
//printf("Data not good, skip\n");
return 0;
}
}
void getData(sensor *s) {
int tries = 100;
while (read_dht22_dat(s) == 0 && tries--) {
delay(1000);
}
}

25
RaspberryPi/dht22.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef dht22_H
#define dht22_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int pin;
float humidity;
float temperature;
} sensor;
void getData(sensor *s);
//static int read_dht22_dat(int pin, sensor *s);
//static uint8_t sizecvt(const int read);
#ifdef __cplusplus
}
#endif
#endif

32
RaspberryPi/main.c Normal file
View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#include <wiringPi.h>
#include "dht22.h"
#include <stdlib.h>
#include <unistd.h>
#include "sendmysql.h"
#include "configreader.h"
int main(void) {
if (wiringPiSetup () == -1)
exit(EXIT_FAILURE) ;
if (setuid(getuid()) < 0)
{
perror("Dropping privileges failed\n");
exit(EXIT_FAILURE);
}
cfginit();
sensor data;
int i;
for (i = 0; i < cfgreadpinamount(); i++) {
data.pin = cfgreadpinnumber(i);
getData(&data);
printf("Pin: %i, %.2f, %.2f\n", data.pin, data.temperature, data.humidity);
insertData(&data);
}
return 0;
}

25
RaspberryPi/makefile Normal file
View File

@@ -0,0 +1,25 @@
#Infos: http://www.ijon.de/comp/tutorials/makefile.html
VERSION = 1.0
CC = /usr/bin/gcc
CFLAGS = -Wall -g -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)
%.o: %.c
$(CC) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -r *.o
install:
sudo cp -r lcdclock /usr/local/bin
sudo cp -r playsong.sh /usr/local/bin
sudo cp -r settings.cfg /usr/local/bin

61
RaspberryPi/sendmysql.c Normal file
View File

@@ -0,0 +1,61 @@
/* Simple C program that connects to MySQL Database server*/
#include <mysql/mysql.h>
//#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include "sendmysql.h"
void insertData(sensor *s) {
MYSQL *conn;
//MYSQL_RES *res;
//MYSQL_ROW row;
char *server = "localhost";
char *user = "dhtuser";
char *password = "raspberry"; /* set me first */
char *database = "dhtstats";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/*
// send SQL query
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
// output table name
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
*/
char *pquerystring = NULL;
if (-1 == asprintf(&pquerystring,
"INSERT INTO stats (pin, humidity, temperature) VALUES (%i, %f, %f)", s->pin, s->humidity, s->temperature)) {
perror("asprintf() failed");
} else {
if (mysql_query(conn, pquerystring)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
}
free(pquerystring);
pquerystring = NULL;
/* close connection */
//mysql_free_result(res);
mysql_close(conn);
}

3
RaspberryPi/sendmysql.h Normal file
View File

@@ -0,0 +1,3 @@
#include "dht22.h"
void insertData(sensor *s);

1
RaspberryPi/settings.cfg Normal file
View File

@@ -0,0 +1 @@
pins=[7];