send coordinates over lorawan

This commit is contained in:
sessionm21 2020-04-19 04:07:23 +01:00
parent 2c146ff224
commit 5a7003d621
6 changed files with 366 additions and 101 deletions

17
.clang Normal file
View File

@ -0,0 +1,17 @@
-Datmega328p
-Dmega328p
-DF_CPU=16000000L
-DARDUINO=200
-DARDUINO_AVR_NANO
-DARDUINO_ARCH_AVR
-D__AVR_ATmega328P
-std=c++11
-I./external/ArduinoCore-avr/libraries/SPI/src
-I./external/ArduinoCore-avr/cores/arduino
-I./external/ArduinoCore-avr/variants/eightanaloginputs
-I./libraries/arduino-LoRa/src
-I./libraries/TinyGPSPlus/src
-I./libraries/TinyLoRa
-I./external/nanopb
-I./protobuf
-I./external/ArduinoCore-avr/libraries/SoftwareSerial/src

6
.ctags Normal file
View File

@ -0,0 +1,6 @@
--recurse=yes
--exclude=.git
--exclude=vendor/*
--exclude=node_modules/*
--exclude=db/*
--exclude=log/*

3
.gitmodules vendored
View File

@ -13,3 +13,6 @@
[submodule "libraries/TinyGPSPlus"] [submodule "libraries/TinyGPSPlus"]
path = libraries/TinyGPSPlus path = libraries/TinyGPSPlus
url = https://github.com/mikalhart/TinyGPSPlus.git url = https://github.com/mikalhart/TinyGPSPlus.git
[submodule "libraries/arduino-lmic"]
path = libraries/arduino-lmic
url = https://github.com/mcci-catena/arduino-lmic.git

View File

@ -31,6 +31,7 @@ INC_DIRS?=-I./$(ARDUINO_DIR)/libraries/SPI/src\
-I./$(ARDUINO_DIR)/variants/eightanaloginputs\ -I./$(ARDUINO_DIR)/variants/eightanaloginputs\
-I./libraries/arduino-LoRa/src\ -I./libraries/arduino-LoRa/src\
-I./libraries/TinyGPSPlus/src\ -I./libraries/TinyGPSPlus/src\
-I./libraries/TinyLoRa\
-I./$(NANOPB_DIR)\ -I./$(NANOPB_DIR)\
-I./protobuf\ -I./protobuf\
-I./$(ARDUINO_DIR)/libraries/SoftwareSerial/src/ -I./$(ARDUINO_DIR)/libraries/SoftwareSerial/src/
@ -46,19 +47,26 @@ SRC_FILES?=./$(ARDUINO_DIR)/cores/arduino/main.cpp\
./$(ARDUINO_DIR)/cores/arduino/HardwareSerial1.cpp\ ./$(ARDUINO_DIR)/cores/arduino/HardwareSerial1.cpp\
./$(ARDUINO_DIR)/cores/arduino/HardwareSerial2.cpp\ ./$(ARDUINO_DIR)/cores/arduino/HardwareSerial2.cpp\
./$(ARDUINO_DIR)/cores/arduino/HardwareSerial3.cpp\ ./$(ARDUINO_DIR)/cores/arduino/HardwareSerial3.cpp\
./$(ARDUINO_DIR)/libraries/SPI/src/SPI.cpp\
./$(ARDUINO_DIR)/cores/arduino/WString.cpp\ ./$(ARDUINO_DIR)/cores/arduino/WString.cpp\
./$(ARDUINO_DIR)/cores/arduino/Print.cpp\ ./$(ARDUINO_DIR)/cores/arduino/Print.cpp\
./$(ARDUINO_DIR)/cores/arduino/Stream.cpp\ ./$(ARDUINO_DIR)/cores/arduino/Stream.cpp\
./$(ARDUINO_DIR)/cores/arduino/new.cpp\ ./$(ARDUINO_DIR)/cores/arduino/new.cpp\
./$(ARDUINO_DIR)/cores/arduino/abi.cpp\ ./$(ARDUINO_DIR)/cores/arduino/abi.cpp\
./$(ARDUINO_DIR)/libraries/SoftwareSerial/src/SoftwareSerial.cpp\ ./$(ARDUINO_DIR)/libraries/SoftwareSerial/src/SoftwareSerial.cpp\
./$(ARDUINO_DIR)/libraries/SPI/src/SPI.cpp\
./libraries/arduino-LoRa/src/LoRa.cpp\ ./libraries/arduino-LoRa/src/LoRa.cpp\
./libraries/TinyGPSPlus/src/TinyGPS++.cpp\ ./libraries/TinyGPSPlus/src/TinyGPS++.cpp\
./libraries/TinyLoRa/TinyLoRa.cpp\
./$(NANOPB_DIR)/pb_encode.c\ ./$(NANOPB_DIR)/pb_encode.c\
./$(NANOPB_DIR)/pb_decode.c\ ./$(NANOPB_DIR)/pb_decode.c\
./$(NANOPB_DIR)/pb_common.c\ ./$(NANOPB_DIR)/pb_common.c\
./gateway/message.pb.c ./protobuf/gateway/message.pb.c
SRC_FILES_COMMON?=\
./$(NANOPB_DIR)/pb_encode.c\
./$(NANOPB_DIR)/pb_decode.c\
./$(NANOPB_DIR)/pb_common.c\
./protobuf/gateway/message.pb.c
build: $(OUT)$(NAME) build: $(OUT)$(NAME)
@ -66,21 +74,31 @@ $(OUT)$(NAME): $(OUT)$(NAME).elf
$(OBJCOPY) -R .eeprom -O ihex $(OUT)$(NAME).elf $(OUT)$(NAME).hex $(OBJCOPY) -R .eeprom -O ihex $(OUT)$(NAME).elf $(OUT)$(NAME).hex
$(OUT)eep.hex: $(OUT)$(NAME).elf $(OUT)eep.hex: $(OUT)$(NAME).elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(OUT)$(NAME).elf $(OUT)eep.hex $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(OUT)$(NAME).elf $(OUT)eep.hex
$(OUT)$(NAME).elf: protobuf bin $(OUT)$(NAME).elf: protobufg bin
$(CC) $(CFLAGS) -o $(OUT)$(NAME).elf collar.cpp $(INC_DIRS) $(SRC_FILES) $(CC) $(CFLAGS) -o $(OUT)$(NAME).elf collar.cpp $(INC_DIRS) $(SRC_FILES)
protobuf: test:
g++ -o $(OUT)test prototest.cpp $(INC_DIRS) $(SRC_FILES_COMMON)
$(OUT)test
protobufg:
mkdir -p protobuf/gateway mkdir -p protobuf/gateway
python3 external/nanopb/generator/nanopb_generator.py gateway/message.proto -D protobuf python3 external/nanopb/generator/nanopb_generator.py gateway/message.proto -D protobuf
bin: bin:
mkdir bin mkdir bin
flash: flash: $(OUT)$(NAME)
avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:$(OUT)$(NAME).hex:i avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:$(OUT)$(NAME).hex:i
run: flash
screen /dev/ttyUSB0
start: flash
systemctl start lora-gateway-bridge loraserver
stop: stop:
avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:nothing.hex:i avrdude -v -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:nothing.hex:i
systemctl stop lora-gateway-bridge loraserver
clean: clean:
rm -r protobuf rm -r protobuf

View File

@ -1,131 +1,211 @@
// Hello LoRa - ABP TTN Packet Sender (Multi-Channel)
// Tutorial Link: https://learn.adafruit.com/the-things-network-for-feather/using-a-feather-32u4
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Copyright 2015, 2016 Ideetron B.V.
//
// Modified by Brent Rubell for Adafruit Industries, 2018
/************************** Configuration ***********************************/
#include <TinyLoRa.h>
#include <SPI.h> #include <SPI.h>
#include <LoRa.h> #include "gateway/message.pb.h"
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "pb_common.h" #include "pb_common.h"
#include "pb.h" #include "pb.h"
#include "pb_encode.h" #include "pb_encode.h"
#include "pb_decode.h"
#include "gateway/message.pb.h" #include "gateway/message.pb.h"
unsigned long previousMillis = 0; // Visit your thethingsnetwork.org device console
const long interval = 5000; // to create an account, or if you need your session keys.
float lat = 0;
float lng = 0;
bool sending = false; // Network Session Key (MSB)
uint8_t NwkSkey[16] = {0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x55, 0x5E, 0xE4, 0xB9, 0x9E, 0x9B, 0x88, 0x66, 0x47, 0xF1};
TinyGPSPlus gps; // Application Session Key (MSB)
uint8_t AppSkey[16] = {0xC4, 0x30, 0xEF, 0x56, 0x4F, 0x6D, 0xA2, 0x56, 0x1F, 0x15, 0x2F, 0xB8, 0x62, 0xC7, 0xCA, 0xC2};
SoftwareSerial ss(6, 7); // Device Address (MSB)
uint8_t DevAddr[4] = {0x26, 0x02, 0x12, 0xB6};
typedef struct lora_status_s { /************************** Example Begins Here ***********************************/
int sleep; // Data Packet to Send to TTN
} lora_status_t; unsigned char loraData[50] = {"hello LoRa"};
static lora_status_t lora_status; // How many times data transfer should occur, in seconds
const unsigned int sendInterval = 30;
void transmitLora() { // Pinout for Adafruit Feather 32u4 LoRa
// Encode as protobuf packet TinyLoRa lora = TinyLoRa(2, 10, 9);
if (lat && lng) {
uint8_t buffer[50] = {0}; // Pinout for Adafruit Feather M0 LoRa
Fenceless_CollarResponse m = Fenceless_CollarResponse_init_zero; //TinyLoRa lora = TinyLoRa(3, 8, 4);
const uint8_t N_POLY_MAX=100;
float polyx[N_POLY_MAX];
float polyy[N_POLY_MAX];
int n_poly=0;
int push_vert(float x, float y) {
if(n_poly>N_POLY_MAX)
return 0;
polyx[n_poly]=x;
polyy[n_poly]=y;
n_poly++;
return 1;
}
void clear_verts() {
n_poly=0;
}
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{ //from stack overflow
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
int check_bounds(float x, float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
}
bool Fenceless_Coordinates_callback(pb_istream_t *stream, const pb_field_iter_t *field, void **arg) {
Serial.println("Called");
while(stream->bytes_left) {
Fenceless_Coordinate m = Fenceless_Coordinate_init_zero;
if(!pb_decode(stream, Fenceless_Coordinate_fields, &m)) {
return false;
}
push_vert(m.x,m.y);
return true;
}
}
void import_protobuf(uint8_t *protobuffer, uint32_t size) {
Fenceless_Coordinates m;
pb_istream_t stream = pb_istream_from_buffer(protobuffer, size);
int status = pb_decode(&stream, Fenceless_Coordinates_fields, &m);
if(!status){
Serial.println("Failed to decode");
}
clear_verts();
switch(m.isr) {
case 10:
push_vert(m.coord9.x, m.coord9.y);
case 9:
push_vert(m.coord8.x, m.coord8.y);
case 8:
push_vert(m.coord7.x, m.coord7.y);
case 7:
push_vert(m.coord6.x, m.coord6.y);
case 6:
push_vert(m.coord5.x, m.coord5.y);
case 5:
push_vert(m.coord4.x, m.coord4.y);
case 4:
push_vert(m.coord3.x, m.coord3.y);
case 3:
push_vert(m.coord2.x, m.coord2.y);
push_vert(m.coord1.x, m.coord1.y);
push_vert(m.coord0.x, m.coord0.y);
}
}
bool Fenceless_Coordinates_encode(pb_ostream_t *stream, const pb_field_iter_t *field, void * const * arg) {
Serial.println("Encode called");
return false;
Fenceless_Coordinate c = *(Fenceless_Coordinate*)field->pData;
if(!pb_encode_tag_for_field(stream, field)) {
return false;
}
return pb_encode(stream, Fenceless_Coordinate_fields, field);
}
void test() {
Serial.println("Testing protobuf");
uint8_t buffer[100] = {0};
Fenceless_Coordinates m = Fenceless_Coordinates_init_zero;
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
m.loc.x = lat; int status = pb_encode(&stream, Fenceless_Coordinates_fields, &m);
m.loc.y = lng; if(!status){
int status = pb_encode(&stream, Fenceless_CollarResponse_fields, &m);
if (!status) {
Serial.println("Failed to encode"); Serial.println("Failed to encode");
} }
import_protobuf(buffer, sizeof(buffer));
Serial.print("Sending packet Lat / Long: ");
Serial.println(lat);
Serial.println(lng);
// send packet
LoRa.beginPacket();
LoRa.print((char *) buffer);
LoRa.endPacket();
}
} }
void updateGPS() { int recieved = 0;
// Gather GPS data void on_recieve(int n) {
if (gps.location.isValid()) { recieved = 1;
lat = gps.location.lat();
lng = gps.location.lng();
} else {
lat = 0;
lng = 0;
}
} }
ISR(TIMER1_OVF_vect) { void setup()
// handle timer overflow interrupt at 1 per second {
updateGPS(); delay(2000);
if(!gps.location.isValid()) {
Serial.println("Seeking GPS");
}
if(sending && gps.location.isValid()) {
if(lora_status.sleep) {
}
transmitLora();
}
}
void initialize_timer() {
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 34286; // timer preload
TCCR1B |= (1<<CS12); // prescale: 256 bits
TIMSK1 |= (1<<TOIE1); // overflow interrupt
interrupts();
}
void setup() {
Serial.begin(9600); Serial.begin(9600);
ss.begin(9600); while (! Serial);
while (!Serial); delay(4000);
// Initialize pin LED_BUILTIN as an output
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("LoRa Transceiver"); // Initialize LoRa
Serial.print("Starting LoRa...");
if (!LoRa.begin(915E6)) { // define multi-channel sending
Serial.println("Starting LoRa failed!"); lora.setChannel(MULTI);
while (1); // set datarate
lora.setDatarate(SF7BW125);
if(!lora.begin())
{
Serial.println("Failed");
Serial.println("Check your radio");
while(true);
} }
LoRa.setSpreadingFactor(7);
lora_status.sleep = 1; Fenceless_Coordinates coords;
//lora.sleep(); coords.isr = 10;
coords.coord0.x = 1; coords.coord0.y = 2;
coords.coord1.x = 1; coords.coord1.y = 2;
coords.coord2.x = 1; coords.coord2.y = 2;
coords.coord3.x = 1; coords.coord3.y = 2;
coords.coord4.x = 1; coords.coord4.y = 2;
coords.coord5.x = 1; coords.coord5.y = 2;
coords.coord6.x = 1; coords.coord6.y = 2;
coords.coord7.x = 1; coords.coord7.y = 2;
coords.coord8.x = 1; coords.coord8.y = 2;
coords.coord9.x = 1; coords.coord9.y = 2;
initialize_timer(); pb_ostream_t stream;
stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
int err = pb_encode(&stream, Fenceless_Coordinates_fields, &coords);
// Optional set transmit power. If not set default is +17 dBm.
// Valid options are: -80, 1 to 17, 20 (dBm).
// For safe operation in 20dBm: your antenna must be 3:1 VWSR or better
// and respect the 1% duty cycle.
lora.setPower(1);
Serial.println("OK");
} }
void loop() { void loop()
/* Get user input {
*/ Serial.println("Sending LoRa Data...");
if(Serial.available()) { lora.sendData(loraData, sizeof(loraData), lora.frameCounter);
char n = Serial.read(); // Optionally set the Frame Port (1 to 255)
if(n=='p') { // uint8_t framePort = 1;
sending ? Serial.println("Sending is: disabled") : Serial.println("Sending is: enabled"); // lora.sendData(loraData, sizeof(loraData), lora.frameCounter, framePort);
sending = !sending; Serial.print("Frame Counter: ");Serial.println(lora.frameCounter);
lora.frameCounter++;
// blink LED to indicate packet sent
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("delaying...");
if(recieved) {
Serial.println("Recieved something");
} else { } else {
Serial.println("Type the letter 'p' to toggle sending"); Serial.println("Nothing yet");
}
}
/* GPS data recieved */
while (ss.available() > 0) {
gps.encode(ss.read());
}
/* Lora data recieved */
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.print("Received Packet: ");
while (LoRa.available()) {
Serial.print((char) LoRa.read());
}
Serial.println();
} }
delay(sendInterval * 1000);
} }

141
prototest.cpp Normal file
View File

@ -0,0 +1,141 @@
#include <iostream>
#include <cassert>
#include "pb_common.h"
#include "pb.h"
#include "pb_encode.h"
#include "pb_decode.h"
#include "gateway/message.pb.h"
/*
* Try to get repeating element to work with nanopb
* for coordinates
*/
using namespace std;
void print_buffer(uint8_t *buffer, int size) {
for(int i=0;i<size;i++) {
printf("%02x ", buffer[i]);
}
printf("\n");
}
int main() {
uint8_t buffer[1000] = {0};
pb_ostream_t stream;
pb_istream_t stream0;
int err;
// encode a coordinate
Fenceless_Coordinate coord = Fenceless_Coordinate_init_zero;
coord.x = 789;
coord.y = 123;
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
err = pb_encode(&stream, Fenceless_Coordinate_fields, &coord);
assert(err);
print_buffer(buffer, stream.bytes_written);
// decode a coordinate from buffer
Fenceless_Coordinate coord0 = Fenceless_Coordinate_init_zero;
stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
err = pb_decode(&stream0, Fenceless_Coordinate_fields, &coord0);
assert(err);
// check result
assert(coord0.x == coord.x);
assert(coord0.y == coord.y);
// encode a coller response
Fenceless_CollarResponse collar = Fenceless_CollarResponse_init_zero;
collar.loc.x = 200;
collar.loc.y = 100;
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
err = pb_encode(&stream, Fenceless_CollarResponse_fields, &collar);
assert(err);
print_buffer(buffer, stream.bytes_written);
Fenceless_CollarResponse collar0 = Fenceless_CollarResponse_init_zero;
stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
err = pb_decode(&stream0, Fenceless_CollarResponse_fields, &collar0);
assert(err);
// check result
assert(collar0.loc.x == collar.loc.x);
assert(collar0.loc.y == collar.loc.y);
// encode 10 coordinates
Fenceless_Coordinates coords;
coords.coord0.x = 1; coords.coord0.y = 2;
coords.coord1.x = 1; coords.coord1.y = 2;
coords.coord2.x = 1; coords.coord2.y = 2;
coords.coord3.x = 1; coords.coord3.y = 2;
coords.coord4.x = 1; coords.coord4.y = 2;
coords.coord5.x = 1; coords.coord5.y = 2;
coords.coord6.x = 1; coords.coord6.y = 2;
coords.coord7.x = 1; coords.coord7.y = 2;
coords.coord8.x = 1; coords.coord8.y = 2;
coords.coord9.x = 1; coords.coord9.y = 2;
coords.isr = 10;
stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
err = pb_encode(&stream, Fenceless_Coordinates_fields, &coords);
assert(err);
print_buffer(buffer, stream.bytes_written);
// encode multiple coordinates
//Fenceless_Coordinates coords = Fenceless_Coordinates_init_zero;
//coords.loc.funcs.encode = [](pb_ostream_t *stream, const pb_field_t *field, void * const * arg){
// cout << "encode called" << endl;
// int err;
// err = pb_encode_tag_for_field(stream , field);
// assert(err);
// Fenceless_Coordinate coord = Fenceless_Coordinate_init_zero;
// coord.x = 123;
// coord.y = 456;
// err = pb_encode_submessage(stream, Fenceless_Coordinate_fields, &coord);
// assert(err);
// err = pb_encode_tag_for_field(stream , field);
// assert(err);
// coord = Fenceless_Coordinate_init_zero;
// coord.x = 789;
// coord.y = 987;
// err = pb_encode_submessage(stream, Fenceless_Coordinate_fields, &coord);
// assert(err);
// return true;
//};
//memset(buffer, 0, sizeof(buffer)-1);
//stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
//err = pb_encode(&stream, Fenceless_Coordinates_fields, &coords);
//assert(err);
//print_buffer(buffer, stream.bytes_written);
//Fenceless_Coordinates coords0 = Fenceless_Coordinates_init_zero;
//coords0.loc.funcs.decode = [](pb_istream_t *stream, const pb_field_iter_t *field, void ** arg){
// cout << "decode called" << endl;
// printf("Array size: %d\n", field->array_size);
// bool eof;
// uint32_t tag;
// pb_wire_type_t t = PB_WT_STRING;
// pb_decode_tag(stream, &t, &tag, &eof);
// Fenceless_Coordinate coord;
// int err = pb_decode(stream, Fenceless_Coordinate_fields, &coord);
// assert(err);
// printf("%d %d", coord.x,coord.y);
// return false;
//};
//stream0 = pb_istream_from_buffer(buffer, stream.bytes_written);
//err = pb_decode(&stream0, Fenceless_Coordinates_fields, &coords0);
//assert(err);
return 0;
}