Heltec WiFi LoRa 32
sample code https://github.com/HelTecAutomation/Heltec_ESP32
from https://robotzero.one/heltec-wifi-lora-32/
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:
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);
#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
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
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
#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 433E6void setup() {Serial.begin(115200);while (!Serial); //if just the the basic function, must connect to a computerdelay(1000);WiFi.begin(ssid, password);while (WiFi.status() != WL_CONNECTED) { //Check for the connectiondelay(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 packetint packetSize = LoRa.parsePacket();if (packetSize) {// received a packetSerial.print("Received packet '");// read packetwhile (LoRa.available()) {Serial.print((char)LoRa.read());receivedText = LoRa.read();}// print RSSI of packetSerial.print("' with RSSI ");Serial.println(LoRa.packetRssi());receivedRssi = LoRa.packetRssi();if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection statusHTTPClient 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 requestint 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");}}}