# LMIC library basic documentation

**URL:** <https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716>\
**Category:** LMIC\
**Created:** [December 14, 2018, 7:49am UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716 "2018-12-14T07:49:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![basvaningen](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@basvaningen](https://www.thethingsnetwork.org/forum/u/basvaningen)\
**Post date:** [December 14, 2018, 7:49am UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/1 "2018-12-14T07:49:36Z")

</div>

Hi there,

Is there some “simple” documentation available for the LMIC library. I found a PDF on Github but that’s not what im looking for. I’m just a PHP developer and starting in C, but where making progress 🙂  
What is see is that people strugling with the library where to put there own code in the sample’s. Normally you put your code in de void\_loop() section. But in all the samples i see it in the EV\_TXCOMPLETE of the select case statement.

What i’m looking for:  
Description of what the functions/procedures do.  
Like for example:

- os\_runloop\_once() or os\_runloop() what are the differences and when do you need to fire them.
- os\_setTimedCallback(&sendjob, os\_getTime()+sec2osticks(TX\_INTERVAL), do\_send) what does this exactly. I know i can schedule when the do\_send is fired.

What i’m trying to reach:  
I want to make one basic sketch with for example the following functions:  
LoraInit()  
LoraJoinAPB()  
LoraJoinOTAA()  
LoraSend()  
LoraReceive()  
LoraSleep()  
and on

I think the community will have there benefit from it. And surely the beginning C programmers 🙂

---

<div class="post-metadata">

**Author:** ![UdLoRa](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/udlora/32/45330_2.png) [@UdLoRa](https://www.thethingsnetwork.org/forum/u/UdLoRa)\
**Post date:** [December 14, 2018, 12:13pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/2 "2018-12-14T12:13:05Z")

</div>

Hi, I agree on the library complexity. At least [one the the PDFs](https://www.developpez.net/forums/attachments/p195381d1450200851/environnements-developpement/delphi/web-reseau/reseau-objet-connecte-lorawan-delphi/lmic-v1.5.pdf/) around is “simple”, though not easy to apply without examples at hand. However, there you can find answers to the specific questions you are asking.

Regarding a wrapper for LMIC, I think it would be welcome, however there has been already some attempts (I remember to have crossed something on Github, but never switched to them).

---

<div class="post-metadata">

**Author:** ![sk\_lora](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/sk_lora/32/20484_2.png) [@sk\_lora](https://www.thethingsnetwork.org/forum/u/sk_lora)\
**Post date:** [February 20, 2019, 11:04am UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/3 "2019-02-20T11:04:06Z")

</div>

Hello, did you have any progress in this way of doing the codes? this would be relatively energetic for the community

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [February 20, 2019, 11:07am UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/4 "2019-02-20T11:07:42Z")

</div>

> [@UdLoRa](#):
>
> a wrapper for LMIC

Like maybe the following. I’ve never used it:

> **[GitHub - mcci-catena/arduino-lorawan: User-friendly library for using...](https://github.com/mcci-catena/arduino-lorawan)**
>
> User-friendly library for using arduino-lmic with The Things Network and other LoRaWAN® networks - mcci-catena/arduino-lorawan

---

<div class="post-metadata">

**Author:** ![marcelzanatta](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/marcelzanatta/32/35697_2.png) [@marcelzanatta](https://www.thethingsnetwork.org/forum/u/marcelzanatta)\
**Post date:** [June 10, 2020, 2:51pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/5 "2020-06-10T14:51:48Z")

</div>

Hi @basvaningen, How are you? Have you made any progress with this? Were you trying to joining the ABP and OTTA connection?

I’m trying to create code that changes the connection automatically. Example. 30 minutes connected and transmitting in OTTA to TTN, after that, 30 minutes connected in APB and so on.

thank`s

---

<div class="post-metadata">

**Author:** ![cslorabox](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cslorabox/32/35697_2.png) [@cslorabox](https://www.thethingsnetwork.org/forum/u/cslorabox)\
**Post date:** [June 10, 2020, 3:15pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/6 "2020-06-10T15:15:32Z")

</div>

> [@marcelzanatta](#):
>
> I’m trying to create code that changes the connection automatically. Example. 30 minutes connected and transmitting in OTTA to TTN, after that, 30 minutes connected in APB and so on.

Why in the world would you ever want to do that???

This is a very, very, very bad idea. The whole point of OTAA is to accomplish a joining process which should happen with exceeding rarity - ideally only once in the entire lifetime of the device. And the point of ABP is to simply hard code what would be the result such that it never needs to happen.

Toggling modes is severely unwise, produces unecessary excess traffic load on the network, and drastically decreases reliability, to no benefit of any sort whatsoever.

Don’t do it!

---

<div class="post-metadata">

**Author:** ![marcelzanatta](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/marcelzanatta/32/35697_2.png) [@marcelzanatta](https://www.thethingsnetwork.org/forum/u/marcelzanatta)\
**Post date:** [June 30, 2020, 10:55pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/7 "2020-06-30T22:55:58Z")

</div>

@cslorabox … Uoool, haha ​​You are absolutely right, I am playing with my project, trying to understand the MCCI library better. But I changed my mind, at least a little. I want to have a unique code, which after recording is possible through the serial, configure keys and determine the connection, ABP or OTTA. I understand serial communication well, this is no problem, but not the library. What in the device define if the connection is OTTA or ABP? I didn’t find anything specific in the source code, what changes from one to the other are the keys … I’m using the examples from the MCCI 3.2 library… You can help-me ?

---

<div class="post-metadata">

**Author:** ![cslorabox](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/cslorabox/32/35697_2.png) [@cslorabox](https://www.thethingsnetwork.org/forum/u/cslorabox)\
**Post date:** [June 30, 2020, 11:06pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/8 "2020-06-30T23:06:08Z")

</div>

That LMiC repo offers entirely distinct examples for ABP vs OTAA, you should start there to understand the difference.

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [July 1, 2020, 7:14am UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/9 "2020-07-01T07:14:13Z")

</div>

> [@marcelzanatta](#):
>
> What in the device define if the connection is OTTA or ABP?

First, there is no “connection”. Second, OTAA and ABP are only used to _activate_ the device and, for OTAA, possibly get some network settings. **Once activated, there is no difference between OTAA and ABP devices** †. And they still don’t have “a connection” but will just transmit when they want, hoping one or more gateways will receive their transmission and forward it to TTN. This is general LoRaWAN, not specific for LMIC.

In a way, one could say that LMIC is OTAA-only: if you schedule a packet using `LMIC_setTxData2` then when it’s time to transmit, LMIC will always first do an OTAA join if it has not been activated yet. That is: if it has no [session keys](https://www.thethingsnetwork.org/docs/lorawan/security.html) yet. There is no choice: LMIC always does OTAA then, and at that point things fail if you did not program the OTAA details into the device.

To avoid that, one can manually invoke `LMIC_setSession` first. That, for example, is required for an OTAA device that already joined earlier, but for which the state is not kept in memory during deep sleep. (And then there’s more to restore than just the session secrets, most importantly the frame counters but much more; see [How to persist LMIC OTAA parameters with an ESP32? - #12 by jofleck](https://www.thethingsnetwork.org/forum/t/how-to-persist-lmic-otaa-parameters-with-an-esp32/35032/12))

So, when one explicitly wants ABP rather than OTAA, then one can call `LMIC_setSession` before scheduling the first packet. This way LMIC also supports ABP.

I’m sure one can figure out from the LMIC internals if it last activated using OTAA or `LMIC_setSession`. But as you already _know_ in your own code, the question would be: why?

† Well, except that an OTAA device might easily re-activate if ever needed, and then by design gets a clean state on the network. When resetting an ABP device then all sorts of state mismatch will happen between network and device, most notably the frame counters being reset in the device but not on the network. Go OTAA.

---

<div class="post-metadata">

**Author:** ![marcelzanatta](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/marcelzanatta/32/35697_2.png) [@marcelzanatta](https://www.thethingsnetwork.org/forum/u/marcelzanatta)\
**Post date:** [July 2, 2020, 10:15pm UTC](https://www.thethingsnetwork.org/forum/t/lmic-library-basic-documentation/20716/10 "2020-07-02T22:15:42Z")

</div>

Hi @arjanvanb and @cslorabox , thank very much for the information!! I have a problem regarding as soon as I solve it, I’ll go back to that task

take care!!
