Overview

Skill Level: Advanced

This implementation helps extend the range of an IoT Gateway device and connect LoRa or Packet radio capable devices to send data in string format to Watson IoT platform. It makes use of the WiFi connectivity to communicate with the Gateway.

Ingredients

This recipe assumes the following -

- A Raspberry Pi module is connected to the local network (via WiFi or Ethernet) and the same network is reachable via WiFi.

- The Raspberry Pi module is registered with Watson IoT platform.

- An MQTT Broker such as mosquitto is running on it and listening on port 1883.

 

The diagram below depicts the location of the component this article describes in the chain of communication from the sensor to the IoT cloud -

LoRa-Capability-1

The component in development here is a MicroController Unit (MCU) which has both WiFi and LoRa radio communication capability thus serving as a bridge between the communicating sensor / device and the IoT MQTT Gateway.

Disclaimer : The example created here is for 433MHz band which is the European license free band for radio communication. For other countries approved and compatible radio devices are to be used.


 

The following hardware are needed for this implementation - 

- Adafruit Feather HUZZAH with ESP8266 WiFi

- RFM9X LoRa Radio Transceiver Breakout

- Jumper wires for connection

- Micro USB cable

- An 830 point Breadboard (For hosting the test circuit)

- 433 MHz antenna

- Soldering iron and soldering wire for connecting the antenna wire to the Radio breakout board

Optional (but used in this example here) :

- An SSD1306 compatible graphic display (128x64 OLED)

- A LoRa transmitter for testing the system.

 

Software requirements -

- Arduino IDE 1.8

 

Working principle : The MCU module ESP8266 has built in WiFi hence that is coded to connect to the local WiFi access point using WPA2 secure connection. Once the WiFi is initialized and connected, the next step is to establish the MQTT connection and publish to a specific topic which is subscribed by a suitable MQTT client also connected to the Gateway device. After MQTT client connection is established the next step is to initialize the LoRa radio module. This example makes use of the Radiohead third-party API for communicating with the radio module. Frequency is set to 433 MHz and radio listens to any sender on the same band. Once data is received, it sends out an acknowledge string for the transmitter to understand that the data packets were received. The received string is then published via the topic string to the Gateway. In this example the topic string is "home/lora/json".

Step-by-step

  1. Wiring

    Wire the connections and the components to the breadboard as shown in the diagram below. Solder the antenna with a wire to the radio board –

    PiLoRaAddOn

     

  2. Coding - Initial Setup

    The sketch makes use of the following APIs :

    – Arduino JSON

    – Adafruit SSD1306, GFX, MQTT

    – Radiohead RF9x

    The following libraries are included in this sketch – 

    Include 

    The first few information that needs adjusting are the following –

    WiFiCredential

     Note that this sketch uses the IP address of the MQTT Gateway hard coded in the snippet. Hence any changes to the IP address would require recompiling and reloading the code to the MCU.

  3. Coding - Step by Step - 1 - OLED Display

    For the optional OLED display this subroutine initializes the same with an additional one for easy display calls :

    OLED-Init

  4. Coding - Step by Step - 2 - WiFi

    The following snippet defines and configures the WiFi connection with help from the string constants shown in the previous step :

    WiFiInit

  5. Coding - Step by Step - 3 - MQTT

    The following snippet initializes the MQTT client. Note the topic URL used :

    MQTTInit

  6. Coding - Step by Step - 4 - LoRa

    The following snippet initializes LoRa radio and sets the frequency to 433 MHz :

    LoRaInit-1

  7. Coding - Step by Step - 5 - Main routines

    Following snippet initializes the MCU on reboot and initializes the Serial interface for debugging purposes :

    init

    Please note that the JSON Buffer object defined here is not used in this sketch but would be needed is processing on the received string is needed before relaying the same to the gateway.

     The following snippet is the main loop in which the radio listens to incoming data packets and acknowledges any reception then relays the data to the MQTT Gateway.

    loop

    Combine all the code together and prepare to build the same in the next step.

  8. Build

    Before compiling the code in Arduino IDE make sure that the following board configuration is configured :

    BoardConfig-1

    Once compiled the IDE shows the following message :

    Compiled-Code-1

    Connect the MCU module to the PC running the Arduino IDE with this sketch and transfer the code. Once loaded the MCU would auto boot and load the program.

  9. Testing

    Once loaded the OLED display shows various step texts in sequence and finally when all init subroutines are successfully completed it would display ‘LoRa Listening…’ as shown in the picture below :

    IMG_2741-1

    Testing should be done with help from a LoRa Transmitter module sending data on the same frequency (433 MHz) as the receiver.

     

    Source code of this project can be found here : 

    https://github.com/tanmoydutta/loragateway.git

Join The Discussion