# Connecting to ttn with STM32

**URL:** <https://www.thethingsnetwork.org/forum/t/connecting-to-ttn-with-stm32/68757>\
**Category:** End Devices (Nodes)\
**Tags:** end-devices\
**Created:** [July 27, 2024, 2:05pm UTC](https://www.thethingsnetwork.org/forum/t/connecting-to-ttn-with-stm32/68757 "2024-07-27T14:05:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![abir\_ba](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@abir\_ba](https://www.thethingsnetwork.org/forum/u/abir_ba)\
**Post date:** [July 27, 2024, 2:05pm UTC](https://www.thethingsnetwork.org/forum/t/connecting-to-ttn-with-stm32/68757/1 "2024-07-27T14:05:10Z")

</div>

**Summary**  
I want to connect an stm32L4 with RFM95) to TTN using ABP mode. This is my lorawan code ( am using the AES and sx1276 libraries )

**Logs**  
\<#include “lorawan.h”  
#include “main.h”  
#include “Encrypt\_V31.h”  
#include “AES-128\_V10.h”  
#include “SX1276.h”  
#include \<stdio.h\>  
#include \<string.h\>  
#include “math.h”

static uint8\_t Buffer[256]; // Global buffer for data

void lorawan\_init(lorawan\_t \*lorawan, SX1276\_t \*sx1276) {  
lorawan-\>sx1276 = sx1276;  
}

void lorawan\_set\_keys(lorawan\_t \*lorawan, uint8\_t NwkSkey, uint8\_t AppSkey, uint8\_t DevAddr) {  
lorawan-\>NwkSkey = NwkSkey;  
lorawan-\>AppSkey = AppSkey;  
lorawan-\>DevAddr = DevAddr;  
}

uint32\_t lorawan\_send\_data(lorawan\_t \*lorawan, uint8\_t \*data, unsigned len, uint16\_t frame\_counter\_up) {  
unsigned char i;  
unsigned char Direction = 0x00; // Direction of frame is up  
unsigned char MIC[4];  
unsigned char Mac\_Header = 0x40; // Unconfirmed data up  
unsigned char Frame\_Control = 0x00;  
unsigned char Frame\_Port = 0x01;

```
// Test flag: if all data[i] == i then don't send but print encrypted data
int is_test = 1;
for (i = 0; i < len; i++) {
    if (data[i] != i) {
        is_test = 0;
        break;
    }
}

// Encrypt the payload
Encrypt_Payload(data, len, frame_counter_up, Direction, lorawan->AppSkey, lorawan->DevAddr);
Println("Data encrypted");

// Build the radio package
Buffer[0] = Mac_Header;
Buffer[1] = lorawan->DevAddr[3];
Buffer[2] = lorawan->DevAddr[2];
Buffer[3] = lorawan->DevAddr[1];
Buffer[4] = lorawan->DevAddr[0];
Buffer[5] = Frame_Control;
Buffer[6] = (frame_counter_up & 0x00FF);
Buffer[7] = ((frame_counter_up >> 8) & 0x00FF);
Buffer[8] = Frame_Port;

unsigned char SX1276_Package_Length = 9;

for (i = 0; i < len; i++) {
    Buffer[SX1276_Package_Length + i] = data[i];
}

SX1276_Package_Length += len;

// Calculate MIC
Calculate_MIC(Buffer, MIC, SX1276_Package_Length, frame_counter_up, Direction, lorawan->NwkSkey, lorawan->DevAddr);
Println("MIC calculated");

// Print test package
if (is_test) {
    Println("Encrypted data:");
    for (i = 0; i < SX1276_Package_Length; i++) {
        char temp[10];
        sprintf(temp, "%02X ", Buffer[i]);
        Println(temp);
    }
    return 0;
}

// Append MIC to the buffer
for (i = 0; i < 4; i++) {
    Buffer[SX1276_Package_Length + i] = MIC[i];
}

SX1276_Package_Length += 4;

// Transmit the package
uint32_t result = SX1276_transmit(lorawan->sx1276, Buffer, SX1276_Package_Length, SX1276_DEFAULT_TIMEOUT);
if (result != 0) {
    Println("Transmission failed");
} else {
    Println("Transmission succeeded");
}
return result;

```

}

> 

**Hardware**  
SX1276 + STM32l476RCT

---

<div class="post-metadata">

**Author:** ![descartes](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/descartes/32/47220_2.png) [@descartes](https://www.thethingsnetwork.org/forum/u/descartes)\
**Post date:** [July 27, 2024, 2:21pm UTC](https://www.thethingsnetwork.org/forum/t/connecting-to-ttn-with-stm32/68757/2 "2024-07-27T14:21:30Z")

</div>

There are already working LW stacks that you can run on STM32 - including the one that was directly above your first post - what was wrong with that?

Overall, please do not develop a stack against the TTN community servers - there are enough problems with comms in Paris at the moment without someone trying to write complicated software and test against the shared servers.

One key element when asking for help - actually say what the problem is - you’ve provided us with the hardware, the libraries & your code but you haven’t actually said what the issue is.

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [July 27, 2024, 3:11pm UTC](https://www.thethingsnetwork.org/forum/t/connecting-to-ttn-with-stm32/68757/3 "2024-07-27T15:11:55Z")

</div>

> [@abir\_ba](#):
>
> This is my lorawan code

Your code does not implement the **mandatory** downlink and MAC command handling. If you do not implement that you will effectively be DoSsing the gateways that receive and forward your uplinks.
