Compare commits

..

4 Commits

Author SHA1 Message Date
Ryan Alder
927f3dc6c6 Protobuf + DB Queries 2020-02-17 19:27:05 -08:00
Ryan Alder
97dbf3facb Switch to 32-bit floats 2020-02-17 15:52:28 -08:00
Ryan Alder
2cda6935ab Add database API example 2020-02-17 15:51:57 -08:00
Ryan Alder
c3b29387fd Add database API 2020-02-17 15:51:44 -08:00
6 changed files with 106 additions and 10 deletions

View File

@@ -2,13 +2,13 @@
# Single lora testing app
CC=g++
CFLAGS=-c -Wall `pkg-config --cflags 'libprotobuf-c >= 1.0.0'`
LIBS=-lwiringPi -lprotobuf-c
CFLAGS=-c -Wall `pkg-config --cflags sqlite3 'libprotobuf-c >= 1.0.0'`
LIBS=-lwiringPi -lprotobuf-c -lsqlite3
all: dragino_lora_app
dragino_lora_app: main.o protobuf.o message.pb-c.o
$(CC) main.o protobuf.o message.pb-c.o $(LIBS) -o dragino_lora_app
dragino_lora_app: main.o protobuf.o message.pb-c.o database.o
$(CC) main.o protobuf.o database.o message.pb-c.o $(LIBS) -o dragino_lora_app
main.o: main.c protobuf.h
$(CC) $(CFLAGS) main.c
@@ -18,6 +18,9 @@ protobuf.o: message.pb-c.h protobuf.c protobuf.h
protobuf.h: message.pb-c.h
database.o: database.c database.h
$(CC) $(CFLAGS) database.c -o database.o
message.pb-c.o: message.pb-c.h message.pb-c.c
$(CC) $(CFLAGS) message.pb-c.c -o message.pb-c.o

53
database.c Normal file
View File

@@ -0,0 +1,53 @@
#include "database.h"
#include <assert.h>
#include <stdio.h>
#include <stddef.h>
void database_state_init(struct database_state* s, const char* db_path) {
int return_code;
return_code = sqlite3_open_v2(db_path, &s->vm, SQLITE_OPEN_READWRITE, NULL);
assert(return_code == SQLITE_OK);
}
void database_state_free(struct database_state* s) {
sqlite3_close_v2(s->vm);
}
void database_write_active(struct database_state* s, int collar_id, int active_status) {
const char query[] = "UPDATE collar SET active=? WHERE id=?";
int return_code;
sqlite3_stmt* stmt;
return_code = sqlite3_prepare_v2(s->vm, query, sizeof(query), &stmt, NULL);
assert(return_code == SQLITE_OK);
return_code = sqlite3_bind_int(stmt, 1, active_status);
assert(return_code == SQLITE_OK);
return_code = sqlite3_bind_int(stmt, 2, collar_id);
assert(return_code == SQLITE_OK);
do {
return_code = sqlite3_step(stmt);
} while(return_code == SQLITE_ROW);
assert(return_code == SQLITE_DONE);
}
void database_write_location(struct database_state* s, int collar_id, double x, double y) {
const char query[] = "INSERT INTO data_point(collar_id, longitude, latitude, datetime)"
"VALUES (?, ?, ?, datetime('now'))";
int return_code;
sqlite3_stmt* stmt;
return_code = sqlite3_prepare_v2(s->vm, query, sizeof(query), &stmt, NULL);
assert(return_code == SQLITE_OK);
return_code = sqlite3_bind_int(stmt, 1, collar_id);
assert(return_code == SQLITE_OK);
return_code = sqlite3_bind_double(stmt, 2, x);
assert(return_code == SQLITE_OK);
return_code = sqlite3_bind_double(stmt, 3, y);
assert(return_code == SQLITE_OK);
do {
return_code = sqlite3_step(stmt);
} while(return_code == SQLITE_ROW);
assert(return_code == SQLITE_DONE);
}

14
database.h Normal file
View File

@@ -0,0 +1,14 @@
#include <sqlite3.h>
extern "C" {
struct database_state {
sqlite3* vm;
};
void database_state_init(struct database_state* s, const char* db_path);
void database_state_free(struct database_state* s);
void database_write_active(struct database_state* s, int collar_id, int active_status);
void database_write_location(struct database_state* s, int collar_id, double x, double y);
}

25
main.c
View File

@@ -21,6 +21,8 @@
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "protobuf.h"
#include "database.h"
// #############################################
// #############################################
@@ -148,6 +150,8 @@ typedef unsigned char byte;
static const int CHANNEL = 0;
database_state db;
char message[256];
const char tok[2] = "|";
bool sx1272 = true;
@@ -171,7 +175,7 @@ int RST = 0;
sf_t sf = SF7;
// Set center frequency
uint32_t freq = 915000000; // in Mhz! (868.1)
uint32_t freq = 915000000; // in Mhz! (915)
byte hello[32] = "HELLO";
@@ -382,7 +386,14 @@ void receivepacket() {
printf("Payload: %s\n", message);
*/
printf("Received message: %s", message);
Fenceless__CollarResponse * m = decode_update(sizeof(message), (uint8_t*) message);
printf("Received message (x, y): ");
printf("%f ", m->loc->x);
printf("%f", m->loc->y);
database_write_location(&db, 1, m->loc->x, m->loc->y);
} // received a message
} // dio0=1
@@ -446,7 +457,12 @@ void txlora(byte *frame, byte datalen) {
}
int main (int argc, char *argv[]) {
if (argc != 2) {
printf("./a.out [Database Location]");
return 1;
} else {
database_state_init(&db, argv[1]);
}
wiringPiSetup () ;
pinMode(ssPin, OUTPUT);
pinMode(dio0, INPUT);
@@ -466,6 +482,7 @@ int main (int argc, char *argv[]) {
delay(1);
}
database_state_free(&db);
return (0);
return 0;
}

View File

@@ -3,8 +3,8 @@ syntax = "proto2";
package Fenceless;
message Coordinate {
required double x = 1;
required double y = 2;
required float x = 1;
required float y = 2;
}
message CollarResponse {

9
sample.c Normal file
View File

@@ -0,0 +1,9 @@
#include "database.h"
int main() {
struct database_state state;
database_state_init(&state, "data.sqlite");
database_write_active(&state, 1, 1);
database_write_location(&state, 1, 5.0, 5.0);
database_state_free(&state);
}