gps still does not work

This commit is contained in:
sessionm21
2020-05-15 09:49:46 +01:00
parent a1d0404a47
commit 1b11b44336
4 changed files with 127 additions and 910 deletions

View File

@@ -33,7 +33,6 @@
#include "pb_encode.h"
#include "pb_decode.h"
#include "lmic_project_config.h"
#include "gateway/message.pb.h"
// DHT digital pin and sensor type
@@ -95,9 +94,6 @@ const lmic_pinmap lmic_pins = {
.rxtx = LMIC_UNUSED_PIN,
.rst = 9,
.dio = {2, 3, LMIC_UNUSED_PIN},
.rxtx_rx_active = 0,
.rssi_cal = 8, // LBT cal for the Adafruit Feather M0 LoRa, in dB
.spi_freq = 8000000,
};
// init. DHT
@@ -110,7 +106,7 @@ const lmic_pinmap lmic_pins = {
* - Software Serial
***************************************************/
TinyGPSPlus gps;
SoftwareSerial ss(6, 7);
// SoftwareSerial ss(6, 7);
/****************************************************
* Track each pair of X and Y coordinates
@@ -157,6 +153,8 @@ int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
}
/****************************************************
* Test a coordinate against all vertices
* - takes current GPS coordinates
* - return 1 if in bounds
***************************************************/
int check_bounds(float x, float y) {
return pnpoly(n_poly, polyx, polyy, x, y);
@@ -167,9 +165,6 @@ int check_bounds(float x, float y) {
* - loading arrays in nanopb does not appear
* to work.
***************************************************/
typedef struct {
float x,y;
} coord;
void import_protobuf(uint8_t *protobuffer, uint32_t size) {
#define TYPE_STRING 0x0A
@@ -235,26 +230,40 @@ void import_protobuf(uint8_t *protobuffer, uint32_t size) {
* this stuff does not work yet
*/
/*coord coordinates[10];
int32_t isr;
uint32_t isr;
isr = 0;
isr = protobuffer[1];
isr = 0;
isr = protobuffer[1];
Serial.print("Isr: ");
Serial.println(isr);
uint8_t *ptr = protobuffer+5;
for(int i=0;i<isr;i++)
{
memcpy(&coordinates[i].x, ptr + i*12, 4);
memcpy(&coordinates[i].y, ptr + i*12+5, 4);
}
if(isr>N_POLY_MAX) isr = N_POLY_MAX;
Serial.println("Recieved valid protobuf data?");
clear_verts();
for(int i=0;i<isr;i++) {
push_vert(coordinates[i].x, coordinates[i].y);
}*/
Serial.println("Recieved valid protobuf data?");
clear_verts();
uint8_t *ptr = protobuffer+5;
for(uint32_t i=0;i<isr;i++) {
if(i%5==0)
Serial.println();
float x,y;
memcpy(&x, ptr + i*12, 4);
memcpy(&y, ptr + i*12+5, 4);
Serial.print('(');
Serial.print(x);
Serial.print(',');
Serial.print(y);
Serial.print(')');
Serial.print(' ');
push_vert(x, y);
}
}
void do_send(osjob_t* j);
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
@@ -294,18 +303,28 @@ void onEvent (ev_t ev) {
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
// if (LMIC.dataLen) {
// import_protobuf(LMIC.frame, LMIC.dataLen);
// }
if (LMIC.txrxFlags & TXRX_ACK)
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
// Serial.println(F(" bytes of payload"));
// for(int i=0;i<LMIC.dataLen;i++) {
// Serial.print(LMIC.frame[LMIC.dataBeg + i], HEX);
// Serial.print(' ');
// Serial.print('-');
// Serial.print(' ');
// if(i%10==0)
// Serial.println();
// }
// Serial.println();
import_protobuf(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
}
os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
@@ -358,14 +377,14 @@ void onEvent (ev_t ev) {
#define FIELD_TWO_VARIANT 0x10
void do_send(osjob_t* j){
uint8_t buffer[15] = {
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0, 0};
uint8_t buffer[] = {
TYPE_STRING,
PROTO_LEN,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x48, 0x43,
FIELD_TWO_FLOAT, 0x00, 0x00, 0xc8, 0x42,
FIELD_TWO_VARIANT, 0};
void do_send(osjob_t* j){
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
@@ -374,25 +393,23 @@ void do_send(osjob_t* j){
// transmit on port 1 (the first parameter); you can use any value from 1 to 223 (others are reserved).
// don't request an ack (the last parameter, if not zero, requests an ack from the network).
// Remember, acks consume a lot of network resources; don't ask for an ack unless you really need it.
float latitude = 123;
float longitude = 456;
latitude = 44.558308;
longitude = -123.28341;
float latitude = gps.location.lat();
float longitude = gps.location.lng();
int oob = check_bounds(latitude, longitude);
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
memcpy(buffer+13, (void*)&oob, 1);
LMIC_setTxData2(1, buffer, 14, 0);
LMIC_setTxData2(1, buffer, sizeof(buffer), 0);
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
delay(1000);
Serial.begin(9600);
ss.begin(4800);
Serial.begin(4800);
// ss.begin(4800);
delay(100);
Serial.println(F("Starting"));
@@ -436,7 +453,7 @@ void setup() {
// Set data rate and transmit power for uplink
LMIC_setDrTxpow(DR_SF7,14);
delay(100);
delay(2000);
// Start job
// do_send(&sendjob);
}
@@ -446,10 +463,12 @@ void setup() {
***************************************************/
int read_gps() {
int ret = 0;
// while(ss.available()>0) {
// gps.encode(ss.read());
// ret = 1;
// }
// int timeout = 0;
while(Serial.available()>0) {// || timeout < 20) {
gps.encode(Serial.read());
ret = 1;
//timeout++;
}
return ret;
}
/****************************************************
@@ -458,12 +477,17 @@ int read_gps() {
const int16_t PROGRESS_BAR_COUNT = 50;
const int16_t START_OF_LINE = 13;
int led_on = 0;
void clear_line() {
Serial.write(START_OF_LINE);
for(int i=0;i<PROGRESS_BAR_COUNT;i++)
Serial.write(' ');
Serial.write(START_OF_LINE);
digitalWrite(LED_BUILTIN, led_on);
led_on = !led_on;
}
/****************************************************
* State variables
* - track events of main loop
@@ -476,85 +500,62 @@ enum STATE_ {
WAITING_LORA,
LORA_DONE
};
// int state = START_GPS;
int state = SENDING_LORA;
int state = START_GPS;
int loopCounter = 0;
int startTime = 0;
uint32_t got_data = 0;
void loop() {
if(state == START_GPS) {
Serial.println("Waiting for GPS");
state = WAITING_GPS;
}
else if(state == WAITING_GPS) {
int got_data =
read_gps();
/****************************************************
* loading bar animation
***************************************************/
if(got_data) {
//if(loopCounter%100==0)
// Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) {
// clear_line();
loopCounter=0;
state = VERIFYING_GPS;
}
loopCounter++;
}
}
else if(state == VERIFYING_GPS) {
/****************************************************
* if no data has been received from the gps in 5 seconds
* then the GPS is probably not connected properly
***************************************************/
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
/****************************************************
* only send to LoRaWAN if valid GPS coordinates are
* available
***************************************************/
if(gps.location.isValid())
state = SENDING_LORA;
else
switch(state) {
case START_GPS:
Serial.println("Waiting for GPS");
state = WAITING_GPS;
}
else if(state == SENDING_LORA) {
/****************************************************
* send encoded buffer over LoRaWAN
***************************************************/
Serial.println("Sending LoRa Data...");
do_send(&sendjob);
break;
case WAITING_GPS:
got_data =
read_gps();
/****************************************************
* loading bar animation
***************************************************/
if(got_data) {
if(loopCounter%100==0)
Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) {
clear_line();
loopCounter=0;
/****************************************************
* set reference time for LoRaWAN transmission delay
***************************************************/
startTime = millis() / 1000;
state = WAITING_LORA;
}
else if(state == WAITING_LORA) {
/****************************************************
* don't block the GPS from reading here
***************************************************/
// read_gps();
state = VERIFYING_GPS;
}
loopCounter++;
}
break;
case VERIFYING_GPS:
/****************************************************
* if no data has been received from the gps in 5 seconds
* then the GPS is probably not connected properly
***************************************************/
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
/****************************************************
* only send to LoRaWAN if valid GPS coordinates are
* available
***************************************************/
if(gps.location.isValid())
state = SENDING_LORA;
else
state = WAITING_GPS;
break;
case SENDING_LORA:
do_send(&sendjob);
/****************************************************
* if enough seconds have been delayed then move to
* next state
***************************************************/
if((millis()/1000 - startTime) >= TX_INTERVAL) {
Serial.println("Lora has finished waiting");
digitalWrite(LED_BUILTIN, 0);
state = LORA_DONE;
}
}
else if(state == LORA_DONE) {
state = VERIFYING_GPS;
state = SENDING_LORA;
break;
default:
break;
}
os_runloop_once();
}