LoRaDuplex.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. LoRa Duplex communication
  3. Sends a message every half second, and polls continually
  4. for new incoming messages. Implements a one-byte addressing scheme,
  5. with 0xFF as the broadcast address.
  6. Uses readString() from Stream class to read payload. The Stream class'
  7. timeout may affect other functuons, like the radio's callback. For an
  8. created 28 April 2017
  9. by Tom Igoe
  10. */
  11. #include <M5Stack.h>
  12. #include <M5LoRa.h>
  13. String outgoing; // outgoing message
  14. byte msgCount = 0; // count of outgoing messages
  15. // byte localAddress = 0xFF; // address of this device
  16. // byte destination = 0xBB; // destination to send to
  17. byte localAddress = 0xBB; // address of this device
  18. byte destination = 0xFF; // destination to send to
  19. long lastSendTime = 0; // last send time
  20. int interval = 2000; // interval between sends
  21. void header(const char *string, uint16_t color){
  22. M5.Lcd.fillScreen(color);
  23. M5.Lcd.setTextSize(1);
  24. M5.Lcd.setTextColor(TFT_MAGENTA, TFT_BLUE);
  25. M5.Lcd.fillRect(0, 0, 320, 30, TFT_BLUE);
  26. M5.Lcd.setTextDatum(TC_DATUM);
  27. M5.Lcd.drawString(string, 160, 3, 4);
  28. }
  29. void setup() {
  30. M5.begin();
  31. M5.Power.begin();
  32. while (!Serial);
  33. header("LoRa Duplex", TFT_BLACK);
  34. M5.Lcd.setTextFont(2);
  35. M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
  36. M5.Lcd.drawString("Please use serial port to view data.",0, 80, 2);
  37. Serial.println("LoRa Duplex");
  38. // override the default CS, reset, and IRQ pins (optional)
  39. LoRa.setPins();// set CS, reset, IRQ pin
  40. if (!LoRa.begin(433E6)) { // initialize ratio at 915 MHz
  41. Serial.println("LoRa init failed. Check your connections.");
  42. while (true); // if failed, do nothing
  43. }
  44. Serial.println("LoRa init succeeded.");
  45. }
  46. void loop() {
  47. if (millis() - lastSendTime > interval) {
  48. String message = "HeLoRa World!"; // send a message
  49. sendMessage(message);
  50. Serial.println("Sending " + message);
  51. lastSendTime = millis(); // timestamp the message
  52. interval = random(2000) + 1000; // 2-3 seconds
  53. }
  54. // parse for a packet, and call onReceive with the result:
  55. onReceive(LoRa.parsePacket());
  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 = "";
  75. while (LoRa.available()) {
  76. incoming += (char)LoRa.read();
  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. }