lora half duplex
#include <SPI.h>
#include <LoRa.h>
const int csPin = 7;
const int resetPin = 6;
const int irqPin = 1;
byte localAddress = 0xAA;
byte destinationAddress = 0xBB;
long lastSendTime = 0;
int interval = 2000;
int count = 0;
void setup() {
Serial.begin(9600);
Serial.println("Start LoRa duplex");
LoRa.setPins(csPin, resetPin, irqPin);
if (!LoRa.begin(433E6)) {
Serial.println("LoRa init failed. Check your connections.");
while (true) {}
}
}
void loop() {
if (millis() - lastSendTime > interval) {
String sensorData = String(count++);
sendMessage(sensorData);
Serial.print("Sending data " + sensorData);
Serial.print(" from 0x" + String(localAddress, HEX));
Serial.println(" to 0x" + String(destinationAddress, HEX));
lastSendTime = millis();
interval = random(2000) + 1000;
}
receiveMessage(LoRa.parsePacket());
}
void sendMessage(String outgoing) {
LoRa.beginPacket();
LoRa.write(destinationAddress);
LoRa.write(localAddress);
LoRa.write(outgoing.length());
LoRa.print(outgoing);
LoRa.endPacket();
}
void receiveMessage(int packetSize) {
if (packetSize == 0) return;
int recipient = LoRa.read();
byte sender = LoRa.read();
byte incomingLength = LoRa.read();
String incoming = "";
while (LoRa.available()) {
incoming += (char)LoRa.read();
}
if (incomingLength != incoming.length()) {
Serial.println("Error: Message length does not match length");
return;
}
if (recipient != localAddress) {
Serial.println("Error: Recipient address does not match local address");
return;
}
Serial.print("Received data " + incoming);
Serial.print(" from 0x" + String(sender, HEX));
Serial.println(" to 0x" + String(recipient, HEX));
}
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line