From a1d0404a47b2ac9bf6b3f6897dd4c16b5fe38e15 Mon Sep 17 00:00:00 2001 From: sessionm21 Date: Fri, 15 May 2020 07:19:37 +0100 Subject: [PATCH] receive does not work --- collar.cpp | 413 +++++++++++++++++++++++++++-------------------------- 1 file changed, 214 insertions(+), 199 deletions(-) diff --git a/collar.cpp b/collar.cpp index f844268..00b60ad 100644 --- a/collar.cpp +++ b/collar.cpp @@ -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;i0) { - 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= 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(); }