Creating a node on The Things Network with a Heltec LoRa 32 using the Arduino IDE
The Heltec Lora 32 is an ESP32 module with an SX1276 chip and a little 0.96″ OLED screen. These are available from the Heltec store on AliExpress. Be careful to buy the new version (V2) to get the new functionality like deep sleep, upgraded FLASH and better LoRa signal.
If you don’t already have the ESP32 boards set up in the Arduino IDE then connect the LoRa antenna and then follow the steps Setting Up the Arduino IDE for the ESP32 Range in this post.
Registering the Device at The Things Network
Sign up for an account here: https://account.thethingsnetwork.org/register
First a new Application has to be created at The Things Network: https://console.thethingsnetwork.org/applications, An example of an application could be a remote greenhouse monitoring system with several sensors that send readings to a gateway.
When the application is setup you need to register a device. On the Application Overview page for your new Application, click ‘register device’
In the page that opens, type a Device ID, click the arrows icon on the left of the Device EUI field to automatically create the EUI and then click Register.
In the Device Overview section click the <> symbols next to the Device EUI, Application EUI and App Key to see data you will use later.
Installing the Heltec Development Framework
Heltec have created a ‘framework’ to support their boards within the Arduino IDE. Follow the instructions in Step 1 in the page linked below to install it. Optionally install the Heltec ESP32 Library (Step 2) and test with the Example Sketches.
Installing the LoRaWAN Library
The ESP32_LoRaWAN library can be installed using the Arduino Library manager Sketch > Include Library > Manage Libraries and search for ESP32_LoRaWAN
Previously the settings for The Things Network were stored in the library files but these are now found in the Sketches themselves. For example
The OTAA.ino example:
/* OTAA para*/
uint8_t DevEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 };
uint8_t AppEui[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t AppKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01 };
License
The Lora32 device needs a license key for this library. To get the key, paste the following into a new Sketch (file > new):
uint64_t chipid; void setup() { Serial.begin(115200); } void loop() { chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes). Serial.printf("ESP32 Chip ID = %04X",(uint16_t)(chipid>>32));//print High 2 bytes Serial.printf("%08X\n",(uint32_t)chipid);//print Low 4bytes. delay(3000); }
Before you power the module, connect the antenna. The unit can be damaged if you try to transmitted data without an antenna. When the Lora32 is connected to power or USB it starts a demo which includes transmitting LoRa packets so connect an antenna first!
Once connected to USB, upload the sketch above to the board and open the serial monitor. You’ll see something like this:
Copy the Chip ID and paste it into the form here to get a license key: http://www.heltec.cn/search/
Connecting with an Example Sketch
Open the example sketch: File > Examples > ESP32_LoRaWAN > OTAA
Paste the new license in this line:
uint32_t LICENSE[4] = {0xC1670CF8,0x19C71AD5,0x6CE47540,0x8CF267EC};
Upload the Sketch, open the serial monitor and you should see something like this:
In The Things Network website navigate to Applications > **your_application** > Devices > **your_device** and you should see something like this:
Your node is now connected to The Things Network!
References
Heltec Development Environment: https://github.com/Heltec-Aaron-Lee/WiFi_Kit_series (Doc – https://heltec-automation-docs.readthedocs.io/en/latest/esp32/quick_start.html)
Heltec ESP32_LoraWAN library on Github: https://github.com/HelTecAutomation/ESP32_LoRaWAN
Buy Me A Coffee
If you found something useful above please say thanks by buying me a coffee here...
I’m not sure why it’s so slow. I tried mine without a connection and it doesn’t do the same thing. However I’ve noticed something. The node looks like it’s working on the US902-928 frequencies and the gateway on the AU915-928 frequencies https://www.thethingsnetwork.org/docs/lorawan/frequency-plans.html
What do you have set for “Open LoRaMac-definitions.h and change the line: #define USE_BAND_470”?
hi,
I don’t have the options for LoRaWAN debuglevel and LoRaWAN region in the tools menu, but you need to choose an option in them to make the code work.
Hi, I’ve just rewritten the tutorial for the changes Heltec have made in their code. You can have to install a ‘Framework’ to get the new menu items.
Hi, I want to ask question. How to set frequency on the nodes? Is it only need to set in the Arduino IDE Tools Menu at LoRaWan Region? I have follow and use the OTAA example and seems that my esp32 node is not connecting to the TTN. Btw I use Heltec Wifi Lora 32 (V2) for the nodes and have Heltec HT-M01 Mini Lora Gateway and the gateway is already shown connected on the TTN. Now my only problem is the nodes did not want to connect to TTN. I hope someone can help me.
Hi!
I tried your example but I get only “joining…” on the serial monitor. I think my ESP32 is too far away from gateway. Is there a way to see if there are gateways around?
Thank you!
Hi, Have you looked at the https://www.thethingsnetwork.org/ website? There will be a map of gateways.
Thanx for your reply. Meanwhile I found out that out of one of the two windows I have in office I can reach a GW that is 5 Km away.
Hi there,
thank you for UR tutorial.
In the meantime HELTEC is providing a new version “v3” of this device. I am wondering, whether this new version will work with your setup?
Hi, Probably not. I think a lot has changed since I made this tutorial but in general is should work the same.