hwthinker icon

LoraReceiverCallbackOLED-433Mhz

hwthinker | PRO | 02/01/19 07:14:23 AM UTC | 0 ⭐ | 588 👁️ | Never ⏰ | []
Arduino |

2.53 KB

|

None

|

0 👍

/

0 👎

//modified lora library by Sandeep Mistry for TTGO ESP32 Lora
// lora receiverCallBack
 
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
 
#ifdef ARDUINO_SAMD_MKRWAN1300
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
#endif
 
// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)
 
#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6
// Initialize the OLED display using Wire library
SSD1306Wire  display(0x3c, OLED_SDA, OLED_SCL); // OLED_SDA=4, OLED_SCL=15
 
int RxDataRSSI = 0;
char Str1[15];
 
void setup() {
  // START aktivas Oled
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(16, OUTPUT);
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50);
  digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high、
 
  // Initialising the UI will init the display too.
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);
  // clear the display
  display.clear();
  // aktivasi Oled END
 
  Serial.begin(115200);
  while (!Serial);
  SPI.begin(5, 19, 27, 18);
  LoRa.setPins(SS, RST, DI0);
 
  Serial.println("LoRa Receiver Callback");
 
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
 
  // register the receive callback
  LoRa.onReceive(onReceive);
 
  // put the radio into receive mode
  LoRa.receive();
  display.setFont(ArialMT_Plain_24);
  display.drawString(0, 0, "HwThinker");
  display.display();
  delay(1000);
  display.clear();
}
 
void loop() {
  // do nothing
  display.setFont(ArialMT_Plain_16);
  display.drawString(0, 0, "Lora Receiver");
 
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 26, "rx Data:" + String(Str1));
 
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  display.drawString(0, 45, "RSSI : " + String(LoRa.packetRssi()));
  display.display();
  delay(2000);
  display.clear();
}
 
void onReceive(int packetSize) {
  // received a packet
  Serial.print("Received packet '");
 
  // read packet
  for (int i = 0; i < packetSize; i++) {
    Str1[i] = (char)LoRa.read();
  }
  Serial.print(Str1);
 
  // print RSSI of packet
  Serial.print("' with RSSI ");
  RxDataRSSI = LoRa.packetRssi();
  Serial.println(RxDataRSSI);
}

Comments