NEW: #1 Mysql support, makefile and readme update, sensor struct update

This commit is contained in:
2017-01-28 13:56:47 +01:00
parent 2167638d2b
commit c8182dafec
7 changed files with 104 additions and 10 deletions

View File

@@ -1 +1,20 @@
# 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

View File

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

10
dht22.h
View File

@@ -1,9 +1,15 @@
#ifndef dht22_H
#define dht22_H
#include <stdint.h>
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

9
main.c
View File

@@ -3,6 +3,8 @@
#include "dht22.h"
#include <stdlib.h>
#include <unistd.h>
#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;
}

View File

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

61
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
sendmysql.h Normal file
View File

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