|
| 1 | +/* |
| 2 | + LoRa Duplex communication |
| 3 | +
|
| 4 | + Sends a message every half second, and polls continually |
| 5 | + for new incoming messages. Implements a one-byte addressing scheme, |
| 6 | + with 0xFF as the broadcast address. |
| 7 | +
|
| 8 | + Uses readString() from Stream class to read payload. The Stream class' |
| 9 | + timeout may affect other functuons, like the radio's callback. For an |
| 10 | +
|
| 11 | + created 28 April 2017 |
| 12 | + by Tom Igoe |
| 13 | +*/ |
| 14 | +#include <M5Stack.h> |
| 15 | +#include <M5LoRa.h> |
| 16 | + |
| 17 | +String outgoing; // outgoing message |
| 18 | + |
| 19 | +byte msgCount = 0; // count of outgoing messages |
| 20 | +byte localAddress = 0xFF; // address of this device |
| 21 | +byte destination = 0xBB; // destination to send to |
| 22 | + |
| 23 | +long lastSendTime = 0; // last send time |
| 24 | +int interval = 1000; // interval between sends |
| 25 | + |
| 26 | +void setup() { |
| 27 | + M5.begin(); |
| 28 | + while (!Serial); |
| 29 | + |
| 30 | + Serial.println("LoRa Duplex B"); |
| 31 | + |
| 32 | + // override the default CS, reset, and IRQ pins (optional) |
| 33 | + LoRa.setPins();// set CS, reset, IRQ pin |
| 34 | + |
| 35 | + if (!LoRa.begin(868E6)) { // initialize ratio at 868 MHz |
| 36 | + Serial.println("LoRa init failed. Check your connections."); |
| 37 | + while (true); // if failed, do nothing |
| 38 | + } |
| 39 | + |
| 40 | + Serial.println("LoRa init succeeded."); |
| 41 | +} |
| 42 | + |
| 43 | +void loop() { |
| 44 | + if (millis() - lastSendTime > interval) { |
| 45 | + String message = "HeLoRa World!"; // send a message |
| 46 | + sendMessage(message); |
| 47 | + Serial.println("Sending " + message); |
| 48 | + M5.Lcd.setTextColor(BLUE); |
| 49 | + M5.Lcd.println("Sending " + message); |
| 50 | + lastSendTime = millis(); // timestamp the message |
| 51 | + interval = random(1000) + 500; // 2-3 seconds |
| 52 | + } |
| 53 | + |
| 54 | + // parse for a packet, and call onReceive with the result: |
| 55 | + onReceive(LoRa.parsePacket()); |
| 56 | + |
| 57 | + if(M5.BtnA.wasPressed()){ |
| 58 | + M5.Lcd.setCursor(0, 0); |
| 59 | + M5.Lcd.clear(BLACK); |
| 60 | + } |
| 61 | + |
| 62 | + if(M5.BtnB.wasPressed()){ |
| 63 | + reinit(); |
| 64 | + } |
| 65 | + |
| 66 | + M5.update(); |
| 67 | +} |
| 68 | + |
| 69 | +void reinit(){ |
| 70 | + Serial.println("LoRa Duplex Reinitialization"); |
| 71 | + |
| 72 | + // override the default CS, reset, and IRQ pins (optional) |
| 73 | + LoRa.setPins();// set CS, reset, IRQ pin |
| 74 | + |
| 75 | + if (!LoRa.begin(868E6)) { // initialize ratio at 868 MHz |
| 76 | + Serial.println("LoRa init failed. Check your connections."); |
| 77 | + M5.Lcd.setCursor(0, 0); |
| 78 | + M5.Lcd.setTextColor(RED); |
| 79 | + M5.Lcd.println("Init failed!!!"); |
| 80 | + while (true); // if failed, do nothing |
| 81 | + } |
| 82 | + |
| 83 | + Serial.println("LoRa init succeeded."); |
| 84 | +} |
| 85 | + |
| 86 | +void sendMessage(String outgoing) { |
| 87 | + LoRa.beginPacket(); // start packet |
| 88 | + LoRa.write(destination); // add destination address |
| 89 | + LoRa.write(localAddress); // add sender address |
| 90 | + LoRa.write(msgCount); // add message ID |
| 91 | + LoRa.write(outgoing.length()); // add payload length |
| 92 | + LoRa.print(outgoing); // add payload |
| 93 | + LoRa.endPacket(); // finish packet and send it |
| 94 | + msgCount++; // increment message ID |
| 95 | +} |
| 96 | + |
| 97 | +void onReceive(int packetSize) { |
| 98 | + if (packetSize == 0) return; // if there's no packet, return |
| 99 | + |
| 100 | + // read packet header bytes: |
| 101 | + int recipient = LoRa.read(); // recipient address |
| 102 | + byte sender = LoRa.read(); // sender address |
| 103 | + byte incomingMsgId = LoRa.read(); // incoming msg ID |
| 104 | + byte incomingLength = LoRa.read(); // incoming msg length |
| 105 | + |
| 106 | + String incoming = ""; |
| 107 | + |
| 108 | + while (LoRa.available()) { |
| 109 | + incoming += (char)LoRa.read(); |
| 110 | + } |
| 111 | + |
| 112 | + if (incomingLength != incoming.length()) { // check length for error |
| 113 | + Serial.println("error: message length does not match length"); |
| 114 | + return; // skip rest of function |
| 115 | + } |
| 116 | + |
| 117 | + // if the recipient isn't this device or broadcast, |
| 118 | + if (recipient != localAddress && recipient != 0xFF) { |
| 119 | + Serial.println("This message is not for me."); |
| 120 | + return; // skip rest of function |
| 121 | + } |
| 122 | + |
| 123 | + // if message is for this device, or broadcast, print details: |
| 124 | + Serial.println("Received from: 0x" + String(sender, HEX)); |
| 125 | + Serial.println("Sent to: 0x" + String(recipient, HEX)); |
| 126 | + Serial.println("Message ID: " + String(incomingMsgId)); |
| 127 | + Serial.println("Message length: " + String(incomingLength)); |
| 128 | + Serial.println("Message: " + incoming); |
| 129 | + Serial.println("RSSI: " + String(LoRa.packetRssi())); |
| 130 | + Serial.println("Snr: " + String(LoRa.packetSnr())); |
| 131 | + Serial.println(); |
| 132 | + |
| 133 | + M5.Lcd.setTextColor(YELLOW); |
| 134 | + M5.Lcd.println("Message: " + incoming); |
| 135 | +} |
0 commit comments