LoRa868Duplex.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Description: LoRa868 Duplex communication.Send messages regularly "HeLoRa World!"
  3. */
  4. #include <M5Stack.h>
  5. #include <M5LoRa.h>
  6. String outgoing; // outgoing message
  7. byte msgCount = 0; // count of outgoing messages
  8. byte localAddress = 0xFF; // address of this device
  9. byte destination = 0xBB; // destination to send to
  10. long lastSendTime = 0; // last send time
  11. int interval = 1000; // interval between sends
  12. void setup() {
  13. M5.begin();
  14. M5.Power.begin();
  15. while (!Serial);
  16. Serial.println("LoRa Duplex B");
  17. // override the default CS, reset, and IRQ pins (optional)
  18. LoRa.setPins();// set CS, reset, IRQ pin
  19. if (!LoRa.begin(868E6)) { // initialize ratio at 868 MHz
  20. Serial.println("LoRa init failed. Check your connections.");
  21. while (true); // if failed, do nothing
  22. }
  23. Serial.println("LoRa init succeeded.");
  24. }
  25. void loop() {
  26. if (millis() - lastSendTime > interval) {
  27. String message = "HeLoRa World!"; // send a message
  28. sendMessage(message);
  29. Serial.println("Sending " + message);
  30. M5.Lcd.setTextColor(BLUE);
  31. M5.Lcd.println("Sending " + message);
  32. lastSendTime = millis(); // timestamp the message
  33. interval = random(1000) + 500;
  34. }
  35. // parse for a packet, and call onReceive with the result:
  36. onReceive(LoRa.parsePacket());
  37. if(M5.BtnA.wasPressed()){
  38. M5.Lcd.setCursor(0, 0);
  39. M5.Lcd.clear(BLACK);
  40. }
  41. if(M5.BtnB.wasPressed()){
  42. reinit();
  43. }
  44. M5.update();
  45. }
  46. void reinit(){
  47. Serial.println("LoRa Duplex Reinitialization");
  48. // override the default CS, reset, and IRQ pins (optional)
  49. LoRa.setPins();// set CS, reset, IRQ pin
  50. if (!LoRa.begin(868E6)) { // initialize ratio at 868 MHz
  51. Serial.println("LoRa init failed. Check your connections.");
  52. M5.Lcd.setCursor(0, 0);
  53. M5.Lcd.setTextColor(RED);
  54. M5.Lcd.println("Init failed!!!");
  55. while (true); // if failed, do nothing
  56. }
  57. Serial.println("LoRa init succeeded.");
  58. }
  59. void sendMessage(String outgoing) {
  60. LoRa.beginPacket(); // start packet
  61. LoRa.write(destination); // add destination address
  62. LoRa.write(localAddress); // add sender address
  63. LoRa.write(msgCount); // add message ID
  64. LoRa.write(outgoing.length()); // add payload length
  65. LoRa.print(outgoing); // add payload
  66. LoRa.endPacket(); // finish packet and send it
  67. msgCount++; // increment message ID
  68. }
  69. void onReceive(int packetSize) {
  70. if (packetSize == 0) return; // if there's no packet, return
  71. // read packet header bytes:
  72. int recipient = LoRa.read(); // recipient address
  73. byte sender = LoRa.read(); // sender address
  74. byte incomingMsgId = LoRa.read(); // incoming msg ID
  75. byte incomingLength = LoRa.read(); // incoming msg length
  76. String incoming = "";
  77. while (LoRa.available()) {
  78. incoming += (char)LoRa.read();
  79. }
  80. if (incomingLength != incoming.length()) { // check length for error
  81. Serial.println("error: message length does not match length");
  82. return; // skip rest of function
  83. }
  84. // if the recipient isn't this device or broadcast,
  85. if (recipient != localAddress && recipient != 0xFF) {
  86. Serial.println("This message is not for me.");
  87. return; // skip rest of function
  88. }
  89. // if message is for this device, or broadcast, print details:
  90. Serial.println("Received from: 0x" + String(sender, HEX));
  91. Serial.println("Sent to: 0x" + String(recipient, HEX));
  92. Serial.println("Message ID: " + String(incomingMsgId));
  93. Serial.println("Message length: " + String(incomingLength));
  94. Serial.println("Message: " + incoming);
  95. Serial.println("RSSI: " + String(LoRa.packetRssi()));
  96. Serial.println("Snr: " + String(LoRa.packetSnr()));
  97. Serial.println();
  98. M5.Lcd.setTextColor(YELLOW);
  99. M5.Lcd.println("Message: " + incoming);
  100. }