LoRa E32 device for Arduino, esp32 or esp8266: power saving and sending structured data – Part 5

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Now we are going to undestand how to send complex structure and manage power saving with our E32 UART LoRa device based on popular SX1276/SX1278 Wireless Modules.

sx1278 sx1276 wireless lora uart module serial 3000m arduino 433 rf

You can find module here

Another interesting features is the power saving configuration, you can set M0 and M1 to do a wake-up from a device to onother device in power saving mode.

Mode M1 M0 Explanation
Normal00UART and wireless channel is good to go
Wake-Up01Same as normal but a preamble code is added to transmitted data for waking-up the receiver.
Power-Saving10UART is disable and wireless is on WOR(wake on radio) mode which means the device will turn on when there is data to be received. Transmission is not allowed.
Sleep11Used in setting parameters. Transmitting and receiving disabled.

So we need to connect the sending device in Wake-Up mode (If you fully connected the device the library do all mode for you):

LoRa E32 Arduino Wake-Up connection
LoRa E32 Wemos D1 Wake-Up connection

And receiver device in power saving mode (If you fully connected the device the library do all mode for you):

LoRa E32 Arduino Power Saving connection
LoRa E32 Wemos D1 Power Saving connection

Than you can use the sketch already posted.

Wake time

An important configuration parameter is wake time, for the sender is important because It add a long preamble to the message (long as wake time), the receiver that use wake time as pull interval time check.
So if the receiver check every 250ms (wake time) is there is a message if the sender add a 2 seconds wake time probably the receiver check message preamble, wait that arrive the real message, read It and return to power save mode.

So If you want maximize the power save you must put minimal WAKE TIME to the sender and maximize WAKE TIME to the receiver, if you want more efficiency you must do the inverse.

Sending sketch (if you don’t do fully connection, remember that first you must set configuration in program/sleep mode and than return in specified modality)

/*
* LoRa E32-TTL-100
* Send fixed transmission message to a specified point.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) 3.3v (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND  (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP & Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6); // Config without connect AUX and M0 M1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, D3); // RX, TX
LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
//LoRa_E32 e32ttl(2, 3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
e32ttl.setMode(MODE_1_WAKE_UP);
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 0x01;
configuration.ADDH = 0x00;
configuration.CHAN = 0x04;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
configuration.OPTION.wirelessWakeupTime = WAKE_UP_2000;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
}
// The loop function is called in an endless loop
void loop()
{
delay(2000);
// ResponseStatus LoRa_E32::sendFixedMessage(byte ADDL, byte ADDH, byte CHAN, const String message)
Serial.println("Send message to 00 03 04");
ResponseStatus rs = e32ttl.sendFixedMessage(0, 3, 0x04, "Message to 00 03 04 device");
Serial.println(rs.getResponseDescription());
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, BIN);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, BIN);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

And receiving sketch (if you don’t do fully connection, remember that first you must set configuration in program/sleep mode and than return in specified modality):

/*
* LoRa E32-TTL-100
* Receive fixed transmission message on channel.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND  (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) 3.3v (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
LoRa_E32 e32ttl(2, 3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
e32ttl.setMode(MODE_2_POWER_SAVING);
//	e32ttl.resetModule();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 3;
configuration.ADDH = 0;
configuration.CHAN = 0x04;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
configuration.OPTION.wirelessWakeupTime = WAKE_UP_250;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
Serial.println();
Serial.println("Start listening!");
}
// The loop function is called in an endless loop
void loop()
{
if (e32ttl.available()  > 1){
ResponseContainer rs = e32ttl.receiveMessage();
// First of all get the data
String message = rs.data;
Serial.println(rs.status.getResponseDescription());
Serial.println(message);
}
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, DEC);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, DEC);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

Send complex structure

We can use string as we want, we can use It as JSON format and so on, but if you want use strucutred message in native mode (byte array) It’s possible to send they.

The example code for the sender can be:

struct Message {
char type[5] = "TEMP";
char message[8] = "Kitchen";
byte temperature[4];
} message;
*(float*)(message.temperature) = 19.2;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());

and to receive:

if (e32ttl.available()  > 1){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Message));
struct Message message = *(Message*) rsc.data;
Serial.println(message.type);
Serial.println(*(float*)(message.temperature));
Serial.println(message.message);
free(rsc.data);
}

Complete sender sketch:

/*
* LoRa E32-TTL-100
* Send fixed transmission structured message to a specified point.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6); // Config without connect AUX and M0 M1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, D3); // RX, TX
LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
//LoRa_E32 e32ttl(2, 3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 0x01;
configuration.ADDH = 0x00;
configuration.CHAN = 0x02;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
}
struct Message {
char type[5];
char message[8];
int temperature;
} message;
int i = 0;
// The loop function is called in an endless loop
void loop()
{
delay(2500);
i++;
struct Message {
char type[5] = "TEMP";
char message[8] = "Kitchen";
byte temperature[4];
} message;
*(float*)(message.temperature) = 19.2;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, BIN);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, BIN);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

Complete receiver sketch:

/*
* LoRa E32-TTL-100
* Receive fixed transmission message on channel.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
LoRa_E32 e32ttl(2, 3, 4); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
//	e32ttl.resetModule();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 3;
configuration.ADDH = 0;
configuration.CHAN = 0x04;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
Serial.println();
Serial.println("Start listening!");
e32ttl.setMode(MODE_2_POWER_SAVING);
}
struct Message {
char type[5];
char message[8];
byte temperature[4];
};
// The loop function is called in an endless loop
void loop()
{
if (e32ttl.available()  > 1){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Message));
struct Message message = *(Message*) rsc.data;
Serial.println(message.type);
Serial.println(*(float*)(message.temperature));
Serial.println(message.message);
free(rsc.data);
}
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, DEC);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, DEC);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

But the real life is not so simple, probably you have multiple sensor with different structure in your home, so you need to receive multiple structure, a possibile solution is to read the first part of structure and instantiate the rest as you want.

So if you want have the TYPE of structure you are going to read:

char type[5]; // first part of structure
ResponseContainer rs = e32ttl.receiveInitialMessage(sizeof(type));
String typeStr = rs.data;

With this information we can create specified structure from different device for example:

struct Message {
char type[5] = "TEMP";
char message[8] = "Kitchen";
byte temperature[4];
} message;
*(float*)(message.temperature) = 19.2;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());

or

struct Message {
char type[5] = "HUM";
char message[8] = "Room";
byte humidity;
} message;
message.humidity = 65;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());

And so you can load specified strucutre from the receiver:

if (e32ttl.available()  > 1){
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) );
String typeStr = rs.data;
Serial.println(typeStr);
if (typeStr=="TEMP"){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Message));
struct Message message = *(Message*) rsc.data;
Serial.println(*(float*)(message.temperature));
Serial.println(message.message);
free(rsc.data);
}else if (typeStr == "HUM"){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(MessageHumidity));
struct MessageHumidity message = *(MessageHumidity*) rsc.data;
Serial.println(message.humidity);
Serial.println(message.message);
free(rsc.data);
}else{
Serial.println("Something goes wrong!!");
}
}

So we have 2 sender sketch like this that send float temperature:

/*
* LoRa E32-TTL-100
* Send fixed transmission structured message to a specified point.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6); // Config without connect AUX and M0 M1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, D3); // RX, TX
LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
//LoRa_E32 e32ttl(2, 3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 0x01;
configuration.ADDH = 0x00;
configuration.CHAN = 0x02;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
}
struct Message {
char type[5];
char message[8];
int temperature;
} message;
int i = 0;
// The loop function is called in an endless loop
void loop()
{
delay(2500);
i++;
struct Message {
char type[5] = "TEMP";
char message[8] = "Kitchen";
byte temperature[4];
} message;
*(float*)(message.temperature) = 19.2;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, BIN);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, BIN);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

and like so to send humidity value:

/*
* LoRa E32-TTL-100
* Send fixed transmission structured message to a specified point.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6); // Config without connect AUX and M0 M1
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D2, D3); // RX, TX
LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
//LoRa_E32 e32ttl(2, 3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 0x02;
configuration.ADDH = 0x00;
configuration.CHAN = 0x02;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
}
int i = 0;
// The loop function is called in an endless loop
void loop()
{
delay(2500);
i++;
struct Message {
char type[5] = "HUM";
char message[8] = "Room";
byte humidity;
} message;
message.humidity = 65;
ResponseStatus rs = e32ttl.sendFixedMessage(0,3,4,&amp;message, sizeof(Message));
Serial.println(rs.getResponseDescription());
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, BIN);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, BIN);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

And a receiver that manage the two type of messages:

/*
* LoRa E32-TTL-100
* Receive fixed transmission structure message and read first part and load the rest of structure.
* https://www.mischianti.org
*
* E32-TTL-100----- Arduino UNO or esp8266
* M0         ----- 3.3v (To config) GND (To send) 7 (To dinamically manage)
* M1         ----- 3.3v (To config) GND (To send) 6 (To dinamically manage)
* RX         ----- PIN 2 (PullUP &amp; Voltage divider)
* TX         ----- PIN 3 (PullUP)
* AUX        ----- Not connected (5 if you connect)
* VCC        ----- 3.3v/5v
* GND        ----- GND
*
*/
#include "Arduino.h"
#include "LoRa_E32.h"
// ---------- esp8266 pins --------------
//LoRa_E32 e32ttl(D2, D3, D5, D7, D6);
//LoRa_E32 e32ttl(D2, D3); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, D5, D7, D6);
// -------------------------------------
// ---------- Arduino pins --------------
//LoRa_E32 e32ttl(2, 3, 5, 7, 6);
LoRa_E32 e32ttl(2, 3, 4); // Config without connect AUX and M0 M1
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); // RX, TX
//LoRa_E32 e32ttl(&amp;mySerial, 5, 7, 6);
// -------------------------------------
void printParameters(struct Configuration configuration);
void printModuleInformation(struct ModuleInformation moduleInformation);
//The setup function is called once at startup of the sketch
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
delay(100);
e32ttl.begin();
//	e32ttl.resetModule();
// After set configuration comment set M0 and M1 to low
// and reboot if you directly set HIGH M0 and M1 to program
ResponseStructContainer c;
c = e32ttl.getConfiguration();
Configuration configuration = *(Configuration*) c.data;
configuration.ADDL = 3;
configuration.ADDH = 0;
configuration.CHAN = 0x04;
configuration.OPTION.fixedTransmission = FT_FIXED_TRANSMISSION;
e32ttl.setConfiguration(configuration, WRITE_CFG_PWR_DWN_SAVE);
printParameters(configuration);
// ---------------------------
Serial.println();
Serial.println("Start listening!");
e32ttl.setMode(MODE_2_POWER_SAVING);
}
struct Message {
char message[8];
byte temperature[4];
};
struct MessageHumidity {
char message[8];
byte humidity;
};
// The loop function is called in an endless loop
void loop()
{
if (e32ttl.available()  > 1){
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) );
String typeStr = rs.data;
Serial.println(typeStr);
if (typeStr=="TEMP"){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(Message));
struct Message message = *(Message*) rsc.data;
Serial.println(*(float*)(message.temperature));
Serial.println(message.message);
free(rsc.data);
}else if (typeStr == "HUM"){
ResponseStructContainer rsc = e32ttl.receiveMessage(sizeof(MessageHumidity));
struct MessageHumidity message = *(MessageHumidity*) rsc.data;
Serial.println(message.humidity);
Serial.println(message.message);
free(rsc.data);
}else{
Serial.println("Something goes wrong!!");
}
}
}
void printParameters(struct Configuration configuration) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD : "));  Serial.print(configuration.HEAD, BIN);Serial.print(" ");Serial.print(configuration.HEAD, DEC);Serial.print(" ");Serial.println(configuration.HEAD, HEX);
Serial.println(F(" "));
Serial.print(F("AddH : "));  Serial.println(configuration.ADDH, DEC);
Serial.print(F("AddL : "));  Serial.println(configuration.ADDL, DEC);
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());
Serial.println("----------------------------------------");
}
void printModuleInformation(struct ModuleInformation moduleInformation) {
Serial.println("----------------------------------------");
Serial.print(F("HEAD BIN: "));  Serial.print(moduleInformation.HEAD, BIN);Serial.print(" ");Serial.print(moduleInformation.HEAD, DEC);Serial.print(" ");Serial.println(moduleInformation.HEAD, HEX);
Serial.print(F("Model no.: "));  Serial.println(moduleInformation.model, HEX);
Serial.print(F("Version  : "));  Serial.println(moduleInformation.version, HEX);
Serial.print(F("Features : "));  Serial.println(moduleInformation.features, HEX);
Serial.println("----------------------------------------");
}

Thanks

And this was the last part of my little tutorial.

  1. LoRa E32 device for Arduino, esp32 or esp8266: settings and basic usage
  2. LoRa E32 device for Arduino, esp32 or esp8266: library
  3. LoRa E32 device for Arduino, esp32 or esp8266: configuration
  4. LoRa E32 device for Arduino, esp32 or esp8266: fixed transmission
  5. LoRa E32 device for Arduino, esp32 or esp8266: power saving and sending structured data
  6. LoRa E32 device for Arduino, esp32 or esp8266: WOR (wake on radio) the microcontroller and Arduino shield
  •  
  •  
  •  
  •  
  •  
  •  

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *