receive does not work

This commit is contained in:
sessionm21 2020-05-15 07:19:37 +01:00
parent 6c64120340
commit a1d0404a47

View File

@ -37,7 +37,7 @@
#include "gateway/message.pb.h"
// DHT digital pin and sensor type
#define DHTPIN 10
#define DHTPIN 60
#define DHTTYPE DHT22
//
@ -87,17 +87,17 @@ static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 60;
const unsigned TX_INTERVAL = 10;
// Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = {
.nss = 10,
.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,
.nss = 10,
.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
@ -150,8 +150,8 @@ int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
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;
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
@ -167,130 +167,187 @@ 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) {
Fenceless_Coordinates m;
pb_istream_t stream = pb_istream_from_buffer(protobuffer, size);
int status = pb_decode(&stream, Fenceless_Coordinates_fields, &m);
if(!status){
#define TYPE_STRING 0x0A
#define TYPE_VARIANT 0x10
#define PROTO_LEN 0x0A
#define FIELD_ONE_FLOAT 0x0D
#define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10
#define FIELD_ONE_VARIANT 0x08
#define FIELD_TWO_STRING 0x12
#define FIELD_THREE_STRING 0x1A
#define FIELD_FOUR_STRING 0x22
#define FIELD_FIVE_STRING 0x2A
#define FIELD_SIX_STRING 0x32
#define FIELD_SEVEN_STRING 0x3A
#define FIELD_EIGHT_STRING 0x42
#define FIELD_NINE_STRING 0x4A
#define FIELD_TEN_STRING 0x52
#define FIELD_ELEVEN_STRING 0x5A
/*uint8_t buffer0[] {
FIELD_ONE_VARIANT, 0x01,
FIELD_TWO_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x1B, 0x91, 0xF6, 0xC2,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_THREE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0xB5, 0x3B, 0x32, 0x42,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FOUR_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_FIVE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SIX_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_SEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_EIGHT_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_NINE_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_TEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40,
FIELD_ELEVEN_STRING, 0x0A,
FIELD_ONE_FLOAT, 0x00, 0x00, 0x80, 0x3F,
FIELD_TWO_FLOAT, 0x00, 0x00, 0x00, 0x40
};*/
Serial.println("DECODE FUNCTION");
if(size != 122) {
Serial.println("Failed to decode");
Serial.print("Size:");
Serial.println(size);
return;
}
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);
}
/*
* this stuff does not work yet
*/
/*coord coordinates[10];
int32_t isr;
isr = 0;
isr = protobuffer[1];
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);
}
Serial.println("Recieved valid protobuf data?");
clear_verts();
for(int i=0;i<isr;i++) {
push_vert(coordinates[i].x, coordinates[i].y);
}*/
}
void onEvent (ev_t ev) {
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_RFU1:
|| Serial.println(F("EV_RFU1"));
|| break;
*/
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
/*
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"));
for (int k=0; k<LMIC.dataLen; k++) {
Serial.print(LMIC.frame[k]);
}
}*/
// Schedule next transmission
//os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_SCAN_FOUND:
|| Serial.println(F("EV_SCAN_FOUND"));
|| break;
*/
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
Serial.print(os_getTime());
Serial.print(": ");
switch(ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_RFU1:
|| Serial.println(F("EV_RFU1"));
|| break;
*/
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
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)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
Serial.println(F(" bytes of payload"));
}
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
break;
case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_SCAN_FOUND:
|| Serial.println(F("EV_SCAN_FOUND"));
|| break;
*/
case EV_TXSTART:
Serial.println(F("EV_TXSTART"));
break;
case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED"));
break;
case EV_RXSTART:
/* do not print anything -- it wrecks timing */
break;
case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break;
default:
Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev);
break;
}
}
#define TYPE_STRING 0x0A
@ -300,13 +357,15 @@ void onEvent (ev_t ev) {
#define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10
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};
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};
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
@ -315,44 +374,31 @@ 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.
double a = 100.0;
for(int i=0;i<4;i++)
{
char buff[20];
sprintf(buff, "%04x", (uint8_t)a<<8*i);
Serial.println(buff);
}
float latitude = 123;
float longitude = 456;
latitude = 44.558308;
longitude = -123.28341;
memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 4);
LMIC_setTxData2(1, buffer, sizeof(buffer)-1, 0);
LMIC_setTxData2(1, buffer, 14, 0);
}
// Next TX is scheduled after TX_COMPLETE event.
}
void setup() {
delay(5000);
delay(1000);
Serial.begin(9600);
ss.begin(4800);
while (! Serial);
delay(100);
Serial.println(F("Starting"));
pinMode(LED_BUILTIN, OUTPUT);
push_vert(44.55818, -123.28341);
push_vert(44.55818, -123.28332);
push_vert(44.558308, -123.28332);
push_vert(44.558308, -123.28341);
// LMIC init
os_init();
// Reset the MAC state. Session and pending data transfers will be discarded.
@ -390,8 +436,9 @@ void setup() {
// Set data rate and transmit power for uplink
LMIC_setDrTxpow(DR_SF7,14);
delay(100);
// Start job
//do_send(&sendjob);
// do_send(&sendjob);
}
/****************************************************
@ -399,10 +446,10 @@ void setup() {
***************************************************/
int read_gps() {
int ret = 0;
while(ss.available()>0) {
gps.encode(ss.read());
ret = 1;
}
// while(ss.available()>0) {
// gps.encode(ss.read());
// ret = 1;
// }
return ret;
}
/****************************************************
@ -429,11 +476,11 @@ enum STATE_ {
WAITING_LORA,
LORA_DONE
};
// int state = START_GPS;
int state = SENDING_LORA;
int loopCounter = 0;
int startTime = 0;
void loop() {
if(state == START_GPS) {
@ -447,10 +494,10 @@ void loop() {
* loading bar animation
***************************************************/
if(got_data) {
if(loopCounter%100==0)
Serial.write('.');
//if(loopCounter%100==0)
// Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) {
clear_line();
// clear_line();
loopCounter=0;
state = VERIFYING_GPS;
@ -478,68 +525,36 @@ void loop() {
state = WAITING_GPS;
}
else if(state == SENDING_LORA) {
/****************************************************
* encode device information into a buffer using
* protobuf
***************************************************/
// Fenceless_CollarResponse coord;
// coord.loc.x = gps.location.lat();
// coord.loc.y = gps.location.lng();
// coord.oob = check_bounds(coord.loc.x, coord.loc.y);
// pb_ostream_t stream;
// stream = pb_ostream_from_buffer(loraData, sizeof(loraData));
// pb_encode(&stream, Fenceless_CollarResponse_fields, &coord);
/****************************************************
* send encoded buffer over LoRaWAN
***************************************************/
Serial.println("Sending LoRa Data...");
do_send(&sendjob);
//lora.sendData(loraData, stream.bytes_written, lora.frameCounter);
//lora.sendData(buffer, sizeof(buffer), lora.frameCounter);
if (LMIC.dataLen) {
Serial.println("Received data back from the gateway: ");
for (int i=0; i<LMIC.dataLen; i++)
Serial.print(LMIC.frame[i]);
}
//Serial.print("Frame Counter: ");
//Serial.println(lora.frameCounter);
/****************************************************
* set reference time for LoRaWAN transmission delay
***************************************************/
startTime = millis();
startTime = millis() / 1000;
state = WAITING_LORA;
}
else if(state == WAITING_LORA) {
/****************************************************
* don't block the GPS from reading here
***************************************************/
read_gps();
// read_gps();
/****************************************************
* if enough seconds have been delayed then move to
* next state
***************************************************/
if(millis()/1000 - startTime >= TX_INTERVAL) {
if((millis()/1000 - startTime) >= TX_INTERVAL) {
Serial.println("Lora has finished waiting");
state = LORA_DONE;
}
}
else if(state == LORA_DONE) {
state = VERIFYING_GPS;
state = SENDING_LORA;
}
//os_runloop_once();
os_runloop_once();
}