Advertisement
hwthinker

LoraReceiverCallbackOLED-915MHz

Feb 3rd, 2019
511
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 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     14   // GPIO14/12 -- 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 4
  24. #define OLED_SCL 15
  25. #define OLED_RST 16
  26.  
  27. #define LED_BUILTIN 2
  28.  
  29.  
  30.  
  31. // Initialize the OLED display using Wire library
  32. SSD1306Wire  display(0x3c, OLED_SDA, OLED_SCL); // OLED_SDA=4, OLED_SCL=15
  33.  
  34. int RxDataRSSI = 0;
  35. char Str1[15];
  36.  
  37. void setup() {
  38.   // START aktivas Oled
  39.   pinMode(LED_BUILTIN, OUTPUT);
  40.   pinMode(OLED_RST , OUTPUT);
  41.   digitalWrite(OLED_RST , LOW);    // set GPIO16 low to reset OLED
  42.   delay(50);
  43.   digitalWrite(OLED_RST , HIGH); // while OLED is running, must set GPIO16 in high、
  44.  
  45.   // Initialising the UI will init the display too.
  46.   display.init();
  47.   display.flipScreenVertically();
  48.   display.setFont(ArialMT_Plain_10);
  49.   // clear the display
  50.   display.clear();
  51.   // aktivasi Oled END
  52.  
  53.   Serial.begin(9600);
  54.   while (!Serial);
  55.   SPI.begin(SCK, MISO, MOSI, SS);
  56.   LoRa.setPins(SS, RST, DI0);
  57.  
  58.   Serial.println("LoRa Receiver Callback");
  59.  
  60.   if (!LoRa.begin(BAND)) {
  61.     Serial.println("Starting LoRa failed!");
  62.     while (1);
  63.   }
  64.  
  65.   // register the receive callback
  66.   LoRa.onReceive(onReceive);
  67.  
  68.   // put the radio into receive mode
  69.   LoRa.receive();
  70.   display.setFont(ArialMT_Plain_24);
  71.   display.drawString(0, 0, "HwThinker");
  72.   display.display();
  73.   delay(1000);
  74.   display.clear();
  75. }
  76.  
  77. void loop() {
  78.   // do nothing
  79.   display.setFont(ArialMT_Plain_16);
  80.   display.drawString(0, 0, "Lora Receiver");
  81.  
  82.   display.setFont(ArialMT_Plain_10);
  83.   display.drawString(0, 26, "rx Data:" + String(Str1));
  84.  
  85.   display.setTextAlignment(TEXT_ALIGN_LEFT);
  86.   display.setFont(ArialMT_Plain_10);
  87.   display.drawString(0, 45, "RSSI : " + String(LoRa.packetRssi()));
  88.   display.display();
  89. }
  90.  
  91. void onReceive(int packetSize) {
  92.   // received a packet
  93.   Serial.print("Received packet '");
  94.   memset(Str1, 0, sizeof(Str1));
  95.   // read packet
  96.   for (int i = 0; i < packetSize; i++) {
  97.     Str1[i] = (char)LoRa.read();
  98.   }
  99.   Serial.print(Str1);
  100.  
  101.   // print RSSI of packet
  102.   Serial.print("' with RSSI ");
  103.   RxDataRSSI = LoRa.packetRssi();
  104.   Serial.println(RxDataRSSI);
  105.     //  digitalWrite(LED_BUILTIN, (state) ? HIGH : LOW);
  106.   //  state = !state;
  107.   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  108.   display.clear();
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement