Using the TTGO ESP32 LoRa Oled board. The software hangs randomly, but more often/sooner if there is a second LoRa transmitter nearby. The unit using the code below is set for SF10, the second interfering unit is set to SF9. I suspect the glitch might be in the LoRa.parsePacket() method, but don't have any way to substantiate this.
// ESP32_receiver
char this_file[] = "ESP32_receiver" ;
char descp[] = "ESP32 TTGO LoRa" ;
char ver[] = "ver 1.2 12/22/22 " ;
char ver2[] = "no inputs, SF10" ;
char ver3[] = " " ;
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 915E6
//OLED pins
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);
byte t1L ;
byte t1H ;
byte t2L ;
byte t2H ;
byte counter ;
byte sync_b ;
int T_1 ;
int T_2 ;
String LoRaData;
unsigned long loop_Time ;
// ===================================================================
void setup() {
//initialize Serial Monitor
Serial.begin(9600);
//reset OLED display via software
pinMode(OLED_RST, OUTPUT);
digitalWrite(OLED_RST, LOW);
delay(20);
digitalWrite(OLED_RST, HIGH);
//initialize OLED
Wire.begin(OLED_SDA, OLED_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false, false)) {
// Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// ----------------version message ------------------------
Serial.println(this_file) ; //file name
Serial.println(descp) ; //description
Serial.println(ver) ; //version and date
Serial.println(ver2) ; //version and date
Serial.println(ver3) ; //version and date
Serial.println() ;
// ------------ end of version message --------------------
// show version on OLED display
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print(this_file);
display.setCursor(0,20);
display.print(descp);
display.setCursor(0,30);
display.print(ver);
display.setCursor(0,40);
display.print(ver2);
display.display();
delay(5000) ; // reading time
//SPI LoRa pins
SPI.begin(SCK, MISO, MOSI, SS);
//setup LoRa transceiver module
LoRa.setPins(SS, RST, DIO0);
if (!LoRa.begin(BAND)) {
Serial.println("Starting LoRa failed!");
while (1); // stop phere
}
display.clearDisplay();
Serial.println("LoRa Initializing OK!");
display.setCursor(0,10);
display.println("LoRa Initializing OK!");
display.display();
loop_Time = millis() ;
pinMode(2, OUTPUT) ;
LoRa.setSpreadingFactor(10);
}
// ===========================================================
void loop() {
// flash the onboard LED every 5 seconds
if((millis() - loop_Time) > 5000) {
digitalWrite(2, 1) ;
delay(200) ;
digitalWrite(2, 0) ;
loop_Time = millis() ;
}
// parse packet
int packetSize = LoRa.parsePacket();
if (packetSize == 6) {
Serial.print("Received packet ");
//read packet
sync_b = LoRa.read(); //
if(sync_b == 0x55) { // user flag is good
counter = LoRa.read();
t1L = LoRa.read();
t1H = LoRa.read();
t2L = LoRa.read();
t2H = LoRa.read();
T_1 = t1H<<8 | t1L ;
T_2 = t2H<<8 | t2L ;
// fix the 4-byte integer sign bit problem
if(T_1 > 20000) T_1 = T_1 - 65536 ;
if(T_2 > 20000) T_2 = T_2 - 65536 ;
//print RSSI of packet
int rssi = LoRa.packetRssi();
Serial.print(" with RSSI ");
Serial.println(rssi);
if((T_1<12000) && (T_2<12000)) { // Less than +120
if((T_1>-3000) && (T_2>-3000)) { // > -30
// Display the packet message
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("LORA Receiver: ");
display.println(counter) ;
display.setTextSize(3);
display.setCursor(0,15);
display.print(" ") ;
if(!(T_1<0)) {
if(T_1<10000) display.print(" ") ;
if(T_1<1000) display.print(" ") ;
} else {
if(abs(T_1)<1000) display.print(" ") ;
}
display.print(T_1/100.0, 1);
display.setCursor(0,40);
display.print(" ") ;
if(!(T_2<0)) {
if(T_2<10000) display.print(" ") ;
if(T_2<1000) display.print(" ") ;
} else {
if(abs(T_2)<1000) display.print(" ") ;
}
display.print(T_2/100.0, 1);
display.display();
}
}
}
// garbled packet: dump any remaing bytes
while (LoRa.available()) {
sync_b = LoRa.read(); //
}
} else {
// packet error: dump the packet
while (LoRa.available()) {
sync_b = LoRa.read(); //
}
}
}
Anyone else experiencing this kind of code freeze?
Don't go nuts over the
if(T_1 > 20000) T_1 = T_1 - 65536 ;
line. That's a kludge fix for not extending the sign bit in the 4-byte integer used in the ESP32 and despite considerable hazing over this, it works perfectly.