Skip to main content Microfire 







LoRa with ESPNow

A map

After writing a component for ESP-Now with ESPHome, it was time to add LoRa.


Semtech says this about LoRa:


LoRa (short for long range) is a spread spectrum modulation technique derived from chirp spread spectrum (CSS) technology. Semtech's LoRa is a long range, low power wireless platform that has become the de facto wireless platform of Internet of Things (IoT).

Currently, ESPHome doesn't have a native method to use LoRa. A simple-to-implement way to add it is to create a bridge. In this case, it will bridge a formatted LoRa transmission from a sensor node to a LoRa-MQTT bridge device.


To avoid repeating the same thing, you can read a bit more about the bridge device on the ESP-Now page. It uses the same format and works the same way.


Bonus points for being able to run this LoRa bridge concurrently with the ESP-Now bridge components. So if you wanted to use LoRa and ESP-Now sensors, you only need one bridge device.


BOM
  • 2x ESP32 boards with a LoRa radio
  • any sensor for testing, I'll be using an SHT30

1.

Make sure your MQTT broker is set up for Home Assistant. The instructions can be found in the addon inside Home Assistant. You'll probably want to follow the instructions by making a new MQTT user and configuring it as an integration. Or you can use your own and just add it as an integration. When you are done, you'll have an MQTT box in `Settings > Devices & Services`.


2.

For the bridge, have a look at the ESPHome YAML below.

1
esphome:
2
  name: esp-now-mqtt-bridge
3
esp32:
4
  board: esp32dev
5
  variant: esp32
6
  framework:
7
    type: arduino
8
logger:
9

10
external_components:
11
  - source:
12
      type: git
13
      url: https://github.com/u-fire/ESPHomeComponents/
14

15
now_mqtt_bridge:
16

17
wifi:
18
  ssid: !secret wifi_ssid
19
  password: !secret wifi_password
20

21
mqtt:
22
  id: mqtt_broker
23
  broker: homeassistant.local
24
  username: mqtt
25
  password: !secret mqtt_password


Setup the `mqtt` for your broker. You can change the Home Assistant discovery prefix (if you changed it for some reason), and it will also be used in the MQTT bridge.


Find your ESP32 pinout and match all the needed pins for SPI and the LoRa radio connections. Fill them in at the appropriate spots in the above YAML. Make sure you pick the right frequency for your device/antenna/location as well. This firmware doesn't implement any limiting to work within rate-limited locations, so keep that in mind.


3.

For our sensor device, look at the YAML below.

1
esphome:
2
  name: example-sht3x-lora
3
esp32:
4
  board: esp32dev
5
  framework:
6
    type: arduino
7
logger:
8

9
# https://esphome.io/components/spi.html for LoRa radio
10
spi:
11
  clk_pin: GPIO05
12
  mosi_pin: GPIO27
13
  miso_pin: GPIO19
14

15
# https://esphome.io/components/i2c.html
16
i2c:
17
  sda: 32
18
  scl: 33
19

20
# import the components
21
external_components:
22
  - source:
23
      type: git
24
      url: https://github.com/u-fire/ESPHomeComponents/
25

26
lora_mqtt:
27
  cs_pin: GPIO18            # chip-select pin for LoRa radio, defaults to 18
28
  reset_pin: GPIO14         # reset pin for LoRa radio, defaults to 14
29
  dio_pin: GPIO26           # DIO0 pin for LoRa radio, defaults to 26
30
  frequency: 915000000      # frequency to use, defaults to 915 MHz
31

32
sensor:
33
  - platform: sht3xd
34
    temperature:
35
      name: "Outside Temperature"
36
    humidity:
37
      name: "Outside Humidity"


The ESP-Now component is the `lora_mqtt` block. There's no need for the `api` or `wifi` block. The component works by waiting for each sensor to update its state. When that happens, it creates a line similar to the one explained in the ESPNow article and sends it out as a LoRa message.


Make any changes for your particular sensors and upload the YAML through the Home Assistant ESPHome addon or the ESPHome command line. You'll start to see the scrolling of sensor measurements and notifications of sending LoRa messages. You'll also see those same messages in the bridge serial output, and if you are paying attention to your MQTT broker, you'll see new entries being added.


And most importantly, you'll also see the new sensor inside the MQTT section of `Settings -> Devices and Services``.


4.

The sensor also has the option to get an `on_sent` notification. This allows you to enter deep sleep the moment all of the sensor measurements have been sent, saving awake time.

1
...
2
globals:
3
  - id: sent
4
    type: int
5
    restore_value: no
6
    initial_value: '0'
7

8
lora_mqtt:
9
  on_sent:
10
    then:
11
      - lambda: id(sent)++; 
12
      - if:
13
          condition:
14
            lambda: 'return id(sent) == 5;'
15
          then:
16
          - deep_sleep.enter:
17
              id: sleep_
18
              sleep_duration: 5m
19

20

21
deep_sleep:
22
  id: sleep_
23
...

The code creates a global variable named `sent`. Then it increases the value each time a sensor measurement is sent. If `sent` equals 5, which in this case is the number of sensors, it goes to sleep for 5 minutes.


5.

I plan on doing a few things with this, mostly just performance testing with various LoRa-equipped boards. You can look at all the code on our GitHub

. There are a lot of other components there as well.


We're on Discord

, too.