LoRaSetSpread.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. LoRa Duplex communication with Spreading Factor
  3. Sends a message every half second, and polls continually
  4. for new incoming messages. Sets the LoRa radio's spreading factor.
  5. Spreading factor affects how far apart the radio's transmissions
  6. are, across the available bandwidth. Radios with different spreading
  7. factors will not receive each other's transmissions. This is one way you
  8. can filter out radios you want to ignore, without making an addressing scheme.
  9. Spreading factor affects reliability of transmission at high rates, however,
  10. so avoid a hugh spreading factor when you're sending continually.
  11. See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
  12. for more on Spreading Factor.
  13. created 28 April 2017
  14. by Tom Igoe
  15. */
  16. #include <M5Stack.h>
  17. #include <M5LoRa.h>
  18. byte msgCount = 0; // count of outgoing messages
  19. int interval = 2000; // interval between sends
  20. long lastSendTime = 0; // time of last packet send
  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(); // initialize serial
  31. M5.Power.begin();
  32. while (!Serial);
  33. header("LoRa Set spreading factor", 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, 35, 2);
  37. Serial.println("LoRa Duplex - Set spreading factor");
  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. LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
  45. Serial.println("LoRa init succeeded.");
  46. }
  47. void loop() {
  48. if (millis() - lastSendTime > interval) {
  49. String message = "HeLoRa World! "; // send a message
  50. message += msgCount;
  51. sendMessage(message);
  52. Serial.println("Sending " + message);
  53. lastSendTime = millis(); // timestamp the message
  54. interval = random(2000) + 1000; // 2-3 seconds
  55. msgCount++;
  56. }
  57. // parse for a packet, and call onReceive with the result:
  58. onReceive(LoRa.parsePacket());
  59. }
  60. void sendMessage(String outgoing) {
  61. LoRa.beginPacket(); // start packet
  62. LoRa.print(outgoing); // add payload
  63. LoRa.endPacket(); // finish packet and send it
  64. msgCount++; // increment message ID
  65. }
  66. void onReceive(int packetSize) {
  67. if (packetSize == 0) return; // if there's no packet, return
  68. // read packet header bytes:
  69. String incoming = "";
  70. while (LoRa.available()) {
  71. incoming += (char)LoRa.read();
  72. }
  73. Serial.println("Message: " + incoming);
  74. Serial.println("RSSI: " + String(LoRa.packetRssi()));
  75. Serial.println("Snr: " + String(LoRa.packetSnr()));
  76. Serial.println();
  77. }