跳到主要內容

Heltec WiFi LoRa 32

sample code https://github.com/HelTecAutomation/Heltec_ESP32


from https://robotzero.one/heltec-wifi-lora-32/



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. If you are interested in LoraWAN and The Things Network, I’ve added a new tutorial:  Heltec Lora32 LoraWan Node on The Things Network


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

  1. Atilio
    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
      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.
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      #include "WiFi.h"
      #include <U8x8lib.h>
      #include <WiFi.h>
      #include <HTTPClient.h>
      #include <LoRa.h>
      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.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");
          }
        }
      }




留言

熱門文章