Advertisement
hwthinker

LoraReceiverCallbackOLED-433Mhz

Feb 1st, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //modified lora library by Sandeep Mistry for TTGO ESP32 Lora
  2. // lora receiverCallBack
  3.  
  4. #include <SPI.h>
  5. #include <LoRa.h>
  6. #include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
  7. #include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
  8.  
  9. #ifdef ARDUINO_SAMD_MKRWAN1300
  10. #error "This example is not compatible with the Arduino MKR WAN 1300 board!"
  11. #endif
  12.  
  13. // GPIO5  -- SX1278's SCK
  14. // GPIO19 -- SX1278's MISO
  15. // GPIO27 -- SX1278's MOSI
  16. // GPIO18 -- SX1278's CS
  17. // GPIO14 -- SX1278's RESET
  18. // GPIO26 -- SX1278's IRQ(Interrupt Request)
  19.  
  20. #define SS      18
  21. #define RST     14
  22. #define DI0     26
  23. #define BAND    433E6
  24. // Initialize the OLED display using Wire library
  25. SSD1306Wire  display(0x3c, OLED_SDA, OLED_SCL); // OLED_SDA=4, OLED_SCL=15
  26.  
  27. int RxDataRSSI = 0;
  28. char Str1[15];
  29.  
  30. void setup() {
  31.   // START aktivas Oled
  32.   pinMode(LED_BUILTIN, OUTPUT);
  33.   pinMode(16, OUTPUT);
  34.   digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  35.   delay(50);
  36.   digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high、
  37.  
  38.   // Initialising the UI will init the display too.
  39.   display.init();
  40.   display.flipScreenVertically();
  41.   display.setFont(ArialMT_Plain_10);
  42.   // clear the display
  43.   display.clear();
  44.   // aktivasi Oled END
  45.  
  46.   Serial.begin(115200);
  47.   while (!Serial);
  48.   SPI.begin(5, 19, 27, 18);
  49.   LoRa.setPins(SS, RST, DI0);
  50.  
  51.   Serial.println("LoRa Receiver Callback");
  52.  
  53.   if (!LoRa.begin(BAND)) {
  54.     Serial.println("Starting LoRa failed!");
  55.     while (1);
  56.   }
  57.  
  58.   // register the receive callback
  59.   LoRa.onReceive(onReceive);
  60.  
  61.   // put the radio into receive mode
  62.   LoRa.receive();
  63.   display.setFont(ArialMT_Plain_24);
  64.   display.drawString(0, 0, "HwThinker");
  65.   display.display();
  66.   delay(1000);
  67.   display.clear();
  68. }
  69.  
  70. void loop() {
  71.   // do nothing
  72.   display.setFont(ArialMT_Plain_16);
  73.   display.drawString(0, 0, "Lora Receiver");
  74.  
  75.   display.setFont(ArialMT_Plain_10);
  76.   display.drawString(0, 26, "rx Data:" + String(Str1));
  77.  
  78.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  79.   display.setFont(ArialMT_Plain_10);
  80.   display.drawString(0, 45, "RSSI : " + String(LoRa.packetRssi()));
  81.   display.display();
  82.   delay(2000);
  83.   display.clear();
  84. }
  85.  
  86. void onReceive(int packetSize) {
  87.   // received a packet
  88.   Serial.print("Received packet '");
  89.  
  90.   // read packet
  91.   for (int i = 0; i < packetSize; i++) {
  92.     Str1[i] = (char)LoRa.read();
  93.   }
  94.   Serial.print(Str1);
  95.  
  96.   // print RSSI of packet
  97.   Serial.print("' with RSSI ");
  98.   RxDataRSSI = LoRa.packetRssi();
  99.   Serial.println(RxDataRSSI);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement