From c8182dafec49d8475298c72249d08889ed413c29 Mon Sep 17 00:00:00 2001 From: structix Date: Sat, 28 Jan 2017 13:56:47 +0100 Subject: [PATCH] NEW: #1 Mysql support, makefile and readme update, sensor struct update --- README.md | 21 +++++++++++++++++- dht22.c | 4 ++-- dht22.h | 10 +++++++-- main.c | 9 ++++++-- makefile | 6 +++--- sendmysql.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ sendmysql.h | 3 +++ 7 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 sendmysql.c create mode 100644 sendmysql.h diff --git a/README.md b/README.md index bfff2f9..dcb0f24 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -# dht22 Temperature Stats \ No newline at end of file +# dht22 Temperature Stats + + +## mySQL + +Login: mysql --user=root --password=password + +Creating new user: create user 'dhtuser'@'localhost' IDENTIFIED BY 'raspberry'; + +Permissions: grant all privileges on *.* to 'dhtuser'@'localhost'; + +Create database: create database 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) ); + +## Dependencies + +sudo apt-get install libmysqlclient-dev libmysqld-dev mysql diff --git a/dht22.c b/dht22.c index b296007..a0c1752 100644 --- a/dht22.c +++ b/dht22.c @@ -31,12 +31,12 @@ static uint8_t sizecvt(const int read) return (uint8_t)read; } -static int read_dht22_dat(int pin, sensor *s) +static int read_dht22_dat(sensor *s) { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; - DHTPIN = pin; + DHTPIN = s->pin; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; diff --git a/dht22.h b/dht22.h index dee7d86..b12e9fb 100644 --- a/dht22.h +++ b/dht22.h @@ -1,9 +1,15 @@ +#ifndef dht22_H +#define dht22_H - +#include typedef struct { + int pin; float humidity; float temperature; } sensor; -void getData(int pin, sensor *s); +void getData(sensor *s); +//static int read_dht22_dat(int pin, sensor *s); +//static uint8_t sizecvt(const int read); +#endif diff --git a/main.c b/main.c index 55085ca..87184cc 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,8 @@ #include "dht22.h" #include #include +#include "sendmysql.h" + int main(void) { if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; @@ -14,8 +16,11 @@ int main(void) { } sensor data; - - getData(7, &data); + data.pin = 7; + getData(&data); printf("%.2f, %.2f\n", data.temperature, data.humidity); + + insertData(&data); + return 0; } diff --git a/makefile b/makefile index 352e807..6882710 100644 --- a/makefile +++ b/makefile @@ -3,11 +3,11 @@ VERSION = 1.0 CC = /usr/bin/gcc -CFLAGS = -Wall -g -D_REENTRANT -DVERSION=\"$(VERSION)\" +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 +LDFLAGS = -lwiringPi `mysql_config --libs` -OBJ = main.o dht22.o +OBJ = main.o dht22.o sendmysql.o dht22: $(OBJ) $(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS) diff --git a/sendmysql.c b/sendmysql.c new file mode 100644 index 0000000..04e7a30 --- /dev/null +++ b/sendmysql.c @@ -0,0 +1,61 @@ + /* Simple C program that connects to MySQL Database server*/ +#include +//#define _GNU_SOURCE +#include +#include +#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); +} + diff --git a/sendmysql.h b/sendmysql.h new file mode 100644 index 0000000..0c8087a --- /dev/null +++ b/sendmysql.h @@ -0,0 +1,3 @@ +#include "dht22.h" + +void insertData(sensor *s);