Heltec WiFi LoRa 32 – ESP32 with OLED and SX1278

P1010108.jpg

Some initial information to get up and running with the Heltec Wifi Lora 32 Kit Module.

Like a lot of these devices this is pretty easy to set up. Once you know which libraries and pin numbers to use! If you’ve not set up the board in the Arduino IDE yet there’s instructions here.

For an initial test run to see if the LoRa part is functioning you’ll need another device to transmit or receive Lora signals. For the tests below I’m using Ai-Thinker RA-02 with an Arduino UNO as described in this post – UNO with Ai-Thinker RA-02 SX1278 LoRa Module to send packets.

There’s a download available for this Heltec device but it’s probably easier to use one of the LoRa libraries that are available through the Arduino IDE library manager. Just search for an install this one:

Lora Library Screen Capture

To make this library work with the Heltec LoRa device you need to define some pins:
#define SS 18
#define RST 14
#define DI0 26

Add a couple of lines to the setup():
SPI.begin(5, 19, 27, 18);
LoRa.setPins(SS, RST, DI0);

and change the baud rate:  Serial.begin(115200); and LoRa frequency: LoRa.begin(433E6)

Or just copy and paste the code below 😉

#include <SPI.h>
#include <LoRa.h>

// WIFI_LoRa_32 ports
// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)

#define SS      18
#define RST     14
#define DI0     26

void setup() {
  
  SPI.begin(5, 19, 27, 18);
  LoRa.setPins(SS, RST, DI0);
  
  Serial.begin(115200);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(433E6)) {
    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 of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

Depending what you are sending you’ll see something like this in the serial monitor:

Received packet ’79’ with RSSI -68
Received packet ’80’ with RSSI -68
Received packet ’81’ with RSSI -58
Received packet ’82’ with RSSI -58
Received packet ’83’ with RSSI -68
Received packet ’84’ with RSSI -68
Received packet ’85’ with RSSI -68
Received packet ’86’ with RSSI -68


Viewing the received packets on the built-in OLED display

I’ll explain in more detail later but if you want to see the LoRa data on the built-in screen as well as the serial console you can just copy and paste the code below. You’ll need to install the U8g2 (https://github.com/olikraus/u8g2) library. This can be found in the Arduino IDE library manager.  Open Sketch > Include Library > Manage Libraries and search for and then install U8g2.

#include <U8x8lib.h>
#include <LoRa.h>

String receivedText;
String receivedRssi;

// WIFI_LoRa_32 ports
// GPIO5  -- SX1278's SCK
// GPIO19 -- SX1278's MISO
// GPIO27 -- SX1278's MOSI
// GPIO18 -- SX1278's CS
// GPIO14 -- SX1278's RESET
// GPIO26 -- SX1278's IRQ(Interrupt Request)

#define SS      18
#define RST     14
#define DI0     26
#define BAND    433E6

// the OLED used
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);

void setup() {

  SPI.begin(5, 19, 27, 18);
  LoRa.setPins(SS, RST, DI0);

  Serial.begin(115200);
  while (!Serial); //if just the the basic function, must connect to a computer
  delay(1000);

  u8x8.begin();
  u8x8.setFont(u8x8_font_chroma48medium8_r);

  Serial.println("LoRa Receiver");
  u8x8.drawString(0, 1, "LoRa Receiver");

  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    u8x8.drawString(0, 1, "Starting LoRa failed!");
    while (1);
  }
}

void loop() {

  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    u8x8.drawString(0, 4, "PacketID");

    // read packet
    while (LoRa.available()) {
      receivedText = (char)LoRa.read();
      Serial.print(receivedText);
      char currentid[64];
      receivedText.toCharArray(currentid, 64);
      u8x8.drawString(9, 4, currentid);
    }

    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    u8x8.drawString(0, 5, "PacketRS");
    receivedRssi = LoRa.packetRssi();
    char currentrs[64];
    receivedRssi.toCharArray(currentrs, 64);
    u8x8.drawString(9, 5, currentrs);
  }

}

This code is just to test everything works. More  needs to be done to properly use LoRa within the legal guidelines etc.

5 Replies to “Heltec WiFi LoRa 32 – ESP32 with OLED and SX1278”

  1. Atilio says:

    Hi! Thank for this post. It was VERY helpful to me. I received my modules 3 days ago.
    I got them up and running based on your instructions.
    I set one as a Sender and another as a Receiver.
    The sender has an ultrasonic sensor attached to it and sends the measured distance to the Receiver using LoRa. Everything works fine.
    Now I’m trying to connect simultaneously the LoRa receiver through WiFi to my router using the ESP32 and send a MQTT message with the value of the measure to MQTTCloud. The question is whether this module can act as a gateway or not.
    Have you tried this?
    Thanks again,
    Atilio

    1. WordBot says:

      I’m not sure which module this was for but you can connect to your router with something like the script below. The HTTPClient.h library does all the magic.

      [code]
      #include “WiFi.h”
      #include
      #include
      #include

      #include

      const char* ssid = “YOURSSID”;
      const char* password = “YOURWIFIPASS”;

      String receivedText;
      String receivedRssi;

      #define SS 18
      #define RST 14
      #define DI0 26
      #define BAND 433E6

      void setup() {
      Serial.begin(115200);
      while (!Serial); //if just the the basic function, must connect to a computer
      delay(1000);

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) { //Check for the connection
      delay(1000);
      Serial.println(“Connecting to WiFi..”);
      }

      Serial.println(“Connected to the WiFi network”);

      Serial.println(“LoRa Receiver”);

      SPI.begin(5, 19, 27, 18);
      LoRa.setPins(SS, RST, DI0);

      if (!LoRa.begin(BAND)) {
      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());
      receivedText = LoRa.read();
      }

      // print RSSI of packet
      Serial.print(“‘ with RSSI “);
      Serial.println(LoRa.packetRssi());
      receivedRssi = LoRa.packetRssi();

      if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

      HTTPClient http;

      String thisUrl;
      thisUrl = “http://YOURURL/TESTDIR/i-am-receiving.php?tx=”;
      thisUrl.concat(receivedText);
      thisUrl.concat(“&rssi=”);
      thisUrl.concat(receivedRssi);

      http.begin(thisUrl); //Specify destination for HTTP request

      int httpCode = http.GET();

      if (httpCode > 0) {
      String payload = http.getString();
      Serial.println(httpCode);
      Serial.println(payload);

      } else {
      Serial.print(“Error on sending POST: “);
      }
      http.end(); //Free resources
      } else {
      Serial.println(“Error in WiFi connection”);
      }

      }
      }
      [/code]

  2. Atilio says:

    Hi WordBot. Thanks for your reply and the code example.
    I have exactly the same board than the one on the above picture (Heltec ESP32 WiFi LoRa). Mine is 868-915Mhz.
    I’m newbie at LoRa and LoRaWAN.
    After more reading, I guess that what I should be doing is LoRaWAN instead of plain LoRa.
    I found that I should be using LMIC.h library in my LoRa Node device code.
    (https://github.com/matthijskooijman/arduino-lmic)
    I started playing with ttn-abp example (its a little bit complex for me right now… but i’ll keep trying)
    Any advice on this path?
    Thanks again.

    1. WordBot says:

      I read this on the difference between LoRa and LoRaWAN – https://www.quora.com/What-is-the-difference-between-Lora-and-LoraWan.

      For my tests I just needed to send GPS coordinates from my location via LoRa to one of the
      Heltec devices above which receives and then sends the data to a webserver via a WiFi connection using a GET request. I can then use my phone to look at a web page on the webserver to check it all functions.

      I need to do some more reading on using encryption and certain frequencies so my location data is only available to me. Maybe that’s where LoRaWAN comes in.

Leave a Reply

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

scroll to top