sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.64k stars 627 forks source link

Issue with LoRa.read() #222

Open keithr0 opened 5 years ago

keithr0 commented 5 years ago

I wrote a small app in order to test the range of the TTGO ESP32/SX1278/SSD1306 boards. The transmit side is straightforward and sends packets with the string "Block" followed by a number that increments from 0 to 999. the receiver is interrupt driven (code below). The problem was that after startup, the correct block number was displayed but all subsequent packets were displayed with the same block number. To fix it I had to comment out the following line from handleDio0Rise() in LoRa.cpp at line 484

writeRegister(REG_FIFO_ADDR_PTR, 0);

After that the correct packet was displayed.

Code:

#include <SPI.h>
#include <LoRa.h>
#include "SSD1306.h"

//****** SX1278 pins to ESP32 GPIOs ******//
#define SCK   5
#define MISO  19
#define MOSI  27
#define CS    18
#define RESET 14
#define IRQ   26 //(Interrupt Request)

#define BAND  915E6 // Set LORA band to 915MHz

//****** OLED pins to ESP32 GPIOs ******//
#define OLED_SDA 4  // GPIO4
#define OLED_SCL 15 // GPIO15
#define OLED_RST 16 // GPIO16

SSD1306  display(0x3c, OLED_SDA, OLED_SCL);

volatile int MessageLength = 0;

void PrintMessage(String Message)
{
  display.clear();
  display.println(Message);
  display.drawLogBuffer(0, 0); // Draw it to the internal screen buffer
  display.display();           // Display it on the screen
}

void setup() 
{
//****** Set up OLED display ******//
  pinMode(OLED_RST,OUTPUT);
  digitalWrite(OLED_RST, LOW);    // set GPIO16 low to reset OLED
  delay(50); 
  digitalWrite(OLED_RST, HIGH);   // while OLED is running, must set GPIO16 in high
  display.init();
  display.setContrast(255);
  display.setLogBuffer(8, 30);    // Initialise OLED buffer to 30x8 char
  PrintMessage("LoRa Range Test starting");

//****** Set up LORA chip ******//
  SPI.begin(SCK, MISO, MOSI, CS);
  LoRa.setPins(CS, RESET, IRQ);
  if (!LoRa.begin(BAND)) 
  {
    PrintMessage(" ");
    PrintMessage(" ");
    PrintMessage("LoRa failed to start!");
    while (1);
  }
  LoRa.onReceive(onReceive); // register the receive callback
  LoRa.receive();            // put the radio into receive mode
}

void loop() 
{
  if(MessageLength > 0)
  {
    HandleInterrupt(MessageLength);
    MessageLength = 0;
  }
}

void onReceive(int packetSize) // received a packet
{   
  MessageLength = packetSize;
}

void HandleInterrupt(int packetSize)
{
  String Message = "Rcved: ";
  for (int i = 0; i < packetSize; i++) Message = Message + (char)LoRa.read();
  Message = Message + " RSSI: " + LoRa.packetRssi(); // print RSSI of packet
  PrintMessage(Message);
}
keithr0 commented 5 years ago

This more or less a dupe of #218. Sorry about that.