If you are lacking good documentation it can be very hard to get started with a new device. I just couldn’t find a comprehensive example project for how to connect TTGO-LoRA32 V1.0 with the V3 version of the Things Network. So I hope this post and the related github project will save you some time to achieve the same.
I have connected devices to TheThingsNetwork before but with this device I had several challenges: no useful example project, TTN just switched to the new V3 version and bad documentation about the hardware. After the first failure I assumed that the TTGO-LoRa32 was to weak to send data to the next gateway. Since my own TTN indoor gateway was still registered for V2 I started to migrate it first. That was another bad experience. The gateway did not connect to TTN V3 and I reclaimed it several times without success. Only after waiting one night it finally worked and I had a gateway connected to TTN v3.
So now that I knew that I had a gateway close enough to receive the TTGO-LoRa32′ data package I could concentrate on fixing the code.
Prepare the Code
In this project, I’m using platformio in Visual Studio Code for editing and uploading the firmware. As a first step get the code from my github repository https://github.com/squix78/TTGO-LoRa32-V1.0-TTN-OTAA either by cloning it with git or by downloading the code as zip file and extracting it.
This project should work as it is except for a few changes we have to do in the code. The most obscure change is buried deep in the library folder in a file called lmic_project_config.h. The full path to the file is
.pio/libdeps/ttgo-lora32-v1/MCCI LoRaWAN LMIC library/project_config/lmic_project_config.h
If you can’t find this file you probably have to compile the project first, since only then all the dependencies will be downloaded. Please note: at the moment the compilation will fail, since we didn’t set the credentials in the code yet. This is expected and normal.
Once you find that file make sure that the correct region settings are there. For Europe 868MHz band it should like this:
// project-specific definitions #define CFG_eu868 1 //#define CFG_us915 1 //#define CFG_au915 1 //#define CFG_as923 1 // #define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP /* for as923-JP */ //#define CFG_kr920 1 //#define CFG_in866 1 #define CFG_sx1276_radio 1 //#define LMIC_USE_INTERRUPTS
By default the code uses CFG_us915. If you are living in another region it is very important you do that change!
Getting the app and device credentials
This was the first device I set up for TTN V3 and there are a couple of traps I want to point out to you. A quick explanation for the TTN novice: Your device needs three credentials to connect to TTN: AppEUI, DevEUI and AppKey. In TTNs V2 interface you could either provide the DevEUI or it would create them for you. In TTN V3 this auto EUI generation is currently missing. For many devices the vendor will provide these identifiers and you just have to enter them in the TTN console. But with devices such as the TTGO-LoRa32 you don’t get it. I show you here how to create it by yourself. This is not ideal but works… Just don’t blame me in case of problems…
- Head over to https://console.cloud.thethings.network/ and pick your V3 region. Don’t use the V2!
- Create an account if you don’t have one yet.
- Go to applications and create a new application. As application id you can pick anything as long as
- it doesn’t exist yet
- only contains letters, dashes and numbers
- On the next screen click on “Add end device” (see image below)
- Now go the “Manually” tab and choose “Over the air activation (OTAA)” and MAC V1.0.2
- On the next screen choose a name for your device consisting only of letters, numbers and dashes. To fill in the JoinEUI and DevEUI you can use this generator: https://descartes.co.uk/CreateEUIKey.html
- On the next screen choose your region and the following settings:
- On the Join settings screen you can click on the “recycle” symbol to generate an AppKey for you.
- Now we have to copy the credentials over to our code. We have to provide JoinEUI and DevEUI in least significat byte order or short LSB. The TTN console helps you with that. First click on the “<>” icon and then on the arrows beside “msb”. To put the data into the clipboard use the most right icon. BTW: The name of JoinEUI is AppEUI in the code. Please add the AppKey msb order, no need to invert the order there.
- After copying JoinEUI (AppEUI), DevEUI and AppKey to the main.cpp file in the code you got from github you can compile and upload the firmware to the TTGO-LoRa32
- Now check if it worked by looking at the device live data on the TTN console:
Summary & Outlook
This is it! If everything worked you your TTGO-LoRa32 is now sending messages to TTN V3, joining it with dynamic session keys! If I find the time I’d like to analyze the power consumption of the device for sending a message as well as in the sleep phase.
My code is not using the OLED display. There is a repository provided by LillyGo that shows you how to do that: https://github.com/LilyGO/TTGO-LORA32/tree/LilyGO-868-V1.0
If you like to support my blog please consider buying the device by using this affiliate link: https://www.banggood.com/custlink/vmDdBjpESq
Great job ! Congrats !
Just a question … Is this code available for AU915 Frequency Plan ?