Lecture 2.3: Setting Up Point-to-Point Communication

Introduction

Good morning, everyone! It’s great to see all of you back and eager to continue our exploration of LoRa technology and LilyGO devices. In our previous lectures, we got hands-on experience with both the LilyGO T-Beam and the T-Deck, learning how to set them up and configure their basic features. Today, we’re going to bring these devices together to establish a point-to-point communication link.

Imagine being able to send data wirelessly over several kilometers without relying on existing network infrastructure. Whether it’s transmitting sensor data from a remote location, enabling communication in areas without cellular coverage, or simply experimenting with wireless technology, point-to-point communication with LoRa opens up a world of possibilities.

By the end of this lecture, you’ll have configured two LilyGO devices to communicate directly with each other, tested the data transmission, and validated the integrity of the communication link. This is a foundational skill that will serve as a building block for more complex network topologies we’ll explore later in the course.

 

Section 1: Understanding Point-to-Point Communication

Before we dive into the practical setup, let’s take a moment to understand what **point-to-point communication** entails in the context of LoRa technology.

What is Point-to-Point Communication?

In its simplest form, point-to-point (P2P) communication involves a direct link between two devices, allowing them to send and receive data exclusively with each other. Unlike networked communication models where multiple devices communicate through a central hub or gateway, P2P is a straightforward connection with minimal overhead.

Advantages of P2P Communication with LoRa:

Long-Range Connectivity: LoRa’s modulation scheme enables communication over distances of several kilometers.
Low Power Consumption: Ideal for battery-powered devices that need to operate over extended periods.
Simplicity: Easier to set up and manage compared to more complex network architectures.
Infrastructure Independence: No need for gateways, servers, or internet connectivity.

Use Cases:

– Remote sensor monitoring (e.g., weather stations, environmental sensors).
– Off-grid communication (e.g., in wilderness areas or disaster zones).
– Simple data logging between two points (e.g., from a device to a local receiver).

 

Section 2: Preparing Your LilyGO Devices

Gathering the Necessary Equipment

For this exercise, you will need:

1. Two LilyGO Devices:

– Any combination of T-Beams and T-Decks will work. For this lecture, we’ll refer to them as Device A and Device B.

2. LoRa Antennas:

– Ensure both devices have their LoRa antennas properly connected.

3. USB Cables:

– For powering the devices and uploading code.

4. Computers:

– Two computers are ideal but not necessary. You can program both devices from one computer if needed.

5. Development Environment:

Arduino IDE installed and configured with ESP32 support, as we’ve done in previous lectures.

Safety Precautions

– Antenna Connection:

– Always ensure the LoRa antenna is connected before powering on the device. Transmitting without an antenna can damage the LoRa module.

– Power Supply:

– Use quality USB cables and power sources to prevent power fluctuations.

 

Section 3: Configuring Device A as the Sender

Let’s start by configuring Device A to act as the sender.

Step 1: Setting Up the Arduino IDE

1. Open the Arduino IDE.

2. Select the Correct Board:

– Go to Tools > Board and select “ESP32 Dev Module” or the specific LilyGO board if listed.

3. Select the Correct Port:

– Under Tools > Port, choose the COM port associated with Device A.

Step 2: Installing the LoRa Library

1. Install the LoRa Library:

– Navigate to Sketch > Include Library > Manage Libraries.
– Search for “LoRa” by Sandeep Mistry.
– Click Install.

Step 3: Writing the Sender Code

Let’s write a simple sketch that sends a message every few seconds.

Code Explanation:

– Include Libraries:

“`cpp
#include <LoRa.h>
“`

– Define LoRa Pins:

– The pins used by the LoRa module might differ between devices. Common pin configurations for LilyGO devices are:

“`cpp
#define LORA_SCK 5
#define LORA_MISO 19
#define LORA_MOSI 27
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DI0 26
“`

– Set Frequency:

– Use the frequency appropriate for your region:

868E6 for Europe.
915E6 for North America.

Complete Sender Sketch:

“`cpp
#include <SPI.h>
#include <LoRa.h>

#define LORA_SCK 5
#define LORA_MISO 19
#define LORA_MOSI 27
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DI0 26

int counter = 0;

void setup() {
Serial.begin(115200);
while (!Serial);

Serial.println(“LoRa Sender”);

// Override the default LoRa pins with the ones for LilyGO devices
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DI0);

// Initialize LoRa module
if (!LoRa.begin(915E6)) { // Replace with your frequency
Serial.println(“Starting LoRa failed!”);
while (1);
}
}

void loop() {
Serial.print(“Sending packet: “);
Serial.println(counter);

// Send packet
LoRa.beginPacket();
LoRa.print(“Hello “);
LoRa.print(counter);
LoRa.endPacket();

counter++;
delay(5000); // Send a packet every 5 seconds
}
“`

Step 4: Uploading the Code

1. Connect Device A to your computer via USB.

2. Verify and Upload the Sketch:

– Click on the Verify button to compile the code.
– Click on the Upload button to upload the code to Device A.

3. Monitor Serial Output:

– Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 115200.
– You should see messages indicating packets are being sent.

 

Section 4: Configuring Device B as the Receiver

Now, let’s configure Device B to act as the receiver.

Step 1: Setting Up the Arduino IDE

1. Open a New Instance of the Arduino IDE (or a new window).

2. Select the Correct Board and Port:

– Choose the appropriate board under Tools > Board.
– Select the COM port associated with Device B under Tools > Port.

Step 2: Writing the Receiver Code

We’ll write a sketch that listens for incoming packets and displays them.

Complete Receiver Sketch:

“`cpp
#include <SPI.h>
#include <LoRa.h>

#define LORA_SCK 5
#define LORA_MISO 19
#define LORA_MOSI 27
#define LORA_SS 18
#define LORA_RST 14
#define LORA_DI0 26

void setup() {
Serial.begin(115200);
while (!Serial);

Serial.println(“LoRa Receiver”);

// Override the default LoRa pins with the ones for LilyGO devices
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DI0);

// Initialize LoRa module
if (!LoRa.begin(915E6)) { // Replace with your frequency
Serial.println(“Starting LoRa failed!”);
while (1);
}
}

void loop() {
// Try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// Received a packet
Serial.print(“Received packet: “);

// Read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}

// Print RSSI (Received Signal Strength Indicator)
Serial.print(” with RSSI “);
Serial.println(LoRa.packetRssi());
}
}
“`

Step 3: Uploading the Code

1. Connect Device B to your computer via USB.
2. Verify and Upload the Sketch:

– Compile and upload the receiver sketch to Device B.

3. Monitor Serial Output:

– Open the Serial Monitor and set the baud rate to 115200.
– You should see messages indicating packets are being received.

 

Section 5: Testing and Validating Data Transmission

With both devices configured, let’s test the communication link.

Step 1: Positioning the Devices

– Initial Test:

– Keep the devices close together (within a few meters) to ensure they can communicate without interference.

– Extended Range Test:

– Gradually increase the distance between the devices to test the range capabilities.

Step 2: Observing the Output

– On Device A (Sender):

– The Serial Monitor should display messages like:

“`
Sending packet: 0
Sending packet: 1
Sending packet: 2
“`

– On Device B (Receiver):

– Corresponding messages should appear:

“`
Received packet: Hello 0 with RSSI -50
Received packet: Hello 1 with RSSI -52
Received packet: Hello 2 with RSSI -51
“`

Step 3: Interpreting RSSI Values

– RSSI (Received Signal Strength Indicator):

– Indicates the signal strength of the received packet.
– Values are negative; closer to zero means stronger signal.
-40 to -70 dBm: Strong signal.
– –70 to -100 dBm: Moderate signal.
Below -120 dBm: Weak signal, may experience data loss.

Step 4: Troubleshooting Communication Issues

– No Packets Received:

– Ensure both devices are set to the same frequency.
– Check that antennas are properly connected.
– Verify that the LoRa pins are correctly defined in the code.

– Poor Signal Strength:

– Check for physical obstructions between devices.
– Try repositioning the antennas for better line-of-sight.

 

Section 6: Enhancing the Communication Link

Bidirectional Communication

Let’s modify the sketches to enable two-way communication, allowing both devices to send and receive messages.

Modifying the Sender Sketch (Device A):

1. Add Packet Reception Logic:

“`cpp
void loop() {
Serial.print(“Sending packet: “);
Serial.println(counter);

// Send packet
LoRa.beginPacket();
LoRa.print(“Hello “);
LoRa.print(counter);
LoRa.endPacket();

// Wait for a reply for a short period
long waitTime = millis() + 1000;
while (millis() < waitTime) {
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.print(“Received reply: “);
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
Serial.print(” with RSSI “);
Serial.println(LoRa.packetRssi());
break;
}
}

counter++;
delay(5000);
}
“`

Modifying the Receiver Sketch (Device B):

1. Send a Reply Upon Receiving a Packet:

“`cpp
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
// Received a packet
Serial.print(“Received packet: “);

String incoming = “”;
while (LoRa.available()) {
char c = (char)LoRa.read();
Serial.print(c);
incoming += c;
}

Serial.print(” with RSSI “);
Serial.println(LoRa.packetRssi());

// Send a reply
LoRa.beginPacket();
LoRa.print(“Ack: “);
LoRa.print(incoming);
LoRa.endPacket();
}
}
“`

Testing Bidirectional Communication

Device A should now display any replies received from **Device B**.
Device B will send an acknowledgment (“Ack”) back to **Device A** upon receiving a packet.

Observing the Output

Device A:

“`
Sending packet: 0
Received reply: Ack: Hello 0 with RSSI -50
“`

Device B:

“`
Received packet: Hello 0 with RSSI -50
“`

 

Section 7: Validating Data Integrity

Ensuring the data transmitted is received accurately is crucial.

Implementing Checksums

Purpose: Detect any errors in data transmission.

Adding a Simple Checksum

– On Sender (Device A):

“`cpp
// Calculate checksum
int checksum = counter % 256;

// Send packet with checksum
LoRa.beginPacket();
LoRa.print(“Hello “);
LoRa.print(counter);
LoRa.print(“,checksum:”);
LoRa.print(checksum);
LoRa.endPacket();
“`

– On Receiver (Device B):

“`cpp
// Process incoming packet
String incoming = “”;
while (LoRa.available()) {
incoming += (char)LoRa.read();
}

// Extract data and checksum
int separatorIndex = incoming.indexOf(“,checksum:”);
if (separatorIndex != -1) {
String dataPart = incoming.substring(0, separatorIndex);
String checksumPart = incoming.substring(separatorIndex + 10);

int receivedChecksum = checksumPart.toInt();
int calculatedChecksum = dataPart.length() % 256;

if (receivedChecksum == calculatedChecksum) {
Serial.println(“Data integrity verified.”);
} else {
Serial.println(“Checksum mismatch. Data may be corrupted.”);
}
}
“`

Testing Data Integrity

– Introduce Errors Intentionally:

– Temporarily modify the sender code to send an incorrect checksum.
– Observe the receiver detecting the mismatch.

Advanced Error Detection

– Consider using CRC (Cyclic Redundancy Check) or other robust error-detection methods for more reliable communication.

 

Section 8: Experimenting with Communication Parameters

Adjusting LoRa parameters can optimize range and reliability.

Key Parameters:

1. Spreading Factor (SF):

– Controls the trade-off between data rate and range.
SF6 to SF12, where higher values increase range but decrease data rate.

“`cpp
LoRa.setSpreadingFactor(7); // Default is 7
“`

2. Bandwidth (BW):

– Affects data rate and resistance to interference.
– Common values: 7.8 kHz** to **500 kHz.

“`cpp
LoRa.setSignalBandwidth(125E3); // Default is 125 kHz
“`

3. Coding Rate (CR):

– Enhances error correction capabilities.
– Values range from 5 (4/5) to 8 (4/8).

“`cpp
LoRa.setCodingRate4(5); // Default is 5 (4/5)
“`

4. Transmit Power:

– Adjusts the output power of the transmitter.
– Range from 2 dBm to 20 dBm.

“`cpp
LoRa.setTxPower(14); // Default is 14 dBm
“`

Experiment Steps:

1. Adjust Parameters:

– Modify the sender and receiver sketches to set new parameters.

2. Test Range and Reliability:

– Observe how changes affect communication distance and data integrity.

3. Document Results:

– Keep a log of parameter settings and corresponding performance metrics.

Safety Note:

– Ensure that transmit power and frequency settings comply with local regulations to avoid interference with other devices and services.

 

Section 9: Troubleshooting Common Issues

Issue 1: Devices Not Communicating

– Possible Causes:

– Frequency mismatch.
– Incorrect pin configurations.
– Faulty antennas.

– Solutions:

– Verify that both devices are using the same frequency.
– Double-check pin definitions in the code.
– Test antennas by swapping them if possible.

Issue 2: Poor Signal Strength

– Possible Causes:

– Physical obstructions.
– Environmental interference.
– Low transmit power.

– Solutions:

– Move to an area with fewer obstructions.
– Adjust transmit power within legal limits.
– Change spreading factor or bandwidth settings.

Issue 3: Data Corruption

Possible Causes:

– Interference.
– Too high data rate for the distance.

– Solutions:

– Increase the spreading factor.
– Implement error detection mechanisms like checksums or CRC.

 

Section 10: Practical Applications

Let’s consider some practical scenarios where point-to-point communication is beneficial.

Scenario 1: Remote Weather Station

Setup:

Device A: Placed in a remote location, collecting temperature, humidity, and atmospheric pressure data.
Device B: Located in your home or lab, receiving and logging the data.

– Implementation:

– Connect sensors to Device A.
– Modify the sender sketch to read sensor data and transmit it.
– Adjust the receiver sketch to parse and store the data.

Scenario 2: Off-Grid Messaging

– Setup:

Two T-Deck devices configured to send and receive text messages.
– Users can input messages via buttons and display received messages on the screen.

– Implementation:

– Develop a user interface on the T-Deck’s display.
– Implement a simple protocol for message exchange.

Scenario 3: Security System Alert

– Setup:

Device A: Monitors a motion sensor in a remote area.
Device B: Receives alerts and triggers an alarm or notification.

– Implementation:

– Program Device A to send an alert when motion is detected.
– Device B processes the alert and takes appropriate action.

 

Conclusion

Today, we’ve successfully configured two LilyGO devices to communicate directly using LoRa technology. We’ve explored the basics of point-to-point communication, tested and validated data transmission, and even enhanced our setup with bidirectional communication and data integrity checks.

This foundational knowledge is crucial as we move forward in building more complex networks and implementing secure communication protocols. The ability to establish reliable links between devices is at the heart of IoT applications, and mastering these skills opens the door to endless possibilities.

 

Questions and Discussion

Let’s take some time to address any questions or experiences you’d like to share.

Question: Can I use encryption to secure the data transmitted between the devices?

Answer: Absolutely! While today’s lecture focused on establishing basic communication, implementing encryption is a critical next step for securing your data. You can use symmetric encryption algorithms like AES to encrypt the payload before transmission and decrypt it upon reception. Be mindful of key management and ensure both devices share the same encryption key securely.

Question: What is the maximum range I can achieve with these devices?

Answer: The range depends on several factors, including antenna quality, transmission power, spreading factor, environmental conditions, and regulatory limitations. Under ideal conditions, LoRa can achieve ranges of up to 15 kilometers in rural areas. In urban environments with obstacles, the range may be reduced to a few kilometers. Experimenting with different settings as we did today can help optimize range for your specific application.

Question: Can I connect more than two devices in a point-to-point setup?

Answer: In a point-to-point setup, communication is typically between two devices. However, you can implement a simple network where one sender transmits data, and multiple receivers listen on the same frequency and settings. For more complex multi-device communication, you might consider implementing a star topology or exploring mesh networking, which we’ll cover in future lectures.

 

Closing Remarks

As we conclude today’s lecture, I encourage you to continue experimenting with your devices. Try integrating sensors, modifying code, and pushing the limits of your communication setups. The hands-on experience you gain will deepen your understanding and spark ideas for innovative applications.

In our next session, we’ll delve into configuring device settings in more detail, including frequency, bandwidth, data rate, and security settings. These are essential for optimizing performance and ensuring compliance with regulations.

Thank you for your enthusiasm and active participation. I’m excited to see how you’ll apply today’s lessons in your projects.

“See you all in the next lecture!”

 

 

‘Fix the broken countries of the west through increased transparency, design and professional skills. Support Skills Gap Trainer.’


To see our Donate Page, click https://skillsgaptrainer.com/donate

To see our Twitter / X Channel, click https://x.com/SkillsGapTrain

To see our Instagram Channel, click https://www.instagram.com/skillsgaptrainer/

To see some of our Udemy Courses, click SGT Udemy Page

To see our YouTube Channel, click https://www.youtube.com/@skillsgaptrainer

Scroll to Top