Search Code

Turn On a Device from 13 mile 21km Away – The Ultimate Off-Grid LoRa Project with WiFi LoRa 32!

This lesson is part of: Introduction to WiFi LoRa

Turn On a Device from 13 mile 21km Away – The Ultimate Off-Grid LoRa Project with WiFi LoRa 32!

Imagine being able to switch on a water pump at your remote cabin, open a gate from miles away, or activate a heater in your workshop—all without a Wi-Fi connection, a cellular plan, or any monthly fees. This is the power of LoRa (Long Range) technology. In this project, we will build a secure, off-grid remote control system using the Heltec Wi-Fi LoRa 32 module. We'll create a transmitter that sends a secure signal to a receiver over an astonishing distance of 13 miles (21 km), triggering a relay to control any device you choose.

 

This guide walks you through the entire process, from assembling the hardware in a rugged case to uploading the code and performing a real-world range test. This isn't just a lab experiment; it's a practical solution for a variety of applications.

  • Remote Agriculture: Turn on irrigation pumps or greenhouse fans from a central location.
  • Property Access: Open and close a garage door or a remote gate as you approach.
  • Off-Grid Automation: Control lighting, heaters, or other appliances in a cabin, boat, or RV.
  • Security Systems: Activate or deactivate a remote security alarm or camera system.
  • Wildlife Monitoring: Trigger a camera trap or a non-lethal deterrent from a distance.

The core of this project is the Heltec Wi-Fi LoRa 32 V3 module, which we will house in a professional Meshnology N32 case with a 3000mAh battery for true portability. We'll explore the wiring for both a relay and a buzzer, explain the essential code configuration, and show you how to set up a secure link that won't interfere with other LoRa devices. The result is a self-contained, powerful remote switch that operates completely free, over distances that would make traditional Wi-Fi or Bluetooth solutions impossible.

Hardware & Components

To build this project, you will need the following components. The core items are often available as a kit from Meshnology, which simplifies the assembly process.

  • 2x Heltec Wi-Fi LoRa 32 V3 Modules: The main microcontroller with integrated LoRa, Wi-Fi, and Bluetooth. (in video at 00:23)
  • 2x Meshnology N32 Cases: A rugged, professional enclosure designed to fit the Heltec module and a large battery. (in video at 02:00)
  • 2x 3000mAh 3.7V LiPo Batteries: Provides long-lasting, portable power for off-grid operation. (in video at 02:14)
  • 1x Relay Module: A 5V relay module to switch your high-power load (e.g., a motor, light, or pump).
  • 1x Buzzer (Optional): A 5V active buzzer for audio feedback, driven by a 2N2222 transistor.
  • 1x 2N2222 Transistor (Optional): An NPN transistor used to drive the buzzer safely.
  • 1x 1kΩ Resistor (Optional): A current-limiting resistor for the transistor's base.
  • 2x High-Gain Antennas: To maximize the transmission range and receiver sensitivity. (in video at 04:35)
  • Various Wires & Connectors: For making the necessary connections.

Installing Heltec ESP32 Boards

Add this path into preferences of your Arduino IDE as shown in the video:https://resource.heltec.cn/download/package_heltec_esp32_index.json

Required Library

You must install the Robojax_HeltecLoRa32 library. Download it from the link below this article.

Install this ZIP library via Arduino IDE before uploading any code.

Wiring Guide

This project involves two separate circuits: the transmitter and the receiver. The wiring for both is straightforward. If you are using the Meshnology case, you will need to drill a small hole to route the wires for the relay or buzzer to the outside. The video demonstrates a clean assembly, but the core connections are as follows:

Transmitter: The transmitter uses the built-in user button on the Heltec module. No external wiring is needed for the control signal. Just ensure the antenna is connected before powering the device.

Receiver: The receiver's GPIO 4 pin is used to control the relay or buzzer. The wiring is shown below. Note that the relay is isolated from the ESP32, so you will need a separate power source for the relay's load circuit.

Relay Connection:

  • Connect the Heltec module's 3.3V pin to the relay module's VCC pin (or the positive input of a boost converter if you need a stable 5V).
  • Connect the Heltec module's GND pin to the relay module's GND pin.
  • Connect the Heltec module's GPIO 4 pin to the relay module's IN (signal) pin. (in video at 14:54)

Buzzer Connection (with Transistor): The ESP32's GPIO pins cannot supply enough current to drive a buzzer directly. A transistor acts as a switch to protect the microcontroller. (in video at 16:47)

  • Connect the 3.3V pin to the buzzer's positive (+) terminal.
  • Connect the buzzer's negative (-) terminal to the collector of the 2N2222 transistor.
  • Connect the emitter of the transistor to the GND pin.
  • Connect the base of the transistor to one end of the 1kΩ resistor.
  • Connect the other end of the resistor to the GPIO 4 pin.
Heltec_Wifi_LoRA_32_wiring_uzzer
Heltec_Wifi_LoRA_32_wiring_uzzer
641_Heltec_Wifi_LoRA_Rotary_Encoder_wiring
641_Heltec_Wifi_LoRA_Rotary_Encoder_wiring
617_WiFi_LoRa32_Relay_wiring_boost
617_WiFi_LoRa32_Relay_wiring_boost

Code Explanation

The code for this project is provided in the Robojax Heltec LoRa 32 library. You will need to install this library and the necessary board support packages in your Arduino IDE. The setup process is detailed in the video. Once the libraries are installed, you can open the example sketches: Relay_Secure_TX_Sample for the transmitter and Relay_Secure_RX for the receiver. (in video at 26:12)

Both the transmitter and receiver code have a few user-configurable variables at the top that you must set to ensure proper communication. These variables are the key to making the project work for your specific setup.

// USER CONFIGURABLE VARIABLES
// ============================
bool debug = false; // Set to true to enable Serial Monitor output

// Transmitter Settings
const int userPin = 0; // GPIO pin for the user button
String relayOnText = "Relay ON"; // Text shown on the display
String relayOffText = "Relay OFF"; // Text shown on the display

// Receiver Settings
const int relayPin = 4; // GPIO pin connected to the relay or transistor
int relayOnDuration = 3000; // Time in milliseconds the relay stays on for the "Sample" code

// Security Settings (MUST BE IDENTICAL ON BOTH DEVICES)
String securityKey = "your_secret_key"; // A shared secret for secure communication
String secret = "your_secret"; // Another secret for added security

// LoRa Settings (MUST BE IDENTICAL ON BOTH DEVICES)
#define LORA_FREQUENCY 915.0 // Frequency in MHz (e.g., 868.0 for Europe, 915.0 for US)
int power = 20; // Transmission power in dBm (2 to 20)
// ============================

Here is what each of these settings does:

  • debug: When set to true, the code will print detailed information to the Arduino IDE's Serial Monitor. This is useful for troubleshooting but should be set to false for normal operation to save power.
  • userPin (Transmitter): This defines the GPIO pin connected to the button you will press to send the signal. The default is 0, which is the built-in user button on the Heltec module.
  • relayPin (Receiver): This is the GPIO pin that will output the signal to control your relay or transistor. The default is 4. If you connect your relay to a different pin, you must change this value.
  • relayOnDuration (Receiver): This is used in the "Sample" code. It determines how long the relay stays on after a signal is received. For example, setting it to 1000 will keep the relay on for 1 second. This is useful for a "momentary" action like opening a door. The "Toggle" example code will ignore this and switch states on each press.
  • securityKey and secret: These are your password for the LoRa link. They must be exactly the same on both the transmitter and receiver. This prevents unauthorized users from controlling your devices. (in video at 27:19)
  • LORA_FREQUENCY: This sets the radio frequency band. It must be the same on both devices and legal in your country (e.g., 868 MHz in Europe, 915 MHz in North America). (in video at 27:36)
  • power: This sets the transmission power in dBm. A value of 2 is low power for testing, while 20 is the maximum for the longest range. (in video at 27:43)

There are three main types of transmitter code: Sample, Toggle, and Momentary. The "Sample" code turns the relay on for a set duration. The "Toggle" code switches the relay's state (on/off) with each button press. The "Momentary" code turns the relay on only while the button is held down. Choose the one that best fits your application.

Live Project & Demonstration

In the video, the creator demonstrates the project in two phases. First, a lab test shows the system working with both a relay controlling a motor and a transistor-driven buzzer. The relay and buzzer are successfully toggled on and off from the transmitter board, proving the basic functionality and the secure communication link.

The second, more impressive phase is a real-world range test. The transmitter was placed on a tripod by a lake, and the receiver was taken to a point 13.04 miles (20.98 km) away with a clear line of sight. The video shows the receiver displaying the voltage value being transmitted from the distant location, successfully receiving the data over the 21 km range. This confirms the project's ability to control devices over vast distances for free, without any existing infrastructure.

Chapters

  • [00:00] Introduction to Off-Grid LoRa Control
  • [01:01] Project Overview and Series Introduction
  • [01:57] Unboxing and Examining the Hardware
  • [04:56] Understanding the Heltec Module and Antenna
  • [07:57] Assembling the Meshnology N32 Case
  • [13:56] Wiring the Relay and Buzzer Circuits
  • [19:41] Final Assembly and Case Modification
  • [21:51] Installing Required Software and Libraries
  • [26:12] Configuring the Transmitter Code
  • [28:46] Configuring the Receiver Code
  • [30:02] Live Demonstration: Controlling a Relay and Buzzer
  • [33:06] Real-World 13-Mile Range Test and Conclusion

Images

617_WiFi_LoRa32_Relay_wiring_boost
617_WiFi_LoRa32_Relay_wiring_boost
641_Heltec_Wifi_LoRA_Rotary_Encoder_wiring
641_Heltec_Wifi_LoRA_Rotary_Encoder_wiring
Heltec_Wifi_LoRA_32_wiring_uzzer
Heltec_Wifi_LoRA_32_wiring_uzzer
Heltec_WiFi_LoRa_32_V3_N35-1-package
Heltec_WiFi_LoRa_32_V3_N35-1-package
No code attached.

Files📁

Other files