Site icon Renzo Mischianti

Ebyte LoRa E220 device for Arduino, esp32 or esp8266: manage Wake On Radio and sends structured data – 5

Spread the love

Now let’s examine the Wake On Radio at Wake On Radio (WOR) and power-saving, one of the essential features of our Ebyte E220 UART LoRa device based on LLCC68 Wireless Modules.

Then we must understand how to manage complex structures that are better optimized than string format data.

Ebyte LoRa E220 LLCC68 device manage Wake On Radio and sending structured data

Here a selection of LoRa devices E220-400T22D 433MHz 5Km - E220-400T30D 433MHz 10Km - E220-900T22D 868MHz 915MHz 5Km - E220-900T30D 868MHz 915MHz 10Km

If you have trouble with the device freezing, you must put a pull-up 4.7k resistor or better connect to the device AUX pin.

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

Manage WOR (Wake On Radio)

As already said, these devices offer some exciting features to manage the power saving, and you can set M0 and M1 to do a wake-up from one device to another device in WOR receiving mode.

ModeM1M0Explanation
Normal00UART and wireless channels are open, and transparent transmission is on
WOR Transmitter01WOR Transmitter
WOR Receiver10WOR Receiver (Supports wake up over air)
Deep sleep mode11The module goes to sleep (automatically wake up when configuring parameters)

Wiring

WOR Transmitting mode

So we need to connect the sending device in WOR transmitting mode (If you fully connect the device, the library manage all modality for you):

LoRa E32 Arduino Wake-Up connection
LoRa E32 Wemos D1 wake up the connection
LoRa E32 E220 ESP32 DEV KIT V1 Wake UP WOR transmitting mode on a breadboard
LoRa E32 E220 Arduino MKR WiFi 1010 Wake UP WOR transmitting mode on a breadboard

Power saving/WOR receiving mode

The receiver device must be in power saving/WOR receiving mode (If you fully connected the device, the library do all modality for you):

LoRa E32 Arduino Power Saving connection
LoRa E32 Wemos D1 power saving connection
LoRa E32 E220 ESP32 DEV KIT V1 Power saving, WOR receiving mode on the breadboard
LoRa E32 E220 Arduino MKR WiFi 1010 Power saving WOR receiving mode on the breadboard

Code

WOR Cycle

A critical configuration parameter is WOR Cycle, for the sender is essential because It adds a long preamble to the message (long as wake time). The handset uses the wake-up time as the cadence for checking a wake-up message. So if the receiver checks every 2000ms (polling time) if there is a message, the sender adds a 2000ms preamble. The receiver intercepts the message preamble, waits for the actual message to read, and returns to power save mode.

So If you want to maximize the power save, you must put minimal WOR Cycle. If you want more efficiency, you must do the inverse.

The sender and receiver in this device must have the same WOR Cycle.

Here is the configuration for the WOR transmitter:

	configuration.ADDL = 0x02;
	configuration.ADDH = 0x00;

	configuration.CHAN = 23;

	configuration.SPED.uartBaudRate = UART_BPS_9600;
	configuration.SPED.airDataRate = AIR_DATA_RATE_010_24;
	configuration.SPED.uartParity = MODE_00_8N1;

	configuration.OPTION.subPacketSetting = SPS_200_00;
	configuration.OPTION.RSSIAmbientNoise = RSSI_AMBIENT_NOISE_DISABLED;
	configuration.OPTION.transmissionPower = POWER_22;

	configuration.TRANSMISSION_MODE.enableRSSI = RSSI_DISABLED;
	configuration.TRANSMISSION_MODE.fixedTransmission = FT_FIXED_TRANSMISSION;
	configuration.TRANSMISSION_MODE.enableLBT = LBT_DISABLED;
	configuration.TRANSMISSION_MODE.WORPeriod = WOR_2000_011;

Sketch for sending a WOR

Sending sketch (if you don’t make a connection entirely, remember that first, you must set configuration in transmitting WOR mode and then return in specified modality)

/*
 * EBYTE LoRa E220
 * Send wake up WOR message to a device in Sleep (WOR transmitter)
 *
 * You must configure the address with 0 3 23 (FIXED SENDER configuration)
 * and pay attention that WOR period must be the same of receiver
 *
 * by Renzo Mischianti <https://mischianti.org>
 *
 * https://mischianti.org
 *
 * E220		  ----- WeMos D1 mini	----- esp32			----- Arduino Nano 33 IoT	----- Arduino MKR	----- ArduinoUNO
 * M0         ----- D7 (or 3.3v)	----- 19 (or 3.3v)	----- 2 (or 3.3v)			----- 2 (or 3.3v)	----- 7 Volt div (or 3.3v)
 * M1         ----- D6 (or GND)		----- 21 (or GND)	----- 4 (or GND)			----- 4 (or GND)	----- 6 Volt div (or GND)
 * TX         ----- D3 (PullUP)		----- TX2 (PullUP)	----- TX1 (PullUP)			----- 14 (PullUP)	----- 4 (PullUP)
 * RX         ----- D4 (PullUP)		----- RX2 (PullUP)	----- RX1 (PullUP)			----- 13 (PullUP)	----- 5 Volt div (PullUP)
 * AUX        ----- D5 (PullUP)		----- 18  (PullUP)	----- 0  (PullUP)			----- 0  (PullUP)	----- 3 (PullUP)
 * VCC        ----- 3.3v/5v			----- 3.3v/5v		----- 3.3v/5v				----- 3.3v/5v		----- 3.3v/5v
 * GND        ----- GND				----- GND			----- GND					----- GND			----- GND
 *
 */

// With FIXED SENDER configuration
#define DESTINATION_ADDL 3

#include "Arduino.h"
#include "LoRa_E220.h"

// ---------- esp8266 pins --------------
//LoRa_E220 e220ttl(RX, TX, AUX, M0, M1);  // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
LoRa_E220 e220ttl(D3, D4, D5, D7, D6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(D2, D3); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, D5, D7, D6); // AUX M0 M1
// -------------------------------------

// ---------- Arduino pins --------------
//LoRa_E220 e220ttl(4, 5, 3, 7, 6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(4, 5); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(4, 5); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, 3, 7, 6); // AUX M0 M1
// -------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
// LoRa_E220 e220ttl(&Serial1, 2, 4, 6); //  RX AUX M0 M1
// -------------------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
// LoRa_E220 e220ttl(&Serial1, 0, 2, 4); //  RX AUX M0 M1
// -------------------------------------------------

// ---------- esp32 pins --------------
// LoRa_E220 e220ttl(&Serial2, 15, 21, 19); //  RX AUX M0 M1

//LoRa_E220 e220ttl(&Serial2, 22, 4, 18, 21, 19, UART_BPS_RATE_9600); //  esp32 RX <-- e220 TX, esp32 TX --> e220 RX AUX M0 M1
// -------------------------------------

void setup() {
  Serial.begin(9600);
  delay(500);

  // Startup all pins and UART
  e220ttl.begin();


  e220ttl.setMode(MODE_1_WOR_TRANSMITTER);

  Serial.println("Hi, I'm going to send WOR message!");

  // Send message
  ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, "Hello, world? WOR!");
  // Check If there is some problem of succesfully send
  Serial.println(rs.getResponseDescription());

//  e220ttl.setMode(MODE_0_NORMAL);
}

void loop() {
	// If something available
  if (e220ttl.available()>1) {
	  // read the String message
	ResponseContainer rc = e220ttl.receiveMessage();
	// Is something goes wrong print error
	if (rc.status.code!=1){
		Serial.println(rc.status.getResponseDescription());
	}else{
		// Print the data received
		Serial.println(rc.status.getResponseDescription());
		Serial.println(rc.data);
	}
  }
  if (Serial.available()) {
	String input = Serial.readString();
	ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, input);
	// Check If there is some problem of succesfully send
	Serial.println(rs.getResponseDescription());
  }
}


Now the receiver configuration:

	configuration.ADDL = 0x03;
	configuration.ADDH = 0x00;

	configuration.CHAN = 23;

	configuration.SPED.uartBaudRate = UART_BPS_9600;
	configuration.SPED.airDataRate = AIR_DATA_RATE_010_24;
	configuration.SPED.uartParity = MODE_00_8N1;

	configuration.OPTION.subPacketSetting = SPS_200_00;
	configuration.OPTION.RSSIAmbientNoise = RSSI_AMBIENT_NOISE_DISABLED;
	configuration.OPTION.transmissionPower = POWER_22;

	configuration.TRANSMISSION_MODE.enableRSSI = RSSI_DISABLED;
	configuration.TRANSMISSION_MODE.fixedTransmission = FT_FIXED_TRANSMISSION;
	configuration.TRANSMISSION_MODE.enableLBT = LBT_DISABLED;
	configuration.TRANSMISSION_MODE.WORPeriod = WOR_2000_011;
Arduino Nano 33 IoT on breadboard with Ebyte LoRa E22

Sketch to wake from WOR

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

/*
 * EBYTE LoRa E220
 * Stay in sleep mode and wait a wake up WOR message
 *
 * You must configure the address with 0 2 23 (FIXED RECEIVER configuration)
 * and pay attention that WOR period must be the same of sender
 *
 * You must uncommend the correct constructor and set the correct AUX_PIN define.
 *
 * by Renzo Mischianti <https://mischianti.org>
 *
 * https://mischianti.org
 *
 * E220		  ----- WeMos D1 mini	----- esp32			----- Arduino Nano 33 IoT	----- Arduino MKR	----- ArduinoUNO
 * M0         ----- D7 (or GND)		----- 19 (or GND)	----- 2 (or GND)			----- 2 (or GND)	----- 7 Volt div (or GND)
 * M1         ----- D6 (or 3.3v)	----- 21 (or 3.3v)	----- 4 (or 3.3v)			----- 4 (or 3.3v)	----- 6 Volt div (or 3.3v)
 * TX         ----- D3 (PullUP)		----- TX2 (PullUP)	----- TX1 (PullUP)			----- 14 (PullUP)	----- 4 (PullUP)
 * RX         ----- D4 (PullUP)		----- RX2 (PullUP)	----- RX1 (PullUP)			----- 13 (PullUP)	----- 5 Volt div (PullUP)
 * AUX        ----- D5 (PullUP)		----- 18  (PullUP)	----- 0  (PullUP)			----- 0  (PullUP)	----- 3 (PullUP)
 * VCC        ----- 3.3v/5v			----- 3.3v/5v		----- 3.3v/5v				----- 3.3v/5v		----- 3.3v/5v
 * GND        ----- GND				----- GND			----- GND					----- GND			----- GND
 *
 */

// With FIXED RECEIVER configuration
#define DESTINATION_ADDL 2

#define AUX_PIN 15

#include "Arduino.h"
#include "LoRa_E220.h"

// ---------- esp8266 pins --------------
//LoRa_E220 e220ttl(RX, TX, AUX, M0, M1);  // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(D3, D4, D5, D7, D6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(D2, D3); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, D5, D7, D6); // AUX M0 M1
// -------------------------------------

// ---------- Arduino pins --------------
//LoRa_E220 e220ttl(4, 5, 3, 7, 6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(4, 5); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(4, 5); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, 3, 7, 6); // AUX M0 M1
// -------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
// LoRa_E220 e220ttl(&Serial1, 2, 4, 6); //  RX AUX M0 M1
// -------------------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
// LoRa_E220 e220ttl(&Serial1, 0, 2, 4); //  RX AUX M0 M1
// -------------------------------------------------

// ---------- esp32 pins --------------
 LoRa_E220 e220ttl(&Serial2, 15, 21, 19); //  RX AUX M0 M1

//LoRa_E220 e220ttl(&Serial2, 22, 4, 18, 21, 19, UART_BPS_RATE_9600); //  esp32 RX <-- e220 TX, esp32 TX --> e220 RX AUX M0 M1
// -------------------------------------

 bool interruptExecuted = false;
#ifdef ESP32
 void IRAM_ATTR wakeUp() {
	 // Do not use Serial on interrupt callback
	 interruptExecuted = true;
	 detachInterrupt(AUX_PIN);
 }
#elif define(ESP8266)
 ICACHE_RAM_ATTR void wakeUp() {
	 // Do not use Serial on interrupt callback
	 interruptExecuted = true;
	 detachInterrupt(AUX_PIN);
 }
#else
 void wakeUp() {
 	 // Do not use Serial on interrupt callback
 	 interruptExecuted = true;
 	 detachInterrupt(AUX_PIN);
  }
#endif

//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);

	e220ttl.begin();

	e220ttl.setMode(MODE_2_WOR_RECEIVER);

	delay(1000);
	Serial.println();
	Serial.println("Start sleep!");
	delay(100);
    attachInterrupt(AUX_PIN, wakeUp, FALLING);
}

// The loop function is called in an endless loop
void loop() {
	if (e220ttl.available() > 1) {
		Serial.println("Message arrived!");
		ResponseContainer rs = e220ttl.receiveMessage();
		// First of all get the data
		String message = rs.data;

		Serial.println(rs.status.getResponseDescription());
		Serial.println(message);

		// Work only with full connection
		e220ttl.setMode(MODE_0_NORMAL);

		delay(1000);

		ResponseStatus rsSend = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, "We have received the message!");
		// Check If there is some problem of succesfully send
		Serial.println(rsSend.getResponseDescription());
	}

	if(interruptExecuted) {
		Serial.println("WakeUp Callback, AUX pin go LOW and start receive message!");
		Serial.flush();
		interruptExecuted = false;
	}
}

Pay attention to this receiver sketch only the LoRa E220 go in sleep mode, so in a real example, you must manage the sleep of the microcontroller. In the following articles, we will check the deep sleep of every microcontroller with detailed examples.

Send complex structure

To send and receive, you must set the normal mode; if you make a complete connection, the library does the correct setting automatically.

Wiring in normal mode

Ebyte LoRa E32 E22 E220 Arduino UNO normal mode breadboard
M0GND (Set normal mode)
M1GND (Set normal mode)
TXPIN 2 (PullUP 4,7KΩ)
RXPIN 3 (PullUP 4,7KΩ & Voltage divider)
AUXNot connected (PullUP 4,7KΩ)
VCC5v
GNDGND

and this configuration for Wemos D1 mini:

Ebyte LoRa E32 E22 E220 Wemos D1 normal mode breadboard
M0GND (Set normal mode)
M1GND (Set normal mode)
TXPIN D2 (PullUP 4,7KΩ)
RXPIN D3 (PullUP 4,7KΩ)
AUXNot connected (PullUP 4,7KΩ)
VCC3.3v/5v
GNDGND

ESP-32:

Ebyte LoRa E32 E22 E220 ESP32 DEV KIT V1 normal mode breadboard
M0GND (Set normal mode)
M1GND (Set normal mode)
RXTX2 (PullUP 4,7KΩ)
TXRX2 (PullUP 4,7KΩ)
AUXNot connected (PullUP 4,7KΩ)
VCC3.3v/5v
GNDGND

Arduino MKR WiFi 1010:

Ebyte LoRa Exx Arduino MKR WiFi 1010 normal mode connected breadboard

We can use string as we want, use It as JSON format, and so on, but if you’re going to use structured messages in native mode (byte array), It’s more .

Code

The example code for the sender can be:

struct Message {
	char type[5];
	char message[8];
	float temperature;
};

  struct	Message message = {"TEMP", ROOM, 19.2};

  // Send message
  ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(Message));
  // Check If there is some problem of succesfully send
  Serial.println(rs.getResponseDescription());

and to receive:

	// If something available
  if (e220ttl.available()>1) {
	  // read the String message
#ifdef ENABLE_RSSI
		ResponseStructContainer rsc = e220ttl.receiveMessageRSSI(sizeof(Message));
#else
		ResponseStructContainer rsc = e220ttl.receiveMessage(sizeof(Message));
#endif

	// Is something goes wrong print error
	if (rsc.status.code!=1){
		Serial.println(rsc.status.getResponseDescription());
	}else{
		// Print the data received
		Serial.println(rsc.status.getResponseDescription());
		struct Message message = *(Message*) rsc.data;
		Serial.println(message.type);
		Serial.println(message.message);
		Serial.println(message.temperature);

#ifdef ENABLE_RSSI
		Serial.print("RSSI: "); Serial.println(rsc.rssi, DEC);
#endif
	}
  }

To manage Sender and Receiver, you can use the FIXED SENDER and FIXED RECEIVER configuration:

Sender:

//	----------------------- FIXED SENDER -----------------------
	configuration.ADDL = 0x02;
	configuration.ADDH = 0x00;

	configuration.CHAN = 23;

	configuration.SPED.uartBaudRate = UART_BPS_9600;
	configuration.SPED.airDataRate = AIR_DATA_RATE_010_24;
	configuration.SPED.uartParity = MODE_00_8N1;

	configuration.OPTION.subPacketSetting = SPS_200_00;
	configuration.OPTION.RSSIAmbientNoise = RSSI_AMBIENT_NOISE_DISABLED;
	configuration.OPTION.transmissionPower = POWER_22;

	configuration.TRANSMISSION_MODE.enableRSSI = RSSI_DISABLED;
	configuration.TRANSMISSION_MODE.fixedTransmission = FT_FIXED_TRANSMISSION;
	configuration.TRANSMISSION_MODE.enableLBT = LBT_DISABLED;
	configuration.TRANSMISSION_MODE.WORPeriod = WOR_2000_011;

And you must uncomment the relative define in the sketch:

// With FIXED SENDER configuration
#define DESTINATION_ADDL 3
#define ROOM "Kitchen"

Receiver:

//	----------------------- FIXED RECEIVER -----------------------
	configuration.ADDL = 0x03;
	configuration.ADDH = 0x00;

	configuration.CHAN = 23;

	configuration.SPED.uartBaudRate = UART_BPS_9600;
	configuration.SPED.airDataRate = AIR_DATA_RATE_010_24;
	configuration.SPED.uartParity = MODE_00_8N1;

	configuration.OPTION.subPacketSetting = SPS_200_00;
	configuration.OPTION.RSSIAmbientNoise = RSSI_AMBIENT_NOISE_DISABLED;
	configuration.OPTION.transmissionPower = POWER_22;

	configuration.TRANSMISSION_MODE.enableRSSI = RSSI_DISABLED;
	configuration.TRANSMISSION_MODE.fixedTransmission = FT_FIXED_TRANSMISSION;
	configuration.TRANSMISSION_MODE.enableLBT = LBT_DISABLED;
	configuration.TRANSMISSION_MODE.WORPeriod = WOR_2000_011;

And you must uncomment the relative define in the sketch

// With FIXED RECEIVER configuration
#define DESTINATION_ADDL 2
#define ROOM "Bathroo"

Complete sender and receiver sketch:

/*
 * EBYTE LoRa E220
 * send a structured message to the device that have ADDH ADDL CHAN -> 0 DESTINATION_ADDL 23
 *
 * write the int humidity value on serial (or reset device that send default message)
 *
 * You must configure 2 device: one as SENDER (with FIXED SENDER config) and uncomment the relative
 * define with the correct DESTINATION_ADDL, and one as RECEIVER (with FIXED RECEIVER config)
 * and uncomment the relative define with the correct DESTINATION_ADDL.
 *
 * Write a string on serial monitor or reset to resend default value.
 *
 * Pay attention e220 support RSSI, if you want use that functionality you must enable RSSI on configuration
 * configuration.TRANSMISSION_MODE.enableRSSI = RSSI_ENABLED;
 *
 * and uncomment #define ENABLE_RSSI true in this sketch
 *
 * You must uncommend the correct constructor and set the correct AUX_PIN define.
 *
 * by Renzo Mischianti <https://mischianti.org>
 *
 * https://mischianti.org
 *
 * E220		  ----- WeMos D1 mini	----- esp32			----- Arduino Nano 33 IoT	----- Arduino MKR	----- ArduinoUNO
 * M0         ----- D7 (or GND)		----- 19 (or GND)	----- 4 (or GND)			----- 2 (or GND)	----- 7 Volt div (or GND)
 * M1         ----- D6 (or GND)		----- 21 (or GND)	----- 6 (or GND)			----- 4 (or GND)	----- 6 Volt div (or GND)
 * TX         ----- D3 (PullUP)		----- TX2 (PullUP)	----- TX1 (PullUP)			----- 14 (PullUP)	----- 4 (PullUP)
 * RX         ----- D4 (PullUP)		----- RX2 (PullUP)	----- RX1 (PullUP)			----- 13 (PullUP)	----- 5 Volt div (PullUP)
 * AUX        ----- D5 (PullUP)		----- 18  (PullUP)	----- 2  (PullUP)			----- 0  (PullUP)	----- 3 (PullUP)
 * VCC        ----- 3.3v/5v			----- 3.3v/5v		----- 3.3v/5v				----- 3.3v/5v		----- 3.3v/5v
 * GND        ----- GND				----- GND			----- GND					----- GND			----- GND
 *
 */

// With FIXED SENDER configuration
#define DESTINATION_ADDL 3
#define ROOM "Kitchen"

// With FIXED RECEIVER configuration
//#define DESTINATION_ADDL 2
//#define ROOM "Bath"

// If you want use RSSI uncomment //#define ENABLE_RSSI true
// and use relative configuration with RSSI enabled
// #define ENABLE_RSSI true

#include "Arduino.h"
#include "LoRa_E220.h"

// ---------- esp8266 pins --------------
//LoRa_E220 e220ttl(RX, TX, AUX, M0, M1);  // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(D3, D4, D5, D7, D6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(D2, D3); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, D5, D7, D6); // AUX M0 M1
// -------------------------------------

// ---------- Arduino pins --------------
//LoRa_E220 e220ttl(4, 5, 3, 7, 6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(4, 5); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(4, 5); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, 3, 7, 6); // AUX M0 M1
// -------------------------------------

// ------------- Arduino Nano 33 IoT -------------
// LoRa_E220 e220ttl(&Serial1, 2, 4, 6); //  RX AUX M0 M1
// -------------------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
// LoRa_E220 e220ttl(&Serial1, 0, 2, 4); //  RX AUX M0 M1
// -------------------------------------------------

// ---------- esp32 pins --------------
 LoRa_E220 e220ttl(&Serial2, 15, 21, 19); //  RX AUX M0 M1

//LoRa_E220 e220ttl(&Serial2, 22, 4, 18, 21, 19, UART_BPS_RATE_9600); //  esp32 RX <-- e220 TX, esp32 TX --> e220 RX AUX M0 M1
// -------------------------------------

struct Message {
	char type[5];
	char message[8];
	float temperature;
};

void setup() {
  Serial.begin(9600);
  delay(500);

  // Startup all pins and UART
  e220ttl.begin();

  Serial.println("Hi, I'm going to send message!");

  struct	Message message = {"TEMP", ROOM, 19.2};

  // Send message
  ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(Message));
  // Check If there is some problem of succesfully send
  Serial.println(rs.getResponseDescription());
}

void loop() {
	// If something available
  if (e220ttl.available()>1) {
	  // read the String message
#ifdef ENABLE_RSSI
		ResponseStructContainer rsc = e220ttl.receiveMessageRSSI(sizeof(Message));
#else
		ResponseStructContainer rsc = e220ttl.receiveMessage(sizeof(Message));
#endif

	// Is something goes wrong print error
	if (rsc.status.code!=1){
		Serial.println(rsc.status.getResponseDescription());
	}else{
		// Print the data received
		Serial.println(rsc.status.getResponseDescription());
		struct Message message = *(Message*) rsc.data;
		Serial.println(message.type);
		Serial.println(message.message);
		Serial.println(message.temperature);

#ifdef ENABLE_RSSI
		Serial.print("RSSI: "); Serial.println(rsc.rssi, DEC);
#endif
	}
  }
  if (Serial.available()) {
	  struct Message message = { "TEMP", ROOM, 0 };
	  message.temperature = Serial.parseFloat();

	  // Send message
	  ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(Message));
	  // Check If there is some problem of succesfully send
	  Serial.println(rs.getResponseDescription());
  }
}

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

esp32 DOIT DEV KIT v1 EByte LoRa E32 shield main

Read a piece of structure

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

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

With this information, we can create specified structures from different devices, for example:

struct MessageTemperature {
	char type[5];
	char message[8];
	byte temperature[4];
};

[...]

	struct MessageTemperature messageT = { "TEMP", ROOM, 0 };
	*(float*)(messageT.temperature) = 19.2;

	// Send message
	ResponseStatus rsT = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &messageT, sizeof(MessageTemperature));
	// Check If there is some problem of succesfully send
	Serial.println(rsT.getResponseDescription());

or

struct MessageHumidity {
	char type[5];
	char message[8];
	byte humidity;
};

[...]

	struct MessageHumidity message = { "HUMI", ROOM, 80 };
	// Send message
	ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(MessageHumidity));
	// Check If there is some problem of succesfully send
	Serial.println(rs.getResponseDescription());

And so you can load the specified structure from the receiver:

	// If something available
	if (e220ttl.available() > 1) {
		// read the String message
		char type[5]; // first part of structure
		ResponseContainer rs = e220ttl.receiveInitialMessage(sizeof(type));
		String typeStr = rs.data;

		// Is something goes wrong print error
		if (rs.status.code != 1) {
			Serial.println(rs.status.getResponseDescription());
		} else {
			Serial.println(typeStr);
			if (typeStr == "TEMP") {
				struct MessageTemperaturePartial {
					char message[8];
					byte temperature[4];
				};

				ResponseStructContainer rsc = e220ttl.receiveMessage( sizeof(MessageTemperaturePartial));
				struct MessageTemperaturePartial message = *(MessageTemperaturePartial*) rsc.data;

				Serial.println(*(float*)(message.temperature));
				Serial.println(message.message);
				rsc.close();
			} else if (typeStr == "HUMI") {
				struct MessageHumidityPartial {
					char message[8];
					byte humidity;
				};

				ResponseStructContainer rsc = e220ttl.receiveMessage(sizeof(MessageHumidityPartial));
				struct MessageHumidityPartial message = *(MessageHumidityPartial*) rsc.data;

				Serial.println(message.humidity);
				Serial.println(message.message);
				rsc.close();
			} else {
				Serial.println("Something goes wrong!!");
			}
		}
	}

Here sender sketches that send humidity and temperature, read the first part, the type, and execute the correct code to retrieve the remaining data:

/*
 * EBYTE LoRa E220
 * send a structured message to the device that have ADDH ADDL CHAN -> 0 DESTINATION_ADDL 23
 *
 * The receiver read the first part of the packet and undestand the type.
 * If the type is HUMI read the message and the humidity as int
 * else read the temperature as float.
 * I use byte array because some microcontroller can have different size for float
 *
 * You must configure 2 device: one as SENDER (with FIXED SENDER config) and uncomment the relative
 * define with the correct DESTINATION_ADDL, and one as RECEIVER (with FIXED RECEIVER config)
 * and uncomment the relative define with the correct DESTINATION_ADDL.
 *
 * Write a string on serial monitor or reset to resend default value.
 *
 * You must uncommend the correct constructor and set the correct AUX_PIN define.
 *
 * by Renzo Mischianti <https://mischianti.org>
 *
 * https://mischianti.org
 *
 * E220		  ----- WeMos D1 mini	----- esp32			----- Arduino Nano 33 IoT	----- Arduino MKR	----- ArduinoUNO
 * M0         ----- D7 (or GND)		----- 19 (or GND)	----- 4 (or GND)			----- 2 (or GND)	----- 7 Volt div (or GND)
 * M1         ----- D6 (or GND)		----- 21 (or GND)	----- 6 (or GND)			----- 4 (or GND)	----- 6 Volt div (or GND)
 * TX         ----- D3 (PullUP)		----- TX2 (PullUP)	----- TX1 (PullUP)			----- 14 (PullUP)	----- 4 (PullUP)
 * RX         ----- D4 (PullUP)		----- RX2 (PullUP)	----- RX1 (PullUP)			----- 13 (PullUP)	----- 5 Volt div (PullUP)
 * AUX        ----- D5 (PullUP)		----- 18  (PullUP)	----- 2  (PullUP)			----- 0  (PullUP)	----- 3 (PullUP)
 * VCC        ----- 3.3v/5v			----- 3.3v/5v		----- 3.3v/5v				----- 3.3v/5v		----- 3.3v/5v
 * GND        ----- GND				----- GND			----- GND					----- GND			----- GND
 *
 */

#define MESSAGE_TYPE "HUMI"

// With FIXED SENDER configuration
//#define DESTINATION_ADDL 3
//#define ROOM "Kitchen"

// With FIXED RECEIVER configuration
#define DESTINATION_ADDL 2
#define ROOM "Bathroo"

// If you want use RSSI uncomment //#define ENABLE_RSSI true
// and use relative configuration with RSSI enabled
// #define ENABLE_RSSI true

#include "Arduino.h"
#include "LoRa_E220.h"

// ---------- esp8266 pins --------------
//LoRa_E220 e220ttl(RX, TX, AUX, M0, M1);  // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(D3, D4, D5, D7, D6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(D2, D3); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(D2, D3); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, D5, D7, D6); // AUX M0 M1
// -------------------------------------

// ---------- Arduino pins --------------
//LoRa_E220 e220ttl(4, 5, 3, 7, 6); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX AUX M0 M1
//LoRa_E220 e220ttl(4, 5); // Config without connect AUX and M0 M1

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(4, 5); // Arduino RX <-- e220 TX, Arduino TX --> e220 RX
//LoRa_E220 e220ttl(&mySerial, 3, 7, 6); // AUX M0 M1
// -------------------------------------

// ------------- Arduino Nano 33 IoT -------------
// LoRa_E220 e220ttl(&Serial1, 2, 4, 6); //  RX AUX M0 M1
// -------------------------------------------------

// ------------- Arduino MKR WiFi 1010 -------------
 LoRa_E220 e220ttl(&Serial1, 0, 2, 4); //  RX AUX M0 M1
// -------------------------------------------------

// ---------- esp32 pins --------------
// LoRa_E220 e220ttl(&Serial2, 15, 21, 19); //  RX AUX M0 M1

//LoRa_E220 e220ttl(&Serial2, 22, 4, 18, 21, 19, UART_BPS_RATE_9600); //  esp32 RX <-- e220 TX, esp32 TX --> e220 RX AUX M0 M1
// -------------------------------------

struct MessageTemperature {
	char type[5];
	char message[8];
	byte temperature[4];
};

struct MessageHumidity {
	char type[5];
	char message[8];
	byte humidity;
};

void setup() {
	Serial.begin(9600);
	delay(500);

	// Startup all pins and UART
	e220ttl.begin();

	Serial.println("Hi, I'm going to send message!");

	struct MessageHumidity message = { "HUMI", ROOM, 80 };
	// Send message
	ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(MessageHumidity));
	// Check If there is some problem of succesfully send
	Serial.println(rs.getResponseDescription());

	struct MessageTemperature messageT = { "TEMP", ROOM, 0 };
	*(float*)(messageT.temperature) = 19.2;

	// Send message
	ResponseStatus rsT = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &messageT, sizeof(MessageTemperature));
	// Check If there is some problem of succesfully send
	Serial.println(rsT.getResponseDescription());
}

void loop() {
	// If something available
	if (e220ttl.available() > 1) {
		// read the String message
		char type[5]; // first part of structure
		ResponseContainer rs = e220ttl.receiveInitialMessage(sizeof(type));
		String typeStr = rs.data;

		// Is something goes wrong print error
		if (rs.status.code != 1) {
			Serial.println(rs.status.getResponseDescription());
		} else {
			Serial.println(typeStr);
			if (typeStr == "TEMP") {
				struct MessageTemperaturePartial {
					char message[8];
					byte temperature[4];
				};

				ResponseStructContainer rsc = e220ttl.receiveMessage( sizeof(MessageTemperaturePartial));
				struct MessageTemperaturePartial message = *(MessageTemperaturePartial*) rsc.data;

				Serial.println(*(float*)(message.temperature));
				Serial.println(message.message);
				rsc.close();
			} else if (typeStr == "HUMI") {
				struct MessageHumidityPartial {
					char message[8];
					byte humidity;
				};

				ResponseStructContainer rsc = e220ttl.receiveMessage(sizeof(MessageHumidityPartial));
				struct MessageHumidityPartial message = *(MessageHumidityPartial*) rsc.data;

				Serial.println(message.humidity);
				Serial.println(message.message);
				rsc.close();
			} else {
				Serial.println("Something goes wrong!!");
			}
		}
	}
 	if (Serial.available()) {
 		if (MESSAGE_TYPE == "HUMI") {
 			struct MessageHumidity message = { "HUMI", ROOM, 0 };
 			message.humidity = Serial.parseInt();

 	 		// Send message
 	 		ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(MessageHumidity));
 	 		// Check If there is some problem of succesfully send
 	 		Serial.println(rs.getResponseDescription());
 		} else {
 			struct MessageTemperature message = { "TEMP", ROOM, 0 };
 			*(float*)(message.temperature) = Serial.parseFloat();

 	 		// Send message
 	 		ResponseStatus rs = e220ttl.sendFixedMessage(0, DESTINATION_ADDL, 23, &message, sizeof(MessageTemperature));
 	 		// Check If there is some problem of succesfully send
 	 		Serial.println(rs.getResponseDescription());
 		}
 	}
}

Thanks

And this was the last part of my little tutorial. Next, we will examine the wake-up of the microcontroller in detail, not only the LoRa device.

  1. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: settings and basic usage
  2. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: library
  3. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: configuration
  4. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: fixed transmission, broadcast, monitor, and RSSI
  5. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: power-saving and sending structured data
  6. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: WOR microcontroller and Arduino shield
  7. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: WOR microcontroller and WeMos D1 shield
  8. Ebyte LoRa E220 device for Arduino, esp32 or esp8266: WOR microcontroller and esp32 dev v1 shield

Github library


Spread the love
Exit mobile version