hwthinker

LoraReceiver_ESP32-915MHZ

Aug 28th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // lora receiverCallBack modified by HwThinker for for TTGO ESP32 Lora 915MHz
  2.  
  3. #include <SPI.h>
  4. #include <LoRa.h>
  5.  
  6. #define SCK 5 // GPIO5 -- lora SCK
  7. #define MISO 19 // GPIO19 -- lora MISO
  8. #define MOSI 27 // GPIO27 -- lora MOSI
  9. #define SS 18 // GPIO18 -- lora CS
  10. #define RST 14 // GPIO14 -- RESET (If Lora does not work, replace it with GPIO14)
  11. #define DI0 26 // GPIO26 -- IRQ(Interrupt Request)
  12.  
  13. #define BAND 915E6
  14.  
  15. #define LED_BUILTIN 2
  16. int state = 0;
  17.  
  18. void setup() {
  19. pinMode(LED_BUILTIN, OUTPUT);
  20. Serial.begin(115200);
  21. while (!Serial);
  22. SPI.begin(SCK, MISO, MOSI, SS);
  23. LoRa.setPins(SS,RST,DI0);
  24.  
  25. Serial.println("LoRa Receiver Callback");
  26.  
  27. if (!LoRa.begin(BAND)) {
  28. Serial.println("Starting LoRa failed!");
  29. while (1);
  30. }
  31.  
  32. Serial.println("LoRa Initial OK! Blinky 1.5 second");
  33. for (int i=0;i<15;i++) {
  34. digitalWrite(LED_BUILTIN, digitalRead(LED_BUILTIN) ? LOW : HIGH);
  35. delay(100);
  36. }
  37.  
  38. // register the receive callback
  39. LoRa.onReceive(onReceive);
  40.  
  41. // put the radio into receive mode
  42. LoRa.receive();
  43. }
  44.  
  45. void loop() {
  46. // do nothing
  47. }
  48.  
  49. void onReceive(int packetSize) {
  50. // received a packet
  51. Serial.print("Received packet '");
  52.  
  53. // read packet
  54. for (int i = 0; i < packetSize; i++) {
  55. Serial.print((char)LoRa.read());
  56. }
  57.  
  58. // print RSSI of packet
  59. Serial.print("' with RSSI ");
  60. Serial.println(LoRa.packetRssi());
  61. digitalWrite(LED_BUILTIN, (state) ? HIGH : LOW);
  62. state = !state;
  63. //or digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  64. }
Add Comment
Please, Sign In to add comment