LoRaDuplexCallback.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. LoRa Duplex communication wth callback
  3. Sends a message every half second, and uses callback
  4. for new incoming messages. Implements a one-byte addressing scheme,
  5. with 0xFF as the broadcast address.
  6. Note: while sending, LoRa radio is not listening for incoming messages.
  7. Note2: when using the callback method, you can't use any of the Stream
  8. functions that rely on the timeout, such as readString, parseInt(), etc.
  9. created 28 April 2017
  10. by Tom Igoe
  11. */
  12. #include <M5Stack.h>
  13. #include <M5LoRa.h>
  14. String outgoing; // outgoing message
  15. byte msgCount = 0; // count of outgoing messages
  16. byte localAddress = 0xBB; // address of this device
  17. byte destination = 0xFF; // destination to send to
  18. long lastSendTime = 0; // last send time
  19. int interval = 2000; // interval between sends
  20. void header(const char *string, uint16_t color){
  21. M5.Lcd.fillScreen(color);
  22. M5.Lcd.setTextSize(1);
  23. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLUE);
  24. M5.Lcd.fillRect(0, 0, 320, 30, TFT_BLUE);
  25. M5.Lcd.setTextDatum(TC_DATUM);
  26. M5.Lcd.drawString(string, 160, 3, 4);
  27. }
  28. void setup() {
  29. M5.begin(); // initialize serial
  30. M5.Power.begin();
  31. while (!Serial);
  32. header("LoRa Duplex with callback", TFT_BLACK);
  33. M5.Lcd.setTextFont(2);
  34. M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
  35. M5.Lcd.drawString("Please use serial port to view data.",0, 35, 2);
  36. Serial.println("LoRa Duplex with callback");
  37. // override the default CS, reset, and IRQ pins (optional)
  38. LoRa.setPins();// set CS, reset, IRQ pin
  39. if (!LoRa.begin(433E6)) { // initialize ratio at 915 MHz
  40. Serial.println("LoRa init failed. Check your connections.");
  41. while (true); // if failed, do nothing
  42. }
  43. LoRa.onReceive(onReceive);
  44. LoRa.receive();
  45. Serial.println("LoRa init succeeded.");
  46. }
  47. void loop() {
  48. if (millis() - lastSendTime > interval) {
  49. String message = "HeLoRa World!"; // send a message
  50. sendMessage(message);
  51. Serial.println("Sending " + message);
  52. lastSendTime = millis(); // timestamp the message
  53. interval = random(2000) + 1000; // 2-3 seconds
  54. LoRa.receive(); // go back into receive mode
  55. }
  56. }
  57. void sendMessage(String outgoing) {
  58. LoRa.beginPacket(); // start packet
  59. LoRa.write(destination); // add destination address
  60. LoRa.write(localAddress); // add sender address
  61. LoRa.write(msgCount); // add message ID
  62. LoRa.write(outgoing.length()); // add payload length
  63. LoRa.print(outgoing); // add payload
  64. LoRa.endPacket(); // finish packet and send it
  65. msgCount++; // increment message ID
  66. }
  67. void onReceive(int packetSize) {
  68. if (packetSize == 0) return; // if there's no packet, return
  69. // read packet header bytes:
  70. int recipient = LoRa.read(); // recipient address
  71. byte sender = LoRa.read(); // sender address
  72. byte incomingMsgId = LoRa.read(); // incoming msg ID
  73. byte incomingLength = LoRa.read(); // incoming msg length
  74. String incoming = ""; // payload of packet
  75. while (LoRa.available()) { // can't use readString() in callback, so
  76. incoming += (char)LoRa.read(); // add bytes one by one
  77. }
  78. if (incomingLength != incoming.length()) { // check length for error
  79. Serial.println("error: message length does not match length");
  80. return; // skip rest of function
  81. }
  82. // if the recipient isn't this device or broadcast,
  83. if (recipient != localAddress && recipient != 0xFF) {
  84. Serial.println("This message is not for me.");
  85. return; // skip rest of function
  86. }
  87. // if message is for this device, or broadcast, print details:
  88. Serial.println("Received from: 0x" + String(sender, HEX));
  89. Serial.println("Sent to: 0x" + String(recipient, HEX));
  90. Serial.println("Message ID: " + String(incomingMsgId));
  91. Serial.println("Message length: " + String(incomingLength));
  92. Serial.println("Message: " + incoming);
  93. Serial.println("RSSI: " + String(LoRa.packetRssi()));
  94. Serial.println("Snr: " + String(LoRa.packetSnr()));
  95. Serial.println();
  96. }