LoRaReceiverCallback.ino 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <M5Stack.h>
  2. #include <M5LoRa.h>
  3. void header(const char *string, uint16_t color){
  4. M5.Lcd.fillScreen(color);
  5. M5.Lcd.setTextSize(1);
  6. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLUE);
  7. M5.Lcd.fillRect(0, 0, 320, 30, TFT_BLUE);
  8. M5.Lcd.setTextDatum(TC_DATUM);
  9. M5.Lcd.drawString(string, 160, 3, 4);
  10. }
  11. void setup() {
  12. M5.begin();
  13. M5.Power.begin();
  14. // initialize serial
  15. header("LoRa Receiver Callback", TFT_BLACK);
  16. M5.Lcd.setTextFont(2);
  17. M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
  18. M5.Lcd.drawString("Please use serial port to view data.",0, 35, 2);
  19. Serial.println("LoRa Receiver Callback");
  20. // frequency in Hz (433E6, 866E6, 915E6)
  21. if (!LoRa.begin(433E6)) {
  22. Serial.println("Starting LoRa failed!");
  23. while (1);
  24. }
  25. // register the receive callback
  26. LoRa.onReceive(onReceive);
  27. // put the radio into receive mode
  28. LoRa.receive();
  29. }
  30. void loop() {
  31. // do nothing
  32. LoRa.receive();
  33. }
  34. void onReceive(int packetSize) {
  35. // received a packet
  36. Serial.print("Received packet '");
  37. // read packet
  38. for (int i = 0; i < packetSize; i++) {
  39. Serial.print((char)LoRa.read());
  40. }
  41. // print RSSI of packet
  42. Serial.print("' with RSSI ");
  43. Serial.println(LoRa.packetRssi());
  44. }