Advertisement
hwthinker

Lora32 Receiver Callback OLED-915_Mhz_V2.1-1.6

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