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,7 +87,7 @@ 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 = {
@ -167,36 +167,93 @@ 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){
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);
}
}
#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;
}
/*
* 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());
@ -236,19 +293,19 @@ void onEvent (ev_t ev) {
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"));
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"));
@ -300,13 +357,15 @@ void onEvent (ev_t ev) {
#define FIELD_TWO_FLOAT 0x15
#define FIELD_TWO_VARIANT 0x10
uint8_t buffer[15] = {
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};
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"));
@ -316,14 +375,6 @@ void do_send(osjob_t* j){
// 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;
@ -333,26 +384,21 @@ void do_send(osjob_t* j){
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();
}