Created
December 22, 2024 20:54
-
-
Save taf2/a8fca1b0afcf41a7d3812f0757b87a66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <SoftwareSerial.h> | |
#define RX_PIN 1 | |
#define TX_PIN 2 | |
#define LED 4 | |
void handleCommand(const String &command); | |
SoftwareSerial LoRaSerial(RX_PIN, TX_PIN); | |
void setup() { | |
LoRaSerial.begin(115200); | |
pinMode(LED, OUTPUT); | |
// Initialize LoRa module | |
LoRaSerial.println("AT+RESET"); | |
digitalWrite(LED, HIGH); | |
delay(2000); // Wait for module to reset | |
digitalWrite(LED, LOW); | |
// Set unique address for receiver | |
LoRaSerial.println("AT+ADDRESS=2"); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
// Set network ID | |
LoRaSerial.println("AT+NETWORKID=10"); | |
delay(100); | |
digitalWrite(LED, LOW); | |
// Set RF frequency to 433 MHz | |
LoRaSerial.println("AT+BAND=433000000"); | |
delay(100); | |
digitalWrite(LED, HIGH); | |
// Set RF parameters: SF=7, BW=125KHz, CR=4/5, Preamble=12 | |
LoRaSerial.println("AT+PARAMETER=7,7,1,12"); | |
delay(100); | |
digitalWrite(LED, LOW); | |
} | |
void loop() { | |
if (LoRaSerial.available()) { | |
String response = LoRaSerial.readStringUntil('\n'); | |
if (response.startsWith("+RCV=")) { | |
int firstComma = response.indexOf(','); | |
int secondComma = response.indexOf(',', firstComma + 1); | |
int thirdComma = response.indexOf(',', secondComma + 1); | |
String data = response.substring(secondComma + 1, thirdComma); | |
handleCommand(data); | |
} | |
} | |
} | |
void handleCommand(const String &command) { | |
if (command == "COLOR BLUE") { | |
digitalWrite(LED, HIGH); | |
} else if (command == "COLOR RED") { | |
digitalWrite(LED, HIGH); | |
} else if (command == "COLOR OFF") { | |
digitalWrite(LED, LOW); | |
} else { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment