Untitled
unknown
c_cpp
a year ago
6.2 kB
7
Indexable
/* * Lora to Lora (P2P) using RN2484 and ESP8266 * This sketch will listen for any lora messages sent with correct SP, frequency, and other settings (see code below). * An LED will blink every time a package is received. The received payload is printed on serial monitor. * * Code based on JP Meijers code from 30 September 2016 * MN Petersen, Aug 2020 * * CONNECTIONS: * RN2483 - ESP8266: * TX - GPIO18 * RX - GPIO19 * 3V3 - 3V3 * GND - GND * * D7 on ESP8266 is connected to anode (long leg) on LED. Insert resistor between cathode and GND on ESP8266. * */ #include <HardwareSerial.h> // Define the RX and TX pins for the connection to the RN2484 module #define RN2484_RX 18 // Example pin for RX, connect to TX of RN2484 #define RN2484_TX 19 // Example pin for TX, connect to RX of RN2484 #define RN2484_RST 23 //RESET PIN // Initialize Serial2 HardwareSerial loraSerial(1); //BEGIN SERIAL ON UART1 String str; void setup() { //output LED pin pinMode(LED_BUILTIN, OUTPUT); // D7 on ESP8266 led_off(); // Open serial communications and wait for port to open: Serial.begin(57600); Serial.println("STARTUP!!!!"); //Startup print digitalWrite(RN2484_RST, LOW); // Resetting RN2483 by pulling RST pin low in 200 ms delay(200); digitalWrite(RN2484_RST, HIGH); loraSerial.begin(57600, SERIAL_8N1, RN2484_RX, RN2484_TX); //SETTING SERIAL UP loraSerial.setTimeout(1000); digitalWrite(RN2484_RST, LOW); // Resetting RN2483 by pulling RST pin low in 200 ms delay(200); digitalWrite(RN2484_RST, HIGH); lora_autobaud(); led_on(); delay(1000); led_off(); Serial.println("Initing LoRa"); // loraSerial.listen(); //str = loraSerial.readStringUntil('\n'); //Serial.println(str); loraSerial.println("sys get ver"); str = loraSerial.readStringUntil('\n'); Serial.print("Version: "); Serial.println(str); loraSerial.println("mac pause"); str = loraSerial.readStringUntil('\n'); Serial.println(str); // loraSerial.println("radio set bt 0.5"); // wait_for_ok(); // Set the LoRa modulation, frequency, power, spreading factor, bandwidth, etc. // Each command is followed by reading the response to ensure it was set correctly loraSerial.println("radio set mod lora"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set freq 869100000"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set pwr 14"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set sf sf12"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set afcbw 41.7"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set rxbw 20.8"); // Receiver bandwidth can be adjusted here. Lower BW equals better link budget / SNR (less noise). str = loraSerial.readStringUntil('\n'); // However, the system becomes more sensitive to frequency drift (due to temp) and PPM crystal inaccuracy. Serial.println(str); // loraSerial.println("radio set bitrate 50000"); // wait_for_ok(); // loraSerial.println("radio set fdev 25000"); // wait_for_ok(); loraSerial.println("radio set prlen 8"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set crc on"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set iqi off"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set cr 4/5"); // Maximum reliability is 4/8 ~ overhead ratio of 2.0 str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set wdt 1000"); //disable for continuous reception str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set sync 12"); str = loraSerial.readStringUntil('\n'); Serial.println(str); loraSerial.println("radio set bw 125"); str = loraSerial.readStringUntil('\n'); Serial.println(str); } void loop() { Serial.println("waiting for a message"); loraSerial.println("radio rx 0"); // Listen for incoming messages indefinitely str = loraSerial.readStringUntil('\n'); Serial.println(str); // Print any initial response delay(20); // Check for "ok" response to ensure the radio is in receive mode if (str.indexOf("ok") == 0) { str = String(""); // Clear the string to prepare for receiving a message while (str == "") { str = loraSerial.readStringUntil('\n'); // Wait for a message } if (str.indexOf("radio_rx") == 0) { // Check if a message was received toggle_led(); // Toggle LED as visual indication of message reception Serial.println(str); // Print the received message } else { Serial.println("Received nothing"); // Indicate that no message was received } } else { Serial.println("radio not going into receive mode"); // Error handling delay(1000); } } void lora_autobaud() { String response = ""; while (response == "") { delay(1000); // Wait a bit before trying again loraSerial.write((byte)0x00); // Send synchronization characters loraSerial.write(0x55); loraSerial.println(); // Ensure command line is terminated loraSerial.println("sys get ver"); // Attempt to get version as a way to confirm communication response = loraSerial.readStringUntil('\n'); } } /* * This function blocks until the word "ok\n" is received on the UART, * or until a timeout of 3*5 seconds. */ int wait_for_ok() { str = loraSerial.readStringUntil('\n'); if ( str.indexOf("ok") == 0 ) { return 1; } else return 0; } void toggle_led() { digitalWrite(LED_BUILTIN, 1); delay(100); digitalWrite(LED_BUILTIN, 0); } void led_on() { digitalWrite(LED_BUILTIN, 1); } void led_off() { digitalWrite(LED_BUILTIN, 0); }
Editor is loading...
Leave a Comment