// 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 #include #include #include #include "Base64.h" #include "gateway/message.pb.h" #include "pb_common.h" #include "pb.h" #include "pb_encode.h" #include "pb_decode.h" #include "gateway/message.pb.h" // Visit your thethingsnetwork.org device console // to create an account, or if you need your session keys. // Network Session Key (MSB) uint8_t NwkSkey[16] = {0x52, 0x92, 0xC0, 0x72, 0x2D, 0x3C, 0x55, 0x5E, 0xE4, 0xB9, 0x9E, 0x9B, 0x88, 0x66, 0x47, 0xF1}; // Application Session Key (MSB) uint8_t AppSkey[16] = {0xC4, 0x30, 0xEF, 0x56, 0x4F, 0x6D, 0xA2, 0x56, 0x1F, 0x15, 0x2F, 0xB8, 0x62, 0xC7, 0xCA, 0xC2}; // Device Address (MSB) uint8_t DevAddr[4] = {0x26, 0x02, 0x12, 0xB6}; /************************** Example Begins Here ***********************************/ // Data Packet to Send to TTN unsigned char loraData[50] = {"hello LoRa"}; // How many times data transfer should occur, in seconds // const unsigned int sendInterval = 30; const unsigned int sendInterval = 60; // Pinout for Adafruit Feather 32u4 LoRa TinyLoRa lora = TinyLoRa(2, 10, 9); TinyGPSPlus gps; SoftwareSerial ss(6, 7); // Pinout for Adafruit Feather M0 LoRa //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, check that coordinate is within polygon boundary 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)); int status = pb_encode(&stream, Fenceless_Coordinates_fields, &m); if(!status){ Serial.println("Failed to encode"); } import_protobuf(buffer, sizeof(buffer)); } int recieved = 0; void on_recieve(int n) { recieved = 1; } void setup() { delay(1000); // start uart Serial.begin(9600); // start software uart for GPS ss.begin(4800); while (! Serial); delay(1000); // Initialize pin LED_BUILTIN as an output pinMode(LED_BUILTIN, OUTPUT); // Initialize GPS - GPS does not work indoors - This will block until GPS available // Serial.println("Starting GPS"); // while(!gps.location.isValid()) { // while(ss.available()>0) { // gps.encode(ss.read()); // } // if (millis() > 5000 && gps.charsProcessed() < 10) // { // Serial.println(F("No GPS detected: check wiring.")); // while(true); // } // } // Initialize LoRa Serial.print("Starting LoRa..."); // define multi-channel sending lora.setChannel(MULTI); // set datarate lora.setDatarate(SF7BW125); if(!lora.begin()) { Serial.println("Failed"); Serial.println("Check your radio"); while(true); } // 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); // start 1 second timer TCCR1A = 0; TCCR1B = 0; TCNT1 = 34286; // preload timer TCCR1B |= (1 << CS12); // 256 prescaler TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt Serial.println("OK"); } // TIMER1_OVF_vect is called once per second int sendCounter = 0; ISR(TIMER1_OVF_vect) { digitalWrite(LED_BUILTIN, sendCounter%2); if(sendCounter==0 && gps.location.isValid()) { Serial.println("Valid gps"); // reset send counter sendCounter = sendInterval; Fenceless_CollarResponse coord; coord.loc.x = gps.location.lat(); coord.loc.y = gps.location.lng(); pb_ostream_t stream; stream = pb_ostream_from_buffer(loraData, sizeof(loraData)); int err = pb_encode(&stream, Fenceless_CollarResponse_fields, &coord); // Generate copy pasteable base64 // char base64[50]; // base64_encode(base64, (char*)loraData, stream.bytes_written); // Serial.println(stream.bytes_written); // Serial.println(base64); Serial.println("Sending LoRa Data..."); lora.sendData(loraData, stream.bytes_written, lora.frameCounter); // Optionally set the Frame Port (1 to 255) // uint8_t framePort = 1; // lora.sendData(loraData, sizeof(loraData), lora.frameCounter, framePort); Serial.print("Frame Counter: ");Serial.println(lora.frameCounter); lora.frameCounter++; } else if (!gps.location.isValid()) { Serial.println("waiting for gps"); } else { Serial.println("delaying til next frame counter"); } sendCounter--; } void loop() { // For later when recieving from gateway is implemented // if(recieved) { // Serial.println("Recieved something"); // } else { // Serial.println("Nothing yet"); // } if(ss.available()>0) gps.encode(ss.read()); delay(1000); }