Push the button

Estimated time: ~90 minutes

Wire up!

You will need the following items:

  1. EE02 breakout board.
  2. A pushbutton switch. A tactile/momentary switch is best for this purpose.
  3. Hookup wire.

Using a breadboard

  1. Insert the pushbutton switch into the breadboard. If your switch have more than two pins, you have to make sure that you get the orientation correct.
  2. Add one wire that connects one side of the switch to EE02 Gnd female header.
  3. Add one wire that connects the other side of the switch to EE02 pin 31 female header.

Using a soldering iron

Steps:

  1. Solder a length of wire to each lead on the switch.
  2. Insert one of the wires into the Gnd female header.
  3. Insert the other wire into the pin 31 female header.

Set device credentials

You will need a set of unique IDs that identifies your application and your device. You will have to register your device in Cloud Connect in order to get these IDs. In this tutorial, we will count the number of times the button has been pressed and then send this information to Cloud Connect. We will also add a widget that can display the number of clicks. Since the LoRa payload is just a set of bytes, we will need to describe how to decode these bytes. We will do this by specifying an uplink transform. This has to be specified as an uplink transform.

  1. Log into Cloud Connect (https://kent.cc.telenorconnexion.com)
  2. Create a new thing type. Give it a label, a description and select a domain and the following uplink transform:
var parser = new Parser()
   .endianess('big')
   .uint16('Clicks');
  return parser.parse(payload);
  1. Click “Save”. Click on your new thing type in the left side bar. Then click “+THINGS” to add your thing.
  2. Specify 1 as “the number of things to add” and select a domain. Click the “Create” button.
  3. Click the “+WIDGETS” button. Add a new widget with the “Files” type and give it a name. This widget will contain a file named “lora_job.h”. Download this file and replace your existing lora_job.h. Verify that LORAWAN_OTAA is defined.
#define LORAWAN_OTAA 1

Button click code

Clone the nrf52-lora repository and use this as a starting point. This already contains the necessary code for communicating via LoRa (https://github.com/comoyo/nrf52-lora)

Define pin 31 as our button pin. Open “config/nrf52_pins.h” and find the #ifdef EE02 block. Add the following code inside this block.

    // Pin that will be grounded when our switch is pressed
    #define MY_BUTTON_PIN 31

Create a new header file in the jobs folder. Name the file “button_job.h”. Copy the contents into the file and save it.

#ifndef _BUTTON_JOB_H_
#define _BUTTON_JOB_H_

#include "nrf_drv_gpiote.h"

// ButtonHandler is our interrupt handler. 
// This function will be called when a pin change interrupt occurs for "pin"
void ButtonHandler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action);
#endif

We now have to implement our function. Create a new implementation file in the jobs folder. Name the file “button_job.c”. Copy the contents into the file and save it.

#include "button_job.h"

uint16_t nClicks = 0;

void ButtonHandler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t  action)
{
    nClicks++;
    NRF_LOG_PRINTF("Button is clicked %d times\n", nClicks);
}

We now have to set up an interrupt handler for our button click. We do this inside th function gpio_config(). Open the file lmic/nrf52/hal.c and navigate to the end of gpio_config(). Add the following code.

    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;
    APP_ERROR_CHECK(nrf_drv_gpiote_in_init(MY_BUTTON_PIN, &in_config, ButtonHandler));
    nrf_drv_gpiote_in_event_enable(MY_BUTTON_PIN, true);

Since our interrupthandler is defined in another file, we have to include it. Add the following include after the other includes lmic/nrf52/hal.c.

#include "button_job.h"

We also have to include our new c-file in the Makefile. Open the makefile and navigate to the main-target. Add the following line after the other jobs.

	$(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c jobs/button_job.c	

We now have code that should increment our variable each time the button is pressed.

Before we move on to the next step, we should check that our code works properly. Open a terminal and navigate to the root folder of the nrf52 project.

Type “make” in the shell and press enter. It should compile without errors.

Test it!

We now have to wire up the EE02 brakout to the programmer.

  1. Connect the EE02 to connector J19 on the nrf52 development kit with the programming cable
  2. Connect USB power to the EE02
  3. Connect USB power to the development kit
  4. Power on the development kit. You should now have access to a new disk (JLINK1)
  5. Type “make deploy” in the terminal. This will copy the compiled image to the JLINK disk.
  6. Switch off the development kit and power cycle the EE02 by disconnecting/reconnecting the USB cable. Switch on the development kit and open another terminal window.

In terminal window #1: Type the following command and press enter.

JLinkExe -if swd -device nrf52 -speed 4000

This will give you a new shell. Type “connect” and press enter.

In terminal window #2: Type the following command and press enter.

JLinkRTTClienT

Now, you deserve a cup of coffe. Go fetch one and take a sip. When you are ready, you can now press the button a couple of times. While you do so, pay attention to the log in terminal window #2. If you see the following text, you’re ready to proceed:

Send LoRaWAN payload

Open the jobs/lora_job.c file and navigate to the SendFrame() function. Immediately before this function, add the following line:

extern uint16_t nClicks;

Replace the contents of the SendFrame() function with the following code:

static void send_frame(void) {
    uint32_t byte = 0;
   
    LMIC.frame[byte] = nClicks >> 8;
    byte++;
    LMIC.frame[byte] = nClicks & 0xFF;
    byte++;

    NRF_LOG_PRINTF("LoRa: Sending %d bytes\n", byte);
  
    int port = 15;
    switch (freq++) {
        case 0:
            port = 12;
            break;
        case 1:
            port = 11;
            break;
        case 2:
            port = 10;
            break;
        case 3:
            port = 9;
            break;
        case 4:
            port = 8;
            break;
        default:
            port = 7;
            freq = 0;
            break;
    }
    if (LMIC_setTxData2( port, LMIC.frame, byte, LORAWAN_CONFIRMED_MSG_ON ) != 0) {
        NRF_LOG_PRINTF("LoRa: *** Error sending frame!\n");
    }
}

Deploy and verify

Now you can run “make deploy” and power cycle your device. You wil have to reconnect JLINK and JLinkRTTClienT if you want to see any debug output.

After it has joined, it will start sending data to Cloud Connect. If you select the “List” tab for your thing type, you should see a green cloud on the far right.

You can now create a widget in the control panel for your thing. Click “+WIDGET”, give it a name, select “value” as type, and “Clicks” (from our transform) as the resource.