/avr64dd32-agri-iot-mplab-mcc

Create an Agricultural IoT Sensor Node with AVR DD and RN2xx3 LoRa Module

Primary LanguageCOtherNOASSERTION

MCHP

Agricultural IoT with the AVR64DD32 and the RN2xx3 LoRa® Module

This example shows how the AVR64DD32 can be used to create a LoRaWAN end node that monitors temperature, humidity, barometric pressure, and soil moisture and transmits that data every three minutes to an application via The Things Network (TTN). The node can be placed anywhere between ten meters and five kilometers from a LoRaWAN gateway in urban environments, but the range is wider for suburban locations. The design demonstrates how an end node, even at long range, can be configured to consume very low power/have long battery life, while securely moving valuable data. This design can be scaled easily to enable the capture of soil variability data by placing additional nodes throughout an agricultural plot without the need to add additional gateways or infrastructure.

This demo uses the RN2903A module which operates on the 915 MHz frequency band and is suitable for US and Australia based projects, but the project is easily adapted to work in other regions by selecting an alternate Click board™ and gateway for your region.

Related Documentation

Software Used

Hardware Used

Setup - End Node Hardware

Add the components to the Nano Adapter board.

Place the LR2 Click on MikroBUS™ 2 and the Weather Click on MikroBUS 1.

Connect the moisture sensor leads as shown in Figure 1.

Lastly, add a jumper wire between (PA1) and PD1 to allow the resetting of the LR2 Click. (PA1) is not connected by default (since it is already connected to an external clock), as a result this project enables the PD1 pin to drive the reset signal.

Jumper Wire Pin
VCC - Red wire from Moisture Sensor VCC
GND - Black wire from Moisture Sensor GND
AOUT - Yellow wire from Moisture Sensor PD2
(PA1) - Place a jumper between PD1

Hardware Setup

Figure 1. Hardware Setup

Setup - Software

End Node Environment Setup

  • Download, install and open MPLAB X IDE
  • Download and install the XC8 C Compiler
  • Open the 'avr64dd32-agri-iot-mplab-mcc.X' project as shown in Figure 2.

Open Project in MPLAB X

Figure 2. Open the Project in MPLAB X

Once the project has opened, begin by calibrating the sensors.

Calibrate the Sensors

To calibrate the moisture sensor, open the application.c file, and change the initial state to TEST_MOISTURE.

State Test Moisture

Figure 3. Setting the Initial State to TEST_MOISTURE

Later these settings can be made specific to your soil and plant needs, but for brevity, begin with the upper and lower bounds for measurements by testing in dry air and in a cup of water.

Place the sensor so it is suspended in dry air.

Sensor Positioned in Dry Air

Figure 4. Sensor Positioned in Dry Air

Start the Data Visualizer with the following settings:

Baud Rate: 9600

Char length: 8 bits

Parity: None

Stop Bits: 1

Click Send to terminal, then make and program the device.

send to terminal

Figure 5. Data Visualizer Send to Terminal

make and program the device

Figure 6. Make and Program the Device

Once the program starts, you will be prompted to press the button to generate an Analog-to-Digital Converter (ADC) read. Press the button a few times with the sensor in dry air, then place the sensor a cup of water and press the button a few more times.

Figure 7 shows two readings. When collecting dry air samples, the ADC reading is close to 3000. When collecting water samples, the reading is close to 1400.

calibrate-moisture

Figure 7. ADC Values in Dry Air and in a Cup of Water

Note your results and open the application.h file. Replace the defintions for IN_MAX and IN_MIN using your results, then use those values to calculate the CONVERSION_PERCENT using the formula shown.

soil-moisture-values

Figure 8. Soil Moisture Definitions

Only one small change is needed to calibrate the Weather Click; the rest is taken care of in software. To have an accurate pressure reading, the device needs to use your current elevation above sea level. Enter this value on line 24 of the bme.h file.

bme-elevation

Figure 9. Elevation for Weather click

Next, set up the TTN side of the project.

The Things Network Application Setup

If you haven't already done so, set up an an account with TTN; the community edition is free for fair use. The account set up begins with selecting a regional cluster, click here to get started.

After setting up your account, navigate to your console and click Go to applications.

Go to Applications

Figure 10. Application Setup

Click the Create application button and follow the instructions to create an application.

Create application

Figure 11. Create Application

Setup Application Uplink Payload Formatter

Click on Payload formatters then on Uplink.

Uplink formatter

Figure 12. Application Uplink Payload Formatter

Under Setup Formatter type*, select Custom Javascript formatter.

Uplink formatter

Figure 13. Select Custom Javascript formatter

Replace the default decodeUplink function with the following snippet:

function decodeUplink(input) {
    var data = {};
    data.temp = input.bytes[0];
    data.humidity = input.bytes[1];
    data.moisture = input.bytes[2];
    data.pressure = (input.bytes[3] + 900);
    
    return {
      data: data
    };
}

Figure 14. Decode Uplink Code Snippet

Click Save and return to the overview page.

Set up an End Device for Over the Air Activation (OTAA)

From the application overview page, click on Register end device button.

register end device

Figure 15. Registering an end device for OTAA

To register the device, a few details from the module are needed. Open the application.c file again and change the state to REGISTER.

register state

Figure 16. Changing the state to REGISTER

Start the Data Visualizer and build and program the device again. When the program runs, copy the HWEUI and note the LoRaWAN spec.

HWEUI and LoRaWAN Spec

Figure 17. End device details in Data Visualizer

Return to the Register end device window on TTN, and select Enter end device specifics manually. Choose your frequency plan based on your location and select the LoRaWAN specification. In the provisioning information section, enter all zeros for the JoinEUI field, then click Confirm.

device specifics manually

Figure 18. Registering a new device

Paste the HWEUI you copied earlier into the DEVEUI field then click Generate to generate a new AppKey.

paste hweui

Figure 19. Generate a new AppKey

The end device is now created and will need to have a payload formatter.

Set up the End Device Payload Formatter

The quickest way to set up the formatter is to use the Paste application formatter button. Click on the Payload formatters button, then select Custom javascript formatter option, and press the Paste application formatter.

end device formatter

Figure 20. Paste the application formatter

Update the Project with the HWEUI and Generated App Key

On TTN, open the end device overview page, there are two fields to copy and paste into the RN2xx3 header file for the project. Click the DevEUI and paste it into the HWEUI field in the RN2xx3.h definition. Copy the AppKey field and paste it into the APPKEY definition.

copy fields end device

Figure 21. Copy the DevEUI and AppKey to the clipboard

paste fields end device

Figure 22. Update the HWEUI and APPKEY definitions

Test on The Things Network

This example assumes that you either have public TTN gateway near you, or you have set one up on your own. Having your own gateway is very helpful for troubleshooting if you run into issues. If you want to set up your own gateway, two helpful guides can be found at: Hackster Project and RAK Developer Gateway.

Once you have access to a gateway, continue the test by opening the application.c file again and changing the state back to INIT.

state machine init

Figure 23. Change back to INIT

Start the Data Visualizer again and re-program the device.

init terminal

Figure 24. Start Data Visualizer

When the application starts, the modules will be initialized. Press the button to start the join TTN procedure.

Setting up the channel frequency plan will take about ten seconds to complete. Afterwards, if the join request is accepted, the device will send its first payload. If the device does not connect, some suggestions will be given. Common problems are being too close to a gateway, incorrect keys, or the gateway may have temporarily disconnected. Normally, you should see an output like the one in figure 25.

join accept

Figure 25. Output When Join Request is Accepted

For this project, the node sends new data every three minutes. Once per day, it sends the data as a confirmed payload as in this first transmission. Confirmed messages require an acknowledgment from the network, but also consume airtime on the gateway and extra battery life for the node. So the next 479 messages each day are sent as unconfirmed messages.

This project sends all confirmed messages on port 10 and unconfirmed messages on port 11. You can see in Figure 26 below showing the application live data window, that after joining and sending an initial confirmed message, the node subsequently sends an unconfirmed message to the application every three minutes.

ttn test

Figure 26. Confirmed Messages on Port 10 and Unconfirmed Messages on Port 11

Summary

This example shows how to set up a LoRaWAN end node using the AVR64DD32 with the MikroE LR2 Click, the MikroE Weather Click and a soil moisture sensor to share soil conditions at long range using low power.