diff --git a/Makefile b/Makefile index 9418df0..10338b9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ GCC = gcc -l sqlite3 -Wall -Wextra main: main.c - $(GCC) main.c -o main + $(GCC) -g main.c -o main diff --git a/README.md b/README.md index 9ab7e54..cf3b9c4 100644 --- a/README.md +++ b/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). -I was working on getting it to write to an SQLite3 database, but didn't get -further than initialising the databases. +It just reads printed messages from the serial port `USB0`, and writes them back +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 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 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. diff --git a/arduino.ino b/arduino.ino index cdeed16..b311ebc 100644 --- a/arduino.ino +++ b/arduino.ino @@ -7,8 +7,7 @@ #define DIO 3 TM1637Display display(CLK, DIO); -//THERMOMETER - +// THERMOMETER #include #include #include @@ -36,7 +35,7 @@ void setup() { display.setBrightness(brightness); display.showNumberDec(8888); - dht.begin(); + dht.begin(); //sensor_t sensor; //dht.temperature().getSensor(&sensor); @@ -44,7 +43,8 @@ void setup() { int count = 0; while (!Serial) { 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; } } diff --git a/main.c b/main.c index 236af15..1730bc6 100644 --- a/main.c +++ b/main.c @@ -5,10 +5,18 @@ #include #include #include +#include #include #include +#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 *db; char *zErrMsg = 0; @@ -52,6 +60,7 @@ void ccleanup(sqlite3 *db) { sqlite3_close(db); printf("Closed DB.\n"); } + } 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) { 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) { @@ -113,15 +122,18 @@ void initialise_db(sqlite3* db) { call_cleanup(1); } } - 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) { fprintf(stderr, "SQL: %s\n", zErrMsg); sqlite3_free(zErrMsg); call_cleanup(1); } - if (r.argc == 0) { + + if (re.argc == 0) { query = "CREATE TABLE humidity(" \ "timestamp INT PRIMARY KEY 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) { sqlite3 *db = get_db(); ccleanup(db); @@ -149,14 +189,24 @@ int main (int argc, char **arv) { char string[200]; int val; + int time_now = 0; + int last_time = 0; while(1) { + last_time = time_now; + time_now = time(NULL); memset(string, 0, sizeof(char) * 200); fscanf(serial, "%s%d", string, &val); if (!strcmp(string, "temp:")) { 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:")) { printf("Relative Humidity: %d\n", val); + if (last_time != time_now) { + insert_rec(db, INSERT_HUM_TEMPL, val, WTHR_STATION_ID, time_now); + } } }