Browse Source

logs to database now

master
user 4 years ago
parent
commit
e67f052222
  1. 2
      Makefile
  2. 10
      README.md
  3. 8
      arduino.ino
  4. 58
      main.c

2
Makefile

@ -1,4 +1,4 @@
GCC = gcc -l sqlite3 -Wall -Wextra GCC = gcc -l sqlite3 -Wall -Wextra
main: main.c main: main.c
$(GCC) main.c -o main $(GCC) -g main.c -o main

10
README.md

@ -5,8 +5,9 @@ temperature and relative humidity values using a DHT11 sensor.
`main.c` is the PC program that does the logging (printing to stdout). `main.c` is the PC program that does the logging (printing to stdout).
I was working on getting it to write to an SQLite3 database, but didn't get It just reads printed messages from the serial port `USB0`, and writes them back
further than initialising the databases. into a SQLite3 database. It automatically creates the database and tables
if they don't exist (making it do that was 90% of the work).
`arduino.ino` is the Arduino IDE project that reads from the hardware using the `arduino.ino` is the Arduino IDE project that reads from the hardware using the
Adafruit sensors library. Adafruit sensors library.
@ -15,3 +16,8 @@ The arduino code is very arduino which isn't too nice since sine its such a tiny
project pulling a lot of dependencies but I guess that's fine. Re-writing the project pulling a lot of dependencies but I guess that's fine. Re-writing the
DHT11 library would probably be interesting to do. DHT11 library would probably be interesting to do.
### Compiling
For `main.c` run `make`
For the `arduino.ino` track down the Adafruit DHT11/DHT22 library, import it and build the project in the Arduino IDE.

8
arduino.ino

@ -7,8 +7,7 @@
#define DIO 3 #define DIO 3
TM1637Display display(CLK, DIO); TM1637Display display(CLK, DIO);
//THERMOMETER // THERMOMETER
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <DHT.h> #include <DHT.h>
#include <DHT_U.h> #include <DHT_U.h>
@ -36,7 +35,7 @@ void setup() {
display.setBrightness(brightness); display.setBrightness(brightness);
display.showNumberDec(8888); display.showNumberDec(8888);
dht.begin(); dht.begin();
//sensor_t sensor; //sensor_t sensor;
//dht.temperature().getSensor(&sensor); //dht.temperature().getSensor(&sensor);
@ -44,7 +43,8 @@ void setup() {
int count = 0; int count = 0;
while (!Serial) { while (!Serial) {
if (count > 8000 * 5000) { if (count > 8000 * 5000) {
serial = false;// wait for serial port to connect. Needed for native USB port only serial = false;
// wait for serial port to connect. Needed for native USB port only
return; return;
} }
} }

58
main.c

@ -5,10 +5,18 @@
#include <termios.h> #include <termios.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <time.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <stdbool.h> #include <stdbool.h>
#define WTHR_STATION_ID 0
#define TIMESTAMP_FIELD 1
#define DATA_VAL_FIELD 2
#define STATION_ID_FIELD 3
#define INSERT_HUM_TEMPL "INSERT INTO humidity(timestamp, value, station) VALUES(?, ?, ?)"
#define INSERT_TEMP_TEMPL "INSERT INTO temperature(timestamp, value, station) VALUES(?, ?, ?)"
sqlite3* get_db(void) { sqlite3* get_db(void) {
sqlite3 *db; sqlite3 *db;
char *zErrMsg = 0; char *zErrMsg = 0;
@ -52,6 +60,7 @@ void ccleanup(sqlite3 *db) {
sqlite3_close(db); sqlite3_close(db);
printf("Closed DB.\n"); printf("Closed DB.\n");
} }
} }
static void call_cleanup(int signo) { static void call_cleanup(int signo) {
@ -88,10 +97,10 @@ static int callback(void *retval, int argc, char **argv, char **azColName) {
void initialise_db(sqlite3* db) { void initialise_db(sqlite3* db) {
char *query = "SELECT name from sqlite_master WHERE name = 'temperature'"; char *query = "SELECT name from sqlite_master WHERE name = 'temperature'";
char *zErrMsg = 0; char *zErrMsg = 0;
int rc; int rc;
struct query_result r; struct query_result r;
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg); rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
@ -113,15 +122,18 @@ void initialise_db(sqlite3* db) {
call_cleanup(1); call_cleanup(1);
} }
} }
query = "SELECT name from sqlite_master WHERE name = 'humidity'"; query = "SELECT name from sqlite_master WHERE name = 'humidity'";
rc = sqlite3_exec(db, query, save_qry, &r, &zErrMsg);
struct query_result re;
rc = sqlite3_exec(db, query, save_qry, &re, &zErrMsg);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
fprintf(stderr, "SQL: %s\n", zErrMsg); fprintf(stderr, "SQL: %s\n", zErrMsg);
sqlite3_free(zErrMsg); sqlite3_free(zErrMsg);
call_cleanup(1); call_cleanup(1);
} }
if (r.argc == 0) {
if (re.argc == 0) {
query = "CREATE TABLE humidity(" \ query = "CREATE TABLE humidity(" \
"timestamp INT PRIMARY KEY NOT NULL," \ "timestamp INT PRIMARY KEY NOT NULL," \
"value INT NOT NULL," \ "value INT NOT NULL," \
@ -137,6 +149,34 @@ void initialise_db(sqlite3* db) {
} }
} }
int check_error(int rc) {
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL Error: %d\n", rc);
call_cleanup(1);
}
return 0;
}
int insert_rec(sqlite3 *db, char* sqlt, int value, int station, int timestamp) {
sqlite3_stmt * statement;
const char *tail = 0;
int rc = sqlite3_prepare_v2(db, sqlt, -1, &statement, &tail);
check_error(rc);
rc = sqlite3_bind_int(statement, DATA_VAL_FIELD, value);
check_error(rc);
rc = sqlite3_bind_int(statement, STATION_ID_FIELD, station);
check_error(rc);
rc = sqlite3_bind_int(statement, TIMESTAMP_FIELD, timestamp);
check_error(rc);
rc = sqlite3_step(statement);
if (rc != SQLITE_DONE) {
check_error(rc);
}
rc = sqlite3_finalize(statement);
check_error(rc);
return 0;
}
int main (int argc, char **arv) { int main (int argc, char **arv) {
sqlite3 *db = get_db(); sqlite3 *db = get_db();
ccleanup(db); ccleanup(db);
@ -149,14 +189,24 @@ int main (int argc, char **arv) {
char string[200]; char string[200];
int val; int val;
int time_now = 0;
int last_time = 0;
while(1) { while(1) {
last_time = time_now;
time_now = time(NULL);
memset(string, 0, sizeof(char) * 200); memset(string, 0, sizeof(char) * 200);
fscanf(serial, "%s%d", string, &val); fscanf(serial, "%s%d", string, &val);
if (!strcmp(string, "temp:")) { if (!strcmp(string, "temp:")) {
printf("Temperature: %d\n", val); printf("Temperature: %d\n", val);
} }
if (last_time != time_now) {
insert_rec(db, INSERT_TEMP_TEMPL, val, WTHR_STATION_ID, time_now);
}
if (!strcmp(string, "humi:")) { if (!strcmp(string, "humi:")) {
printf("Relative Humidity: %d\n", val); printf("Relative Humidity: %d\n", val);
if (last_time != time_now) {
insert_rec(db, INSERT_HUM_TEMPL, val, WTHR_STATION_ID, time_now);
}
} }
} }

Loading…
Cancel
Save