ESP32 and SX1278 communication

Communication between ESP32 and SX1278 to realize LoRa and wireless interconnection

ESP32 and LoRa-R02 are products of anxinco. Hope to realize the communication from LoRa to wireless network through these two products.

1. Select LoRa Library

Using Arduino IDE to program software, the non-commercial software of Arduino IDE can find the response library through Arduino, and download the response library function in the response library, which is very convenient, not as much trouble as using Github. By entering LoRa in the Arduino library, we will find a large number of stocks in:

You can see the library written by Indians mentioned before. This time, I chose a library I didn't know was the LoRaLib library written by Jan growes of that country. Version No.: 8.2.0

2. Implementation of LoRa Library

Select the LoRa library, download in minutes, and everything will be done. Here is the key to implementation.

2.1 changing library functions

First, change the Module.cpp file so that we can use the correct SPI port. Which SPI port is used by default can't be seen. The downloaded files are as follows:

void Module::init(uint8_t interface, uint8_t gpio) {
  // select interface
  switch(interface) {
    case USE_SPI:
      pinMode(_cs, OUTPUT);
      digitalWrite(_cs, HIGH);
    case USE_UART:
    case USE_I2C:

Note that "SPI - > Begin () is used here. Here we change the program to:

_spi->begin(SCK, MISO, MOSI, NSS);

Then define in the header file of the library function:

Add to:

  #define SCK			14
  #define MISO			12
  #define MOSI			13
  #define NSS			15

#if defined(ESP32) || defined(ESP8266)
	#define LORALIB_DEFAULT_SPI_CS        15
  // all other architectures
  #define LORALIB_DEFAULT_SPI_CS              10

3. Programming

In the program, you need to define and configure:

#include <LoRaLib.h>
SX1278 lora = new LoRa(15, 26, 25);
// 15, 26 and 25 are used here, respectively
// 15 - SX1278_NSS
// 26 - the terminal sx1278 Θ dio0 must be connected, otherwise the message cannot be received, and there will be an error code when sending
// 25 - SX1278_DIO1
void setup() 
  pinMode(reset, OUTPUT);
  pinMode(o_led01, OUTPUT);
  digitalWrite(o_led01, HIGH);
  Serial.printf("ESP32 chip revision : %d\r\n", (int16_t)ESP.getChipRevision());
  Serial.printf("ESP32 SDK Version : %s\r\n", ESP.getSdkVersion()); 
  Serial.printf("ESP32 Speed : %u MHz\r\n", ESP.getCpuFreqMHz());
  Serial.println("Reset lora chip");
  digitalWrite(reset, HIGH);
  digitalWrite(reset, LOW);
  Serial.print(F("Initializing ... "));
  int state = lora.begin(484.001216,         // float freq
                         125.0,              // float bw
                         12,                 // uint8_t sf
                         6);                 // uint8_t cr
   if(state == ERR_NONE) 
    Serial.print(F("failed, code "));
      case ERR_CHIP_NOT_FOUND : 
        Serial.println(F("Error, CHIP not found"));
      default :

The above program completes the setup with LoRa and receives success! Description initialization completed. Now we're going to test our LoRa communications. I used the receiving communication to measure. The original equipment is an application compiled by STC, and the communication setting is 484MHz. For details, please refer to the above settings. The others remain the default.

void loop() 
	 int xState = lora.receive(n1, 0x14);
  	if ( xState == ERR_NONE)
 	     	digitalWrite(o_led01, LOW);
    		Serial.print(F("HUB Address(0-6):\t\t"));    
   	 	Serial.printf("%02X %02X %02X %02X %02X %02X %02X\r\n",

4. Operation results

The use of the library is very convenient, can quickly start, receive the effect. There is a little regret:

  1. The length of the received data cannot be determined by receiving the receipt through binary system. The byte length of the received data should be known in advance each time.
  2. Received as a string, ending when 0 is encountered.

A little regret, but not a fault. I hope our Chinese and experts can make a library function that we can use. Don't let foreigners say all day on the Internet that we will only take, not give!

32 original articles published, 41 praised, 30000 visitors+
Private letter follow

Tags: Programming network github SDK

Posted on Sat, 11 Jan 2020 01:12:29 -0500 by HaVoC