Introduction: LoRa 3Km to 8Km Wireless Communication With Low Cost E32 (sx1278/sx1276) Device for Arduino, Esp8266 or Esp32
I create a library to manage EBYTE E32 based on Semtech series of LoRa device, very powerfull, simple and cheap device.
You can find 3Km version here, 8Km version here
They can work over a distance of 3000m to 8000m, and they have a lot of features and parameter. So I create this library to simplify the usage.
It's a solution for retrieve data from metropolitan sensors or to control drone.
Supplies
Step 1: Library
You can find my library here.
To download.
Click the DOWNLOADS button in the top right corner, rename the uncompressed folder LoRa_E32.
Check that the LoRa_E32 folder contains LoRa_E32.cpp and LoRa_E32.h.
Place the LoRa_E32 library folder your /libraries/ folder. You may need to create the libraries subfolder if its your first library.
Restart the IDE.
Step 2: Pinout
As you can see you can set various modes via M0 and M1 pins.
There are some pins that can be use in a static way, but If you connect It to the microcontroller and configure they in the library you gain in performance and you can control all mode via software, but we are going to explain better next.
Step 3: AUX Pin
As I already say It’s not important to connect all pin to the output of microcontroller, you can put M0 and M1 pins to HIGH or LOW to get desidered configuration, and if you don’t connect AUX the library set a reasonable delay to be sure that the operation is complete.
AUX pin
When transmitting data can be used to wake up external MCU and return HIGH on data transfer finish.
When receiving AUX going LOW and return HIGH when buffer is empty.
It’s also used for self checking to restore normal operation (on power-on and sleep/program mode).
Step 4: Fully Connected Schema Esp8266
esp8266 connection schema is more simple because It work at the same voltage of logical communications (3.3v).
It’s important to add pull-up resistor (4, 7Kohm) to get good stability.
Step 5: Fully Connected Schema Arduino
Arduino working voltage is 5v, so we need to add a voltage divider on RX pin M0 and M1 of LoRa module to prevent damage, you can get more information here Voltage divider: calculator and application.
You can use a 2Kohm resistor to GND and 1Kohm from signal than put together on RX.
Step 6: Library: Constructor
I made a set of quite numerous constructors, because we can have more options and situations to manage.
LoRa_E32(byte rxPin, byte txPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600); LoRa_E32(byte rxPin, byte txPin, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600); LoRa_E32(byte rxPin, byte txPin, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);
First set of constructor are create to delegate the manage of Serial and other pins to the library.
rxPin and txPin is the pin to connect to UART and they are mandatory.
auxPin is a pin that check the operation, transmission and receiving status (we are going to explain better next), that pin It isn’t mandatory, if you don’t set It I apply a delay to permit the operation to complete itself (with latency).
m0pin and m1Pin are the pins to change operation MODE (see the table upper), I think this pins in “production” are going to connect directly HIGH or LOW, but for test they are usefully to be managed by the library.
bpsRate is the boudrate of SoftwareSerial normally is 9600 (the only baud rate in programmin/sleep mode)
A simple example is
<p>#include "LoRa_E32.h"<br>LoRa_E32 e32ttl100(2, 3); // RX, TX // LoRa_E32 e32ttl100(2, 3, 5, 6, 7); // RX, TX</p>
We can use directly a SoftwareSerial with another constructor
<p>LoRa_E32(HardwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p><p>LoRa_E32(HardwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p><p>LoRa_E32(HardwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p>
The example upper with this constructor can be do like so.
<p>#include #include "LoRa_E32.h"</p><p>SoftwareSerial mySerial(2, 3); // RX, TX</p><p>LoRa_E32 e32ttl100(&mySerial);</p><p>// LoRa_E32 e32ttl100(&mySerial, 5, 7, 6);</p>
The last set of constructor is to permit to use an HardwareSerial instead of SoftwareSerial.
<p>LoRa_E32(SoftwareSerial* serial, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p><p>LoRa_E32(SoftwareSerial* serial, byte auxPin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p><p>LoRa_E32(SoftwareSerial* serial, byte auxPin, byte m0Pin, byte m1Pin, UART_BPS_RATE bpsRate = UART_BPS_RATE_9600);</p>
Step 7: Begin
The begin command is used to startup Serial and pins in input and output mode.
void begin();
in execution is
// Startup all pins and UART
e32ttl100.begin();
Step 8: Configuration and Information Method
There a set of methods for manage configuration and get information of the device.
ResponseStructContainer getConfiguration();
ResponseStatus setConfiguration(Configuration configuration, PROGRAM_COMMAND saveType = WRITE_CFG_PWR_DWN_LOSE);
ResponseStructContainer getModuleInformation();
void printParameters(struct Configuration configuration);
ResponseStatus resetModule();
Step 9: Response Container
To simplify the manage of response I create a set of container, for me very usefully to manage errors and return generic data.
ResponseStatus
This is a status container and have 2 simple entry point, with this you can get the status code and the description of status code
Serial.println(c.getResponseDescription()); // Description of code
Serial.println(c.code); // 1 if Success
The code are
SUCCESS = 1,
ERR_UNKNOWN,
ERR_NOT_SUPPORT,
ERR_NOT_IMPLEMENT,
ERR_NOT_INITIAL,
ERR_INVALID_PARAM,
ERR_DATA_SIZE_NOT_MATCH,
ERR_BUF_TOO_SMALL,
ERR_TIMEOUT,
ERR_HARDWARE,
ERR_HEAD_NOT_RECOGNIZED
ResponseContainer
This container is created to manage String response and have 2 entry point.
data with the string returned from message and status an instance of RepsonseStatus.
ResponseContainer rs = e32ttl.receiveMessage();
String message = rs.data;
Serial.println(rs.status.getResponseDescription());
Serial.println(message);
ResponseStructContainer
This is the more “complex” container, I use this to manage structure, It has the same entry point of ResponseContainer but data is a void pointer to manage complex structure.
ResponseStructContainer c;
c = e32ttl100.getConfiguration();// It's important get configuration pointer before all other operation
Configuration configuration = *(Configuration*) c.data;
Serial.println(c.status.getResponseDescription());
Serial.println(c.status.code);
getConfiguration and setConfiguration
The first method is getConfiguration, you can use It to retrive all data stored on device.
ResponseStructContainer getConfiguration();
Here an usage example.
ResponseStructContainer c;
c = e32ttl100.getConfiguration();// It's important get configuration pointer before all other operation
Configuration configuration = *(Configuration*) c.data;
Serial.println(c.status.getResponseDescription());
Serial.println(c.status.code);
Serial.println(configuration.SPED.getUARTBaudRate());
Structure of configuration have all data of settings, and I add a series of function to get all description of single data.
configuration.ADDL = 0x0; // First part of address
configuration.ADDH = 0x1; // Second part of address configuration.CHAN = 0x19;// Channel configuration.OPTION.fec = FEC_0_OFF; // Forward error correction switch configuration.OPTION.fixedTransmission = FT_TRANSPARENT_TRANSMISSION; // Transmission mode configuration.OPTION.ioDriveMode = IO_D_MODE_PUSH_PULLS_PULL_UPS; // Pull-up management configuration.OPTION.transmissionPower = POWER_17; // dBm transmission power configuration.OPTION.wirelessWakeupTime = WAKE_UP_1250; // Wait time for wake up configuration.SPED.airDataRate = AIR_DATA_RATE_011_48; // Air data rate configuration.SPED.uartBaudRate = UART_BPS_115200; // Communication baud rate configuration.SPED.uartParity = MODE_00_8N1; // Parity bit
You have the equivalent function for all attribute to get all description:
Serial.print(F("Chan : ")); Serial.print(configuration.CHAN, DEC); Serial.print(" -> "); Serial.println(configuration.getChannelDescription());
Serial.println(F(" ")); Serial.print(F("SpeedParityBit : ")); Serial.print(configuration.SPED.uartParity, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getUARTParityDescription()); Serial.print(F("SpeedUARTDatte : ")); Serial.print(configuration.SPED.uartBaudRate, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getUARTBaudRate()); Serial.print(F("SpeedAirDataRate : ")); Serial.print(configuration.SPED.airDataRate, BIN);Serial.print(" -> "); Serial.println(configuration.SPED.getAirDataRate()); Serial.print(F("OptionTrans : ")); Serial.print(configuration.OPTION.fixedTransmission, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getFixedTransmissionDescription()); Serial.print(F("OptionPullup : ")); Serial.print(configuration.OPTION.ioDriveMode, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getIODroveModeDescription()); Serial.print(F("OptionWakeup : ")); Serial.print(configuration.OPTION.wirelessWakeupTime, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getWirelessWakeUPTimeDescription()); Serial.print(F("OptionFEC : ")); Serial.print(configuration.OPTION.fec, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getFECDescription()); Serial.print(F("OptionPower : ")); Serial.print(configuration.OPTION.transmissionPower, BIN);Serial.print(" -> "); Serial.println(configuration.OPTION.getTransmissionPowerDescription());
At same way setConfiguration want a configuration strucutre, so I think the better way to manage configuration is to retrieve the current one, apply the only change you need and set It again.
ResponseStatus setConfiguration(Configuration configuration, PROGRAM_COMMAND saveType = WRITE_CFG_PWR_DWN_LOSE);
configuration is the strucutre previsiouly show, saveType permit to you to choiche if the change become permanently of only for the current session.
ResponseStructContainer c;
c = e32ttl100.getConfiguration(); // It's important get configuration pointer before all other operation Configuration configuration = *(Configuration*) c.data; Serial.println(c.status.getResponseDescription()); Serial.println(c.status.code); printParameters(configuration); configuration.ADDL = 0x0; configuration.ADDH = 0x1; configuration.CHAN = 0x19; configuration.OPTION.fec = FEC_0_OFF; configuration.OPTION.fixedTransmission = FT_TRANSPARENT_TRANSMISSION; configuration.OPTION.ioDriveMode = IO_D_MODE_PUSH_PULLS_PULL_UPS; configuration.OPTION.transmissionPower = POWER_17; configuration.OPTION.wirelessWakeupTime = WAKE_UP_1250; configuration.SPED.airDataRate = AIR_DATA_RATE_011_48; configuration.SPED.uartBaudRate = UART_BPS_115200; configuration.SPED.uartParity = MODE_00_8N1; // Set configuration changed and set to not hold the configuration ResponseStatus rs = e32ttl100.setConfiguration(configuration, WRITE_CFG_PWR_DWN_LOSE); Serial.println(rs.getResponseDescription()); Serial.println(rs.code); printParameters(configuration);
The parameter are all managed as constant:
Step 10: Basic Configuration Option
Step 11: Send Receive Message
First we must introduce a simple but usefully method to check if something is in the receiving buffer
int available();
It’s simply return how many bytes you have in the current stream.
Step 12: Normal Transmission Mode
Normal/Transparent transmission mode is used to send messages to all device with same address and channel.
There are a lot of method to send/receive message, we are going to explain in detail:
ResponseStatus sendMessage(const String message);
ResponseContainer receiveMessage();
First method is sendMessage and is used to send a String to a device in Normal mode.
ResponseStatus rs = e32ttl.sendMessage("Prova");
Serial.println(rs.getResponseDescription());
The other device simply do on the loop
if (e32ttl.available() > 1){
ResponseContainer rs = e32ttl.receiveMessage(); String message = rs.data; // First ever get the data Serial.println(rs.status.getResponseDescription()); Serial.println(message); }
Step 13: Manage Structure
If you want send a complex strucuture you can use this method
ResponseStatus sendMessage(const void *message, const uint8_t size);
ResponseStructContainer receiveMessage(const uint8_t size);
It’s used to send strucutre, for example:
struct Messaggione {
char type[5]; char message[8]; bool mitico; }; struct Messaggione messaggione = {"TEMP", "Peple", true}; ResponseStatus rs = e32ttl.sendMessage(&messaggione, sizeof(Messaggione)); Serial.println(rs.getResponseDescription());
and the other side you can receive the message so
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Messaggione));
struct Messaggione messaggione = *(Messaggione*) rsc.data; Serial.println(messaggione.message); Serial.println(messaggione.mitico);
Read partial strucure
If you want read first part of the message to manage more type of strucutre you can use this method.
ResponseContainer receiveInitialMessage(const uint8_t size);
I create It to receive a string with type or other to identify the strucuture to load.
struct Messaggione { // Partial strucutre without type
char message[8]; bool mitico; }; char type[5]; // first part of structure ResponseContainer rs = e32ttl.receiveInitialMessage(sizeof(type)); // Put string in a char array (not needed) memcpy ( type, rs.data.c_str(), sizeof(type) ); Serial.println("READ TYPE: "); Serial.println(rs.status.getResponseDescription()); Serial.println(type); // Read the rest of structure ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Messaggione)); struct Messaggione messaggione = *(Messaggione*) rsc.data;
Step 14: Fixed Mode Instead of Normal Mode
At same manner I create a set of method to use with fixed transmission
Fixed transmission
You need to change only the sending method, because the destination device don’t receive the preamble with Address and Channel quando settato il fixed mode.
So for String message you have
ResponseStatus sendFixedMessage(byte ADDL, byte ADDH, byte CHAN, const String message);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const String message);
and for structure you have
ResponseStatus sendFixedMessage(byte ADDL, byte ADDH, byte CHAN, const void *message, const uint8_t size);
ResponseStatus sendBroadcastFixedMessage(byte CHAN, const void *message, const uint8_t size );
Here a simple example
ResponseStatus rs = e32ttl.sendFixedMessage(0, 0, 0x17, &messaggione, sizeof(Messaggione));
// ResponseStatus rs = e32ttl.sendFixedMessage(0, 0, 0x17, "Ciao");
Fixed transmission have more scenarios
If you send to a specific device (second scenarios Fixed transmission) you must add ADDL, ADDH and CHAN to identify It directly.
ResponseStatus rs = e32ttl.sendFixedMessage(2, 2, 0x17, "Message to a device");
If you want send a message to all device in a specified Channel you can use this method.
ResponseStatus rs = e32ttl.sendBroadcastFixedMessage(0x17, "Message to a devices of a channel");
If you want receive all broadcast message in the network you must set your ADDH and ADDL with BROADCAST_ADDRESS.
ResponseStructContainer c;
c = e32ttl100.getConfiguration(); // It's important get configuration pointer before all other operation Configuration configuration = *(Configuration*) c.data; Serial.println(c.status.getResponseDescription()); Serial.println(c.status.code); printParameters(configuration); configuration.ADDL = BROADCAST_ADDRESS; configuration.ADDH = BROADCAST_ADDRESS; // Set configuration changed and set to not hold the configuration ResponseStatus rs = e32ttl100.setConfiguration(configuration, WRITE_CFG_PWR_DWN_LOSE); Serial.println(rs.getResponseDescription()); Serial.println(rs.code); printParameters(configuration);
Step 15: Thanks
Now you have all information to do your work, but I think It’s important to show some realistic examples to undestand better all the possibility.
- LoRa E32 device for Arduino, esp32 or esp8266: settings and basic usage
- LoRa E32 device for Arduino, esp32 or esp8266: library
- LoRa E32 device for Arduino, esp32 or esp8266: configuration
- LoRa E32 device for Arduino, esp32 or esp8266: fixed transmission
- LoRa E32 device for Arduino, esp32 or esp8266: power saving and sending structured data