LoRaSetSpread.ino 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Heltec.LoRa Duplex communication with Spreading Factor
  3. Sends a message every half second, and polls continually
  4. for new incoming messages. Sets the Heltec.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. by Aaron.Lee from HelTec AutoMation, ChengDu, China
  14. 成都惠利特自动化科技有限公司
  15. www.heltec.cn
  16. this project also realess in GitHub:
  17. https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series
  18. */
  19. #include "heltec.h"
  20. #define BAND 433E6 //you can set band here directly,e.g. 868E6,915E6
  21. byte msgCount = 0; // count of outgoing messages
  22. int interval = 2000; // interval between sends
  23. long lastSendTime = 0; // time of last packet send
  24. void setup()
  25. {
  26. //WIFI Kit series V1 not support Vext control
  27. Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  28. LoRa.setSpreadingFactor(8); // ranges from 6-12,default 7 see API docs
  29. Serial.println("Heltec.LoRa init succeeded.");
  30. }
  31. void loop()
  32. {
  33. if (millis() - lastSendTime > interval)
  34. {
  35. String message = "Heltec.LoRa World! "; // send a message
  36. message += msgCount;
  37. sendMessage(message);
  38. Serial.println("Sending " + message);
  39. lastSendTime = millis(); // timestamp the message
  40. interval = random(2000) + 1000; // 2-3 seconds
  41. msgCount++;
  42. }
  43. // parse for a packet, and call onReceive with the result:
  44. onReceive(LoRa.parsePacket());
  45. }
  46. void sendMessage(String outgoing)
  47. {
  48. LoRa.beginPacket(); // start packet
  49. LoRa.print(outgoing); // add payload
  50. LoRa.endPacket(); // finish packet and send it
  51. msgCount++; // increment message ID
  52. }
  53. void onReceive(int packetSize)
  54. {
  55. if (packetSize == 0) return; // if there's no packet, return
  56. // read packet header bytes:
  57. String incoming = "";
  58. while (LoRa.available())
  59. {
  60. incoming += (char)LoRa.read();
  61. }
  62. Serial.println("Message: " + incoming);
  63. Serial.println("RSSI: " + String(LoRa.packetRssi()));
  64. Serial.println("Snr: " + String(LoRa.packetSnr()));
  65. Serial.println();
  66. }