Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.7 kB
0
Indexable
Never
/*
 * There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
 * 
*/

// Define pins for RN2483 communication
#define RN2483_RX 18  // Sets RX pin for ESP32
#define RN2483_TX 19   // Sets TX pin for ESP32
#define RST 23      // Sets Reset pin for ESP32
#include <HardwareSerial.h>

String str;
int packageID = 0;

// Define the hardware serial port (UART2)
HardwareSerial mySerial(1);
void setup() {
  //output LED pin
  pinMode(13, OUTPUT);  // D7 on ESP8266

  // Open serial communications and wait for port to open:
  Serial.begin(57600);  // Serial communication to PC
  reset();
  Serial.println("tester");
  mySerial.begin(57600, SERIAL_8N1, 18, 19);  // Starts serial for RN2483
  mySerial.setTimeout(1000);
  reset();

  lora_autobaud();   
  led_on();
  delay(1000);
  led_off();

  Serial.println("Initing LoRa");

  if (mySerial.available()) {       //Replaced previous command and checks for messages.
    String incomingData = mySerial.readStringUntil('\n');
    Serial.println("Received: " + incomingData);
  }

  //Settings for the RN2483 module
  str = mySerial.readStringUntil('\n');
  Serial.println(str);
  mySerial.println("sys get ver");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("mac pause");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  //  mySerial.println("radio set bt 0.5");  // Uncomment if we want to use FSK
  //  wait_for_ok();

  mySerial.println("radio set mod lora"); // Comment if we want to use FSK
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set freq 869100000");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set pwr 14");  //max power 14 dBm
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set sf sf12");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set afcbw 41.7");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set rxbw 125");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  //  mySerial.println("radio set bitrate 50000");
  //  wait_for_ok();

  //  mySerial.println("radio set fdev 25000");
  //  wait_for_ok();

  mySerial.println("radio set prlen 8");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set crc on");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set iqi off");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set cr 4/5");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set wdt 60000"); //disable for continuous reception
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set sync 12");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  mySerial.println("radio set bw 125");
  str = mySerial.readStringUntil('\n');
  Serial.println(str);

  Serial.println("starting loop");
}

void loop() { 
  //Loop where the packages sent are printed as well as package ID
  led_on();
  Serial.print("packageID = ");
  Serial.println(packageID);
  mySerial.print("radio tx ");
  mySerial.println("20");
  mySerial.println(packageID);
  str = mySerial.readStringUntil('\n');
  Serial.println(str);
  str = mySerial.readStringUntil('\n');
  Serial.println(str);
  led_off();
  delay(2000);
  packageID = packageID + 1;
}


void lora_autobaud()
{
  //Used to print settings from the RN2483
  String response = "";
  while (response == "")
  {
    delay(1000);
    mySerial.write((byte)0x00);
    mySerial.write(0x55);
    mySerial.println();
    mySerial.println("sys get ver");
    response = mySerial.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 = mySerial.readStringUntil('\n');
  if ( str.indexOf("ok") == 0 ) {
    return 1;
  }
  else return 0;
}

void led_on()
{
  digitalWrite(13, 1);
}

void led_off()
{
  digitalWrite(13, 0);
}
void reset()
{
  digitalWrite(RST, LOW); // Resetting RN2483 by pulling RST pin low in 200 ms
  delay(200);
  digitalWrite(RST, HIGH);
}
Leave a Comment