Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for Arduino MKR WAN 1310 #1343

Merged
merged 10 commits into from
Jun 9, 2024
58 changes: 29 additions & 29 deletions doc/content/devices/models/mkr1310/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ weight:

{{< figure src="mkrwan1310.png" alt="Arduino MKR WAN 1310" class="float plain" >}}

This section will help you get started with using The Arduino MKR WAN 1310 on {{% tts %}}.
This section will help you get started with using The [Arduino MKR WAN 1310](https://docs.arduino.cc/hardware/mkr-wan-1310/) on {{% tts %}}.

<!--more-->

The **Arduino MKR WAN 1310** is a board that combines the power of an ATMEL SAMD21 microcontroller and the communications capabilities of the Murata LoRa® module. It is designed to integrate the core's low power-consumption and high performance with the ease-of-use of Arduino's environment, making this board a practical and cost effective solution for makers seeking to add LoRa® connectivity to their projects, with minimal previous experience in networking.
The **Arduino MKR WAN 1310** is a board that combines the power of an ATMEL SAMD21 microcontroller and the communication capabilities of the Murata LoRa® module. It is designed to integrate the core's low power-consumption and high performance with the ease-of-use of Arduino's environment, making this board a practical and cost effective solution for makers seeking to add LoRa® connectivity to their projects, with minimal previous experience in networking.

The Arduino MKR WAN 1310 is programmed using the Arduino Software (IDE).

Expand All @@ -20,22 +20,26 @@ The Arduino IDE allows you to write code using the Arduino programming language

Download the latest release of the [Arduino Software (IDE)](https://www.arduino.cc/en/Main/Software), [install](https://www.arduino.cc/en/Guide) it on your operating system and run it.

Before we get to programming you need to install a few additional things. First is the drivers for the board. This can be done by clicking the **second icon in the left-hand menu** or navigating to **Tools > Board > Board Manager...**. Here you need to look for the **Arduino SAMD boards (32-bits ARM® Cortex®-M0+)** and install it.
Before we get to programming you need to install a few additional things. First is the drivers for the board. This can be done by clicking the **second icon in the left-hand menu** or navigating to **Tools > Board > Board Manager...**. Here you need to look for the **Arduino SAMD boards (32-bits ARM® Cortex®-M0+)** package and install it.

{{< figure src="boards-manager.png" alt="Arduino Boards Manager" >}}

Then you need to install the library for the board. Click on the **third icon in the left-hand menu** or navigate to **Tools > Manage libraries..**. In here search for **MKRWAN** and install it.

{{< figure src="library-manager.png" alt="Arduino Library Manager" >}}

{{< note "You might have noticed there is also a library named MKRWAN_v2 from Arduino. We used the MKRWAN library for this tutorial, but both libraries work. As the two libraries rely on two separate firmware, it is important that your device has the correct firmware installed. Updating it is done by running the *File > Examples > MKRWAN / MKRWAN_v2 > MKRWANFWUpdate_standalone* example from the corresponding library. " />}}
{{< note "You might have noticed there is also a library named MKRWAN_v2 from Arduino. We used the MKRWAN library for this tutorial, but both libraries work. As the two libraries rely on two separate firmware, it is important that your device has the correct firmware installed. Updating it is done by running the **File > Examples > MKRWAN / MKRWAN_v2 > MKRWANFWUpdate_standalone** example from the corresponding library. " />}}

Once you have downloaded the library, you will need to upload an example code from the **MKRWAN** library, called **FirstConfiguration**. You can find this in **File > Examples > MKRWAN > FirstConfiguration**.

Then plug the Arduino into your computer. In the IDE, click on **Select Board** at the top, and select your **Arduino MKR WAN 1310**.

{{< figure src="select-board.png" alt="Selecting Board" >}}

{{< note >}}
If your board does not show up in the dropdown, try a different cable. The cable might not be able to transfer data.
{{</ note >}}

Once that is done you can upload the file to the board (Using the **->** button). Once the code is uploaded successfully, open the Serial Monitor. You can open it by clicking the **icon in the top-right corner**.

{{< figure src="serial-monitor.png" alt="Serial Monitor" >}}
Expand All @@ -48,49 +52,50 @@ Here you will find the unique Device EUI as displayed in the picture below:

Now copy the Device EUI and save it, as it will be used for connecting to {{% tts %}}.

## Registering on the Stack
## Registering on {{% tts %}}

Move to the Stack, create an application and click register device.
{{< figure src="hw-version.png" alt="Hardware/Module version of the MKR in the arduino serial monitor" class="float" width="150%" >}}
Move to {{% tts %}} Console, create an application and click register device. Check [Adding Devices](https://www.thethingsindustries.com/docs/devices/adding-devices/) for more info.

Here enter the following details:
- End device brand: **Arduino SA**
- Model: **MKR WAN 1310**
- Hardware Ver.: **1.0**
- Firmware Ver.: **Check in the Serial Monitor ->**
- **End device brand:** `Arduino SA`
- **Model:** `MKR WAN 1310`
- **Hardware Ver.:** `1.0`
- **Firmware Ver.:** Check in the Arduino IDE Serial Monitor ↓

{{< figure src="hw-version.png" alt="Hardware/Module version of the MKR in the arduino serial monitor" class="" width="150%" >}}

{{< figure src="stack-device.png" alt="details of the end device type" >}}

For the Provisioning Information, enter the following details:
- JoinEUI: **All 0's**
- DevEUI: **Copied from the Serial Monitor**
- AppKey: **Generated by clicking on `Generate`**
For the **Provisioning information**, enter the following details:
- **JoinEUI:** `0000000000000000`
- **DevEUI:** The Device EUI value that was previously copied from Arduino IDE Serial Monitor
- **AppKey:** Generate by clicking the **Generate** button

{{< figure src="stack-prov.png" alt="provisioning information of MKR" >}}

In your device overview you can now see three rows under Activation information: **Device EUI, Application EUI and App Key**. You are going to need these values for the next steps.
In the device overview you can now see three parameters in the **Activation information** section: **AppEUI**, **DevEUI** and **AppKey**. You are going to need these values for the next steps.

{{< figure src="device-overview.png" alt="Device overview page with activation information highlighted" >}}

## Connecting board to Stack
## Connecting board to {{% tts %}}

Now let's go back to the **FirstConfiguration** sketch again. To be safe, reset your board and open the Serial Monitor again. Once the program starts, it will start asking questions in the Serial Monitor. The first one is if you are using OTAA or ABP. We are going to use OTAA, so you can enter a "**1**" in the message field and hit enter.
Now let's go back to the **FirstConfiguration** sketch again. To be safe, reset your board and open the Arduino IDE Serial Monitor again. Once the program starts, it will start asking questions in the Serial Monitor. The first one is if you are using OTAA or ABP. We are going to use OTAA, so you can enter a `1` in the message field and hit enter. For why we are using OTAA, check [ABP vs OTAA](https://www.thethingsindustries.com/docs/devices/concepts/abp-vs-otaa/).

Then it will ask you for your **APP EUI** and **APP KEY**. First copy the **AppEUI** from it's respective **Activation information** fields inside {{% tts %}}, then enter it in the message field, hit enter, and repeat for the other one. Once you have entered them, the device will try to connect to TTN.
Then it will ask you for your **APP EUI** and **APP KEY**. Enter values for these parameters from the **Activation information** fields inside {{% tts %}} Console device overview. Once you have entered them, the device will try to connect to {{% tts %}}.

{{< figure src="monitor-prov.png" alt="serial monitor with message field highlighted" >}}

Now go to {{% tts %}}, you should see the device connecting. But you are going to need a payload formatter.
Now go to {{% tts %}} Console **Live data** tab and you should see messages arriving, which should mean the device is connecting.

As the payload comes in a HEX format, you will need to decode it using a **Payload Formatter**. To do this, in your application on {{% tts %}}, click on **Payload formatters**.
As the device payload comes to {{% tts %}} in a HEX format, you will need to decode it using a [payload formatter](https://www.thethingsindustries.com/docs/integrations/payload-formatters/). To do this, in your application on {{% tts %}}, navigate to **Payload formatters**.

{{< figure src="stack-pf.png" alt="Payload formatters highlighted" width="70%" >}}

Make sure that **Custom Javascript formatter** is selected here from the dropdown
Select the **Custom Javascript formatter** from the **Formatter type** dropdown.

{{< figure src="custom-js.png" alt="Custom javascript selected in dropdown" width="70%" >}}

Now in the **Formatter code** field, replace it with this formatter:
Enter the following code in the **Formatter code** field:

```js
function Decoder(bytes, port) {
Expand All @@ -102,9 +107,4 @@ function Decoder(bytes, port) {
}
```

That was it! Now go get creating or try an [ example project]({{< ref "/devices/models/mkr1310/temp" >}}).

## Troubleshoot

**My Arduino does not show up in Select Board**
- Make sure that the cable you are using can transfer data. Try a different cable.
That was it! Now you can check out our [example project]({{< ref "/devices/models/mkr1310/temp" >}}) or start creating your own.
117 changes: 26 additions & 91 deletions doc/content/devices/models/mkr1310/temp.md
Original file line number Diff line number Diff line change
@@ -1,104 +1,42 @@
---
title: "Sending Temperature with MKR WAN 1310"
title: "Monitoring Temperature with MKR WAN 1310"
description: ""
weight:
---

In this section you are going to try an example project where you will connect a Temperature and Humidity sensor to the MKR WAN 1310 to send the data over LoRaWAN and then decode it on {{% tts %}}.
In this section we present an example project where we show how to connect a temperature and humidity sensor to the MKR WAN 1310 device to send the data to {{% tts %}} and decode it.

<!--more-->

The sensor we are going to be using is a DHT11, which is a Temperature and Humidity sensor. For more information on how the sensor works and how the module you are using might differ in wiring, check [this guide](https://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-an-arduino/).

{{< figure src="../fritzing.png" alt="Wiring mockup of the DHT11 connected to the arduino" class="float plain" width="90%">}}

## Circuit
## Building the Circuit

For this project you are going to be needing:
- **DHT11**
- Some **Jumper Wires**
- **Breadboard**
- DHT11 sensor
- Some jumper wires
- Breadboard

Once you have the components, wire it up like so:
Once you have the components, wire it up like shown on the image on the right:
- **Arduino 5V** -> **Plus DHT11**
- **Arduino GND** -> **Minus DHT11**
- **Arduino Pin 6** -> **Signal DHT11**

Illustration on the right displays the wiring as well.

{{< note "Double check your own sensor's pinout, it can vary between modules. If you are using a bare DHT11, you might need to use a resistor." />}}

## Creating the code
## Programming the MKR 1310

Before we continue, you need to make sure you have the **DHT sensor library by Adafruit** installed:
Before we continue, you need to make sure you have the **DHT sensor library by Adafruit** installed in the Arduino IDE:

{{< figure src="../dht-lib.png" alt="DHT sensor library by Adafruit">}}

Next let's go through the code step by step and explain what all the fields mean. The completed code can be found below it.

### The code explained

First we initialize the **MKRWAN** and **DHT** libraries we need for this code. After that is the creation of the LoRa modem. Then we set up the variables for the `AppEUI` and `AppKey`. **Note that you need to add the AppEUI and AppKey from {{% tts %}} between the double quotes**. Finally we setup the DHT component with `6` as the pin and `DHT11` as the sensor type.

```cpp
#include <MKRWAN.h>
#include <DHT.h>

LoRaModem modem(Serial1);

String appEui = "";
String appKey = "";

DHT dht(6, DHT11);
```

In the `void setup ()` we initialize the Serial communication first. Then we set up the modem to use the correct regional band (**Note that you need to change this to whichever region you're in**). Using `dht.begin()` we initialize the dht component. Finally using `modem.joinOTAA` the device will join the LoRaWAN network.

```cpp
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};

dht.begin();

int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
}
```

In the `void loop()` we initialize the variables containing the values of Temperature and Humidity. We multiply these values by 100, since we can not send decimal numbers directly over LoRaWAN. Using the `byte payload` function we contain all the values we need for sending. Then using `mode.write` we finally send the package off to the network, ready to be received by {{% tts %}}.
Next is the code itself. You can find it below, but you do need to change a few things.

```cpp
void loop() {
int t = dht.readTemperature() * 100;
int h = dht.readHumidity() * 100;
First you have to **add the AppEUI and AppKey from {{% tts %}} between the double quotes**.
Copy link
Contributor

@nejraselimovic nejraselimovic Jun 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually meant in-code comments, such as:

String appEui = "123456789ABCDEF"; #Fill in with your AppEUI

Something like that, just to make it a no-brainer. Same thing for regional band part

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt like users would not notice that but sure ill just say "check the in-code comments"


byte payload[4];
payload[0] = highByte(t);
payload[1] = lowByte(t);
payload[2] = highByte(h);
payload[3] = lowByte(h);

Serial.println("Temperature: ");
Serial.println(dht.readTemperature());
Serial.println("Humidity: ");
Serial.println(dht.readHumidity());

modem.beginPacket();
modem.write(payload, sizeof(payload));
modem.endPacket(true);
delay(100000);
}
```

### Entire code
After that you **might need to change the regional band in the void setup** at `if (!modem.begin(EU868))`.

```cpp
#include <MKRWAN.h>
Expand Down Expand Up @@ -151,32 +89,35 @@ void loop() {
delay(100000);
}
```
## Uploading the code

Now to upload the code to the MKR WAN 1310. First plug the Arduino into your computer. In the IDE, click on **Select Board** at the top, and select your **Arduino MKR WAN 1310**.
Now the code needs to be uploaded to the MKR WAN 1310. First plug the Arduino into your computer. In the IDE, click on **Select Board** at the top, and select your **Arduino MKR WAN 1310**.

{{< figure src="../select-board.png" alt="Selecting Board" >}}

Once that is done you can upload the file to the board (Using the **->** button). Once the code is uploaded successfully, open the Serial Monitor. You can open it by clicking the **icon in the top-right corner**.

{{< figure src="../serial-monitor.png" alt="Serial Monitor button highlighted" >}}

Here you can check if the temperature and humidity values are showing up correctly.
Temperature and humidity values should be listed in the Serial Monitor.

{{< figure src="../serial-monitor-temp.png" alt="Serial Monitor" width="55%" >}}

## {{% tts %}}
{{< note >}}
If the Arduino IDE Serial Monitor does not show DHT sensor data, make sure to double-check the wiring.
{{</ note >}}


## Decoding Temperature and Humidity data

Now that the device is working go back to {{% tts %}} and set up the payload formatter. A payload formatter will turn the hexadecimal string received into readable text. To do this, in your application on {{% tts %}}, click on **Payload formatters**.

{{< figure src="../stack-pf.png" alt="payload formatter highlighted" width="70%" >}}

Make sure that **Custom Javascript formatter** is selected here from the dropdown.
Select **Custom Javascript formatter** from the dropdown.

{{< figure src="../custom-js.png" alt="custom javascript selected in dropdown" width="70%" >}}

Now in the **Formatter code** field, replace it with this formatter:

Now paste the following code in the **Formatter code** field:
```js
function decodeUplink(input) {
// Read the temperature and humidity from the payload
Expand All @@ -192,14 +133,8 @@ function decodeUplink(input) {
}
```

Now if you go back to the **Live Data** tab you'll see the temperature and humidity show up!
If you go back to the **Live Data** tab you'll see the readable temperature and humidity values showing up!

{{< figure src="../live-data.png" alt="Live data tab">}}

This concludes the example project. Now go create your own project!

## Troubleshoot

**My DHT sensor does not display any data**
- Make sure that you have wired it up properly.
- If you are using a bare DHT11, make sure that it has a pull up resistor between Vcc and Signal. Consult google for more info.
This concludes our example project. Now you can proceed with creating your own project! Make sure to check our [integrations](https://www.thethingsindustries.com/docs/integrations/) that can help you visualize your data, set up monitoring and alerting, etc.
Loading