user
4 years ago
commit
615a7ac42b
2 changed files with 168 additions and 0 deletions
@ -0,0 +1,4 @@
@@ -0,0 +1,4 @@
|
||||
GCC = gcc -l sqlite3 -Wall -Wextra
|
||||
|
||||
main: main.c |
||||
$(GCC) main.c -o main
|
@ -0,0 +1,164 @@
@@ -0,0 +1,164 @@
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <unistd.h> |
||||
#include <fcntl.h> |
||||
#include <termios.h> |
||||
#include <stdlib.h> |
||||
#include <signal.h> |
||||
|
||||
#include <sqlite3.h> |
||||
#include <stdbool.h> |
||||
|
||||
sqlite3* get_db(void) { |
||||
sqlite3 *db; |
||||
char *zErrMsg = 0; |
||||
int rc; |
||||
|
||||
rc = sqlite3_open("test.db", &db); |
||||
|
||||
if( rc ) { |
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); |
||||
} else { |
||||
fprintf(stderr, "Opened database successfully\n"); |
||||
} |
||||
|
||||
return db; |
||||
} |
||||
|
||||
int open_port(void) { |
||||
int fd; /* File descriptor for the port */ |
||||
|
||||
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); |
||||
if (fd == -1) { |
||||
perror("open_port: Unable to open /dev/USB0 - "); |
||||
} else |
||||
fcntl(fd, F_SETFL, 0); |
||||
return (fd); |
||||
} |
||||
|
||||
enum record_type { |
||||
R_TEMP, |
||||
R_REL_HUMIDITY |
||||
}; |
||||
|
||||
void ccleanup(sqlite3 *db) { |
||||
static bool setup = false; |
||||
static sqlite3 *saved; |
||||
|
||||
if (!setup) { |
||||
saved = db; |
||||
setup = true; |
||||
} else { |
||||
sqlite3_close(db); |
||||
printf("Closed DB.\n"); |
||||
} |
||||
} |
||||
|
||||
static void call_cleanup(int signo) { |
||||
ccleanup(NULL); |
||||
exit(0); |
||||
} |
||||
|
||||
struct query_result { |
||||
int argc; |
||||
char **argv; |
||||
char **azColName; |
||||
}; |
||||
|
||||
static int save_qry(void *retval, int argc, char **argv, char **azColName) { |
||||
struct query_result r; |
||||
r.argc = argc; |
||||
r.argv = argv; |
||||
r.azColName = azColName; |
||||
|
||||
struct query_result* ret = retval; |
||||
*ret = r; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int callback(void *retval, int argc, char **argv, char **azColName) { |
||||
int i; |
||||
for(i = 0; i<argc; i++) { |
||||
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
||||
} |
||||
printf("\n"); |
||||
return 0; |
||||
} |
||||
|
||||
void initialise_db(sqlite3* db) { |
||||
char *query = "SELECT name from sqlite_master WHERE name = 'temperature'"; |
||||
|
||||
char *zErrMsg = 0; |
||||
int rc; |
||||
struct query_result r; |
||||
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
||||
|
||||
if (rc != SQLITE_OK) { |
||||
fprintf(stderr, "SQL: %s\n", zErrMsg); |
||||
sqlite3_free(zErrMsg); |
||||
} |
||||
|
||||
if (r.argc == 0) { |
||||
char *query = "CREATE TABLE temperature (" \
|
||||
"timestamp INT PRIMARY KEY NOT NULL," \
|
||||
"value INT NOT NULL," \
|
||||
"station INT NOT NULL," \
|
||||
"FOREIGN KEY(station) REFERENCES station(id)" \
|
||||
" )"; |
||||
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
||||
if (rc != SQLITE_OK) { |
||||
fprintf(stderr, "SQL: %s\n", zErrMsg); |
||||
sqlite3_free(zErrMsg); |
||||
call_cleanup(1); |
||||
} |
||||
} |
||||
|
||||
query = "SELECT name from sqlite_master WHERE name = 'humidity'"; |
||||
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
||||
if (rc != SQLITE_OK) { |
||||
fprintf(stderr, "SQL: %s\n", zErrMsg); |
||||
sqlite3_free(zErrMsg); |
||||
call_cleanup(1); |
||||
} |
||||
if (r.argc == 0) { |
||||
query = "CREATE TABLE humidity(" \
|
||||
"timestamp INT PRIMARY KEY NOT NULL," \
|
||||
"value INT NOT NULL," \
|
||||
"station INT NOT NULL," \
|
||||
"FOREIGN KEY(station) REFERENCES station(id)" \
|
||||
" )"; |
||||
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); |
||||
if (rc != SQLITE_OK) { |
||||
fprintf(stderr, "SQL: %s\n", zErrMsg); |
||||
sqlite3_free(zErrMsg); |
||||
call_cleanup(1); |
||||
} |
||||
} |
||||
} |
||||
|
||||
int main (int argc, char **arv) { |
||||
sqlite3 *db = get_db(); |
||||
ccleanup(db); |
||||
initialise_db(db); |
||||
|
||||
signal(SIGINT, call_cleanup); |
||||
|
||||
FILE *serial = fdopen(open_port(), "r"); |
||||
|
||||
char string[200]; |
||||
int val; |
||||
|
||||
while(1) { |
||||
memset(string, 0, sizeof(char) * 200); |
||||
fscanf(serial, "%s%d", string, &val); |
||||
if (!strcmp(string, "temp:")) { |
||||
printf("Temperature: %d\n", val); |
||||
} |
||||
if (!strcmp(string, "humi:")) { |
||||
printf("Relative Humidity: %d\n", val); |
||||
} |
||||
} |
||||
|
||||
fclose(serial); |
||||
} |
Loading…
Reference in new issue