NEW: #1 Mysql support, makefile and readme update, sensor struct update
This commit is contained in:
21
README.md
21
README.md
@@ -1 +1,20 @@
|
|||||||
# dht22 Temperature Stats
|
# 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
|
||||||
|
4
dht22.c
4
dht22.c
@@ -31,12 +31,12 @@ static uint8_t sizecvt(const int read)
|
|||||||
return (uint8_t)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 laststate = HIGH;
|
||||||
uint8_t counter = 0;
|
uint8_t counter = 0;
|
||||||
uint8_t j = 0, i;
|
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;
|
dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0;
|
||||||
|
|
||||||
|
10
dht22.h
10
dht22.h
@@ -1,9 +1,15 @@
|
|||||||
|
#ifndef dht22_H
|
||||||
|
#define dht22_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
int pin;
|
||||||
float humidity;
|
float humidity;
|
||||||
float temperature;
|
float temperature;
|
||||||
} sensor;
|
} 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
9
main.c
@@ -3,6 +3,8 @@
|
|||||||
#include "dht22.h"
|
#include "dht22.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "sendmysql.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
if (wiringPiSetup () == -1)
|
if (wiringPiSetup () == -1)
|
||||||
exit(EXIT_FAILURE) ;
|
exit(EXIT_FAILURE) ;
|
||||||
@@ -14,8 +16,11 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sensor data;
|
sensor data;
|
||||||
|
data.pin = 7;
|
||||||
getData(7, &data);
|
getData(&data);
|
||||||
printf("%.2f, %.2f\n", data.temperature, data.humidity);
|
printf("%.2f, %.2f\n", data.temperature, data.humidity);
|
||||||
|
|
||||||
|
insertData(&data);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
6
makefile
6
makefile
@@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
VERSION = 1.0
|
VERSION = 1.0
|
||||||
CC = /usr/bin/gcc
|
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 = -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)
|
dht22: $(OBJ)
|
||||||
$(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS)
|
$(CC) $(CFLAGS) -o dht22 $(OBJ) $(LDFLAGS)
|
||||||
|
61
sendmysql.c
Normal file
61
sendmysql.c
Normal 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
3
sendmysql.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include "dht22.h"
|
||||||
|
|
||||||
|
void insertData(sensor *s);
|
Reference in New Issue
Block a user