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" #include "gateway/message.pb.h"
// DHT digital pin and sensor type // DHT digital pin and sensor type
#define DHTPIN 10 #define DHTPIN 60
#define DHTTYPE DHT22 #define DHTTYPE DHT22
// //
@ -87,17 +87,17 @@ static osjob_t sendjob;
// Schedule TX every this many seconds (might become longer due to duty // Schedule TX every this many seconds (might become longer due to duty
// cycle limitations). // cycle limitations).
const unsigned TX_INTERVAL = 60; const unsigned TX_INTERVAL = 10;
// Pin mapping for Adafruit Feather M0 LoRa // Pin mapping for Adafruit Feather M0 LoRa
const lmic_pinmap lmic_pins = { const lmic_pinmap lmic_pins = {
.nss = 10, .nss = 10,
.rxtx = LMIC_UNUSED_PIN, .rxtx = LMIC_UNUSED_PIN,
.rst = 9, .rst = 9,
.dio = {2, 3, LMIC_UNUSED_PIN}, .dio = {2, 3, LMIC_UNUSED_PIN},
.rxtx_rx_active = 0, .rxtx_rx_active = 0,
.rssi_cal = 8, // LBT cal for the Adafruit Feather M0 LoRa, in dB .rssi_cal = 8, // LBT cal for the Adafruit Feather M0 LoRa, in dB
.spi_freq = 8000000, .spi_freq = 8000000,
}; };
// init. DHT // init. DHT
@ -150,8 +150,8 @@ int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
int i, j, c = 0; int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) { for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) && if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) ) (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c; c = !c;
} }
return c; return c;
} }
@ -167,130 +167,187 @@ int check_bounds(float x, float y) {
* - loading arrays in nanopb does not appear * - loading arrays in nanopb does not appear
* to work. * to work.
***************************************************/ ***************************************************/
typedef struct {
float x,y;
} coord;
void import_protobuf(uint8_t *protobuffer, uint32_t size) { void import_protobuf(uint8_t *protobuffer, uint32_t size) {
Fenceless_Coordinates m;
pb_istream_t stream = pb_istream_from_buffer(protobuffer, size); #define TYPE_STRING 0x0A
int status = pb_decode(&stream, Fenceless_Coordinates_fields, &m); #define TYPE_VARIANT 0x10
if(!status){ #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.println("Failed to decode");
Serial.print("Size:");
Serial.println(size);
return;
} }
clear_verts(); /*
switch(m.isr) { * this stuff does not work yet
case 10: */
push_vert(m.coord9.x, m.coord9.y);
case 9: /*coord coordinates[10];
push_vert(m.coord8.x, m.coord8.y); int32_t isr;
case 8:
push_vert(m.coord7.x, m.coord7.y); isr = 0;
case 7: isr = protobuffer[1];
push_vert(m.coord6.x, m.coord6.y);
case 6: uint8_t *ptr = protobuffer+5;
push_vert(m.coord5.x, m.coord5.y); for(int i=0;i<isr;i++)
case 5: {
push_vert(m.coord4.x, m.coord4.y); memcpy(&coordinates[i].x, ptr + i*12, 4);
case 4: memcpy(&coordinates[i].y, ptr + i*12+5, 4);
push_vert(m.coord3.x, m.coord3.y); }
case 3:
push_vert(m.coord2.x, m.coord2.y); Serial.println("Recieved valid protobuf data?");
push_vert(m.coord1.x, m.coord1.y); clear_verts();
push_vert(m.coord0.x, m.coord0.y); for(int i=0;i<isr;i++) {
} push_vert(coordinates[i].x, coordinates[i].y);
}*/
} }
void onEvent (ev_t ev) { void onEvent (ev_t ev) {
Serial.print(os_getTime()); Serial.print(os_getTime());
Serial.print(": "); Serial.print(": ");
switch(ev) { switch(ev) {
case EV_SCAN_TIMEOUT: case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT")); Serial.println(F("EV_SCAN_TIMEOUT"));
break; break;
case EV_BEACON_FOUND: case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND")); Serial.println(F("EV_BEACON_FOUND"));
break; break;
case EV_BEACON_MISSED: case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED")); Serial.println(F("EV_BEACON_MISSED"));
break; break;
case EV_BEACON_TRACKED: case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED")); Serial.println(F("EV_BEACON_TRACKED"));
break; break;
case EV_JOINING: case EV_JOINING:
Serial.println(F("EV_JOINING")); Serial.println(F("EV_JOINING"));
break; break;
case EV_JOINED: case EV_JOINED:
Serial.println(F("EV_JOINED")); Serial.println(F("EV_JOINED"));
break; break;
/* /*
|| This event is defined but not used in the code. No || This event is defined but not used in the code. No
|| point in wasting codespace on it. || point in wasting codespace on it.
|| ||
|| case EV_RFU1: || case EV_RFU1:
|| Serial.println(F("EV_RFU1")); || Serial.println(F("EV_RFU1"));
|| break; || break;
*/ */
case EV_JOIN_FAILED: case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED")); Serial.println(F("EV_JOIN_FAILED"));
break; break;
case EV_REJOIN_FAILED: case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED")); Serial.println(F("EV_REJOIN_FAILED"));
break; break;
case EV_TXCOMPLETE: case EV_TXCOMPLETE:
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
/*
if (LMIC.txrxFlags & TXRX_ACK) // if (LMIC.dataLen) {
Serial.println(F("Received ack")); // import_protobuf(LMIC.frame, LMIC.dataLen);
if (LMIC.dataLen) { // }
Serial.println(F("Received "));
Serial.println(LMIC.dataLen); if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F(" bytes of payload")); Serial.println(F("Received ack"));
for (int k=0; k<LMIC.dataLen; k++) { if (LMIC.dataLen) {
Serial.print(LMIC.frame[k]); Serial.println(F("Received "));
} Serial.println(LMIC.dataLen);
}*/ Serial.println(F(" bytes of payload"));
// Schedule next transmission }
//os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
break; break;
case EV_LOST_TSYNC: case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC")); Serial.println(F("EV_LOST_TSYNC"));
break; break;
case EV_RESET: case EV_RESET:
Serial.println(F("EV_RESET")); Serial.println(F("EV_RESET"));
break; break;
case EV_RXCOMPLETE: case EV_RXCOMPLETE:
// data received in ping slot // data received in ping slot
Serial.println(F("EV_RXCOMPLETE")); Serial.println(F("EV_RXCOMPLETE"));
break; break;
case EV_LINK_DEAD: case EV_LINK_DEAD:
Serial.println(F("EV_LINK_DEAD")); Serial.println(F("EV_LINK_DEAD"));
break; break;
case EV_LINK_ALIVE: case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE")); Serial.println(F("EV_LINK_ALIVE"));
break; break;
/* /*
|| This event is defined but not used in the code. No || This event is defined but not used in the code. No
|| point in wasting codespace on it. || point in wasting codespace on it.
|| ||
|| case EV_SCAN_FOUND: || case EV_SCAN_FOUND:
|| Serial.println(F("EV_SCAN_FOUND")); || Serial.println(F("EV_SCAN_FOUND"));
|| break; || break;
*/ */
case EV_TXSTART: case EV_TXSTART:
Serial.println(F("EV_TXSTART")); Serial.println(F("EV_TXSTART"));
break; break;
case EV_TXCANCELED: case EV_TXCANCELED:
Serial.println(F("EV_TXCANCELED")); Serial.println(F("EV_TXCANCELED"));
break; break;
case EV_RXSTART: case EV_RXSTART:
/* do not print anything -- it wrecks timing */ /* do not print anything -- it wrecks timing */
break; break;
case EV_JOIN_TXCOMPLETE: case EV_JOIN_TXCOMPLETE:
Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept")); Serial.println(F("EV_JOIN_TXCOMPLETE: no JoinAccept"));
break; break;
default: default:
Serial.print(F("Unknown event: ")); Serial.print(F("Unknown event: "));
Serial.println((unsigned) ev); Serial.println((unsigned) ev);
break; break;
} }
} }
#define TYPE_STRING 0x0A #define TYPE_STRING 0x0A
@ -300,13 +357,15 @@ void onEvent (ev_t ev) {
#define FIELD_TWO_FLOAT 0x15 #define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10 #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){ 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 // Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) { if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending")); 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). // 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). // 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. // 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 latitude = 123;
float longitude = 456; float longitude = 456;
latitude = 44.558308; latitude = 44.558308;
longitude = -123.28341; longitude = -123.28341;
memcpy(buffer+3, (void*)&latitude, 4); memcpy(buffer+3, (void*)&latitude, 4);
memcpy(buffer+8, (void*)&longitude, 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. // Next TX is scheduled after TX_COMPLETE event.
} }
void setup() { void setup() {
delay(5000); delay(1000);
Serial.begin(9600); Serial.begin(9600);
ss.begin(4800); ss.begin(4800);
while (! Serial);
delay(100); delay(100);
Serial.println(F("Starting")); Serial.println(F("Starting"));
pinMode(LED_BUILTIN, OUTPUT); 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 // LMIC init
os_init(); os_init();
// Reset the MAC state. Session and pending data transfers will be discarded. // 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 // Set data rate and transmit power for uplink
LMIC_setDrTxpow(DR_SF7,14); LMIC_setDrTxpow(DR_SF7,14);
delay(100);
// Start job // Start job
//do_send(&sendjob); // do_send(&sendjob);
} }
/**************************************************** /****************************************************
@ -399,10 +446,10 @@ void setup() {
***************************************************/ ***************************************************/
int read_gps() { int read_gps() {
int ret = 0; int ret = 0;
while(ss.available()>0) { // while(ss.available()>0) {
gps.encode(ss.read()); // gps.encode(ss.read());
ret = 1; // ret = 1;
} // }
return ret; return ret;
} }
/**************************************************** /****************************************************
@ -429,11 +476,11 @@ enum STATE_ {
WAITING_LORA, WAITING_LORA,
LORA_DONE LORA_DONE
}; };
// int state = START_GPS;
int state = SENDING_LORA; int state = SENDING_LORA;
int loopCounter = 0; int loopCounter = 0;
int startTime = 0; int startTime = 0;
void loop() { void loop() {
if(state == START_GPS) { if(state == START_GPS) {
@ -447,10 +494,10 @@ void loop() {
* loading bar animation * loading bar animation
***************************************************/ ***************************************************/
if(got_data) { if(got_data) {
if(loopCounter%100==0) //if(loopCounter%100==0)
Serial.write('.'); // Serial.write('.');
if(loopCounter>PROGRESS_BAR_COUNT*100) { if(loopCounter>PROGRESS_BAR_COUNT*100) {
clear_line(); // clear_line();
loopCounter=0; loopCounter=0;
state = VERIFYING_GPS; state = VERIFYING_GPS;
@ -478,68 +525,36 @@ void loop() {
state = WAITING_GPS; state = WAITING_GPS;
} }
else if(state == SENDING_LORA) { 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 * send encoded buffer over LoRaWAN
***************************************************/ ***************************************************/
Serial.println("Sending LoRa Data..."); Serial.println("Sending LoRa Data...");
do_send(&sendjob); 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 * set reference time for LoRaWAN transmission delay
***************************************************/ ***************************************************/
startTime = millis(); startTime = millis() / 1000;
state = WAITING_LORA; state = WAITING_LORA;
} }
else if(state == WAITING_LORA) { else if(state == WAITING_LORA) {
/**************************************************** /****************************************************
* don't block the GPS from reading here * don't block the GPS from reading here
***************************************************/ ***************************************************/
read_gps(); // read_gps();
/**************************************************** /****************************************************
* if enough seconds have been delayed then move to * if enough seconds have been delayed then move to
* next state * next state
***************************************************/ ***************************************************/
if(millis()/1000 - startTime >= TX_INTERVAL) { if((millis()/1000 - startTime) >= TX_INTERVAL) {
Serial.println("Lora has finished waiting");
state = LORA_DONE; state = LORA_DONE;
} }
} }
else if(state == LORA_DONE) { else if(state == LORA_DONE) {
state = VERIFYING_GPS; state = VERIFYING_GPS;
state = SENDING_LORA;
} }
os_runloop_once();
//os_runloop_once();
} }