Developing a Wind Speed/Direction Sensor using Seeed's LoRa-E5 module for a LoRaWAN network

Description

This project covers the process of developing a Seeed LoRa-E5 based Arduino wind speed/direction sensor and housing it in an outdoor enclosure capable of being attached to the weather platform on the rooftop of Digi-Key’s corporate headquarters building. The project also covers adding the sensor node to an existing ChirpStack based private LoRaWAN network and Machinechat’s JEDI Pro IoT software platform. The LoRa sensor uses a Seeeduino Xiao, a Grove LoRa-E5 radio board, and the anemometer and wind vane hardware from Sparkfun’s Weather Meter Kit. The LoRaWAN network uses Seeed’s IP67 rated industrial LoRaWAN gateway to forward the LoRa sensor packets to a private ChirpStack LoRaWAN network server running on a Seeed’s ReServer (with Ubuntu linux installed). Machinechat’s JEDI Pro IoT platform runs on the same ReServer.

Hardware

  • Seeed reServer
    reServer is based on an ODYSSEY X86 v2 board and powered by Intel® Core™ 11th Gen. i3 and Intel UHD Graphics 48EUs
  • Seeed SenseCAP Outdoor Gateway - LoRaWAN US915MHz
    SenseCAP LoRaWAN Gateway is based on LoRaWAN® protocol, applicable for low-power, long-distance environmental data collection and monitoring.
  • Seeed Grove LoRa-E5 board
    STM32WLE5JC, SX126x LoRa RF Grove Platform Evaluation Expansion Board
  • Seeed Seeeduino Xiao board
    SAMD21G18 Seeeduino XIAO series ARM® Cortex®-M0 MCU 32-Bit Embedded Evaluation Board
  • Sparkfun Weather Meter Kit
    The kit provides the three core components of weather measurement: wind speed, wind direction and rainfall (note: this post uses the anemometer and wind vane sensors, the rain gauge sensor is planned to be covered in a future post).

Software

  • JEDI Pro or JEDI Pro SSE
    Adaptable software for IoT data collection, visualization, monitoring and data storage that can be integrated into IoT solutions. Capabilities include: collect data from sensors, devices and machines; build intuitive real-time and historical data and system view dashboards; create rules to monitor and respond to data conditions automatically; receive alert notifications by email and SMS. JEDI Pro SSE is the Seeed Studio Edition version of JEDI Pro that adds a Data Collector for Seeed’s line of SenseCAP LoRaWAN sensors
  • ChirpStack
    The ChirpStack open-source LoRaWAN Network Server stack provides open-source components for LoRaWAN networks. The modular architecture makes it possible to integrate within existing infrastructures.
  • Arduino
    Arduino is an open-source electronics platform based on easy-to-use hardware and software.

Background

This post is a follow-on project that builds on the related TechForum post Set up a private LoRaWAN Sensor Network with Machinechat and Seeed SenseCAP which details setting up a private LoRaWAN IoT sensor network using off the shelf hardware and software available at Digi-Key. Software used in the related project included Machinechat’s JEDI Pro applications software and ChirpStack’s LoRaWAN network server application. Hardware used in the project included a Seeed reServer x86 server and SenseCAP outdoor LoRaWAN gateway. For this post a Seeeduino Xiao, Grove LoRa-E5 board, and Sparkfun weather meter kit are set up to report wind speed and direction over a LoRaWAN network.
Sparkfun Weather Meter Kit Anemometer and Wind Vane
Wind speed is measured by the cup-type anemeoter that rotates a magnet causing a contact to close once per revolution. A contact closure of once per second correlates to a wind speed of 1.492 MPH (2.4 km/h). The contact closure can be monitored via the two inner conductors of the RJ-11 connector (pins 2 and 3) on the wind vane cable.
Wind direction is determined by eight switches on the wind vane, each with their own resistor. A magnet in the wind vane causes the various switches to close as the vane direction changes. An external resistor is used to set up a voltage divider so that the voltage output can be monitored to determine the position of the vane. The two outer conductors of the RJ-11 connector (pins 1 and 4) are used for the wind vane. (note: detailed information is available in the Sparkfun Weather Meter Hookup Guide)
Digi-Key Weather Platform
Details on the Digi-Key weather platform can be found at link Digi-Key Weather Platform .

Implementation

For the project, an external USB 5V power supply powers the Seeeduino Xiao and the Xiao’s 3V3 output powers the Grove LoRa-E5 board and weather sensor hardware. The Xiao UART is connected to the TX/RX pins of the Grove LoRa-E5 and Xiao input pins 1 and 2 connected to the wind sensor anemometer and wind vane.
The Xiao and LoRa-E5 boards are mounted in an outdoor enclosure (Hammod Manufacturing part# RL6225) and the +5V power and wind sensor anemometer/vane attached using M8 panel mount connectors and cable assemblies.
The Arduino application code runs on the Seeeduino Xiao to send AT commands to the LoRa-E5 and monitor input pins 1 and 2. The application joins the LoRa network, monitors the input pins to calculate wind speed/direction data, encodes the data in CayenneLPP format, sends the data over LoRa, delays and loops back for next reading. Below schematic diagram illustrates how the circuit is wired up and implemented (see Scheme-it Project: LoRaE5_Xiao_WindSensorM8cables).

Electrical connections between the modules and wind sensor connector J1 are shown below:

Seeeduino Xiao pin Grove LoRa-E5 connector Wind Sensor Connector J1
GND GND 1
3V3 VCC 2
1 4 (wind vane)
2 3 (anemometer)
6 RX
7 TX

Set up the Seeeduino Xiao, Grove LoRa-E5 and Anemometer/Wind Vane

1 - Set up Arduino on the Seeeduino Xiao. See link Getting started with Seeeduino Xiao

2 - Install libraries needed for application. Add these libraries thru Arduino’s Library Manager:

3 - Code walkthrough (filename: Xiao_LoraE5CayenneLPP_WindRev1.ino)

Initial setup

// below code is based on Seeed LoRaE5 example code and modified to NOT use the display on the Seeeduino Xiao expansion board (just uses a Xiao connected to a LoRaE5 
// Grove expansion board. Seeed example code at https://wiki.seeedstudio.com/Grove_LoRa_E5_New_Version/
//
//note: all Seeed LoRaE5 Grove boards have example code App key of "2B7E151628AED2A6ABF7158809CF4F3C" so needs to be changed
// this version changes out the DHT11 sensor for Sparkfun weather meter kit for wind speed and direction

// Anemometer measuring code based on below:
//More Information at: https://www.aeq-web.com/
//Version 2.0 | 11-NOV-2020
//
//SBR modifications
//SensorPin is connected to RC pulldown circuit on anemometer switch
//Wind vane pin is connected to voltage divider circuit for vane resistors


#include <Arduino.h>
#include <CayenneLPP.h>


// anemometer and direction parameters
const int RecordTime = 3; //Define Measuring Time (Seconds)
const int SensorPin = 2;     // the number of the sensorpin
const int VanePin = 1;      // pin# connected to wind vane
const int ledPin =  13;      // the number of the LED pin
int InterruptCounter;
float WindSpeed;
float DirWind;   //wind direction voltage
float angleWind; //wind vane angle
  

CayenneLPP lpp(51);   //setup Cayenne LPP (low power payload) buffer - per documentation 51 bytes is safe to send
 
static char recv_buf[512];
static bool is_exist = false;
static bool is_join = false;
static int led = 0;

int buf_size;  //Cayenne LPP buffer payload size
int Pointer;   //pointer used in Cayenne LPP buffer
int Offset = 12;    //offset to where Cayenne LPP payload data starts
int Loop1;       //loop counter in LoRa payload builder
int Loop2;       //loop counter in LoRa payload builder
int Loop3 = 0;       //loop counter in LoRa parameter send

 
static int at_send_check_response(char *p_ack, int timeout_ms, char *p_cmd, ...)
{
    int ch;
    int num = 0;
    int index = 0;
    int startMillis = 0;
    va_list args;
    memset(recv_buf, 0, sizeof(recv_buf));
    va_start(args, p_cmd);
    Serial1.printf(p_cmd, args);
    Serial.printf(p_cmd, args);
    va_end(args);
    delay(200);
    startMillis = millis();
 
    if (p_ack == NULL)
    {
        return 0;
    }
 
    do
    {
        while (Serial1.available() > 0)
        {
            ch = Serial1.read();
            recv_buf[index++] = ch;
            Serial.print((char)ch);
            delay(2);
        }
 
        if (strstr(recv_buf, p_ack) != NULL)
        {
            return 1;
        }
 
    } while (millis() - startMillis < timeout_ms);
    return 0;
}
 
static void recv_prase(char *p_msg)
{  
    if (p_msg == NULL)
    {
        return;
    }
    char *p_start = NULL;
    int data = 0;
    int rssi = 0;
    int snr = 0;
 
    p_start = strstr(p_msg, "RX");
    if (p_start && (1 == sscanf(p_start, "RX: \"%d\"\r\n", &data)))
    {
        Serial.println(data);
        led = !!data;
        if (led)
        {
            digitalWrite(LED_BUILTIN, LOW);
        }
        else
        {
            digitalWrite(LED_BUILTIN, HIGH);
        }
    }
 
    p_start = strstr(p_msg, "RSSI");
    if (p_start && (1 == sscanf(p_start, "RSSI %d,", &rssi)))
    {
         Serial.println(rssi);
    }
    p_start = strstr(p_msg, "SNR");
    if (p_start && (1 == sscanf(p_start, "SNR %d", &snr)))
    {
        Serial.println(snr);
    }
}

 
void setup(void)
{
    Serial.begin(9600);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);

    // anemeometer and wind vane setup
    pinMode(SensorPin, INPUT); //use input to count interrupts of anemometer
    pinMode(VanePin, INPUT); //use input to read analog voltage of wind vane output
 
    Serial1.begin(9600);                  //UART serial1 connection to LoRaE5
    Serial.print("E5 LORAWAN TEST\r\n");
 
    if (at_send_check_response("+AT: OK", 100, "AT\r\n"))
    {
        is_exist = true;
        at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
        at_send_check_response("+MODE: LWOTAA", 1000, "AT+MODE=LWOTAA\r\n");
        //at_send_check_response("+DR: EU868", 1000, "AT+DR=EU868\r\n");  //original
        at_send_check_response("+DR: US915", 1000, "AT+DR=US915\r\n"); 
        //at_send_check_response("+CH: NUM", 1000, "AT+CH=NUM,0-2\r\n");  //original
        at_send_check_response("+CH: NUM", 1000, "AT+CH=NUM,8-15,65\r\n"); // configure channels to match chirpstack
        at_send_check_response("+KEY: APPKEY", 1000, "AT+KEY=APPKEY,\"2B7E151628AED2A6ABF7158809CF4F3C\"\r\n");
        at_send_check_response("+CLASS: C", 1000, "AT+CLASS=A\r\n");
        at_send_check_response("+PORT: 8", 1000, "AT+PORT=8\r\n");
        delay(200);
        is_join = true;
    }
    else
    {
        is_exist = false;
        Serial.print("No E5 module found.\r\n");
    }
 
}
 

Main loop - determine wind speed and direction, encode in CayenneLPP format and send as LoRa payload data

void loop(void)
//void loop()
{
     
    // measure windspeed
    measure();
    // measure wind vane voltage
    DirWind = analogRead(VanePin)*3.33;
    // calculate wind direction in degrees
    vaneAngle();
    
    
    Serial.print("Windspeed: ");
    Serial.print(WindSpeed);
    Serial.print(" mph ");
    Serial.print("Wind Direction: ");
    Serial.print(angleWind);
    Serial.println(" degrees");


//***************** start of Cayenne LPP code **************************
    // due to character byte limitatations in LoRa payload buffer, need to alternate between 
    // the two wind parameters when sending out parameter payload
    
    Serial.print("Loop3 = ");  //debug code
    Serial.println(Loop3);    //debug code

    if (Loop3 == 0)
    {
      lpp.reset();
      lpp.addAnalogOutput(1, WindSpeed);  //channel 1, windspeed value
      Loop3 = 1;
    }
    else
    {
      lpp.reset();
      lpp.addAnalogOutput(2, angleWind);  //channel 2, wind direction value
      Loop3 = 0;
    }
   

    buf_size = lpp.getSize();
    Serial.print("Cayenne LPP buffer size = ");
    Serial.println(buf_size);

    uint8_t *payload = lpp.getBuffer();
    char cmd[128];
    
    // Lmsg is just test code that probably can be deleted?
    char Lmsg[4];
    for (unsigned char i = 0; i < lpp.getSize(); i++)
    {
      sprintf(Lmsg, "%02X", (char)payload[i]);
      Serial.print(Lmsg);
    }

    Serial.println(" payload"); 

//**************end of payload builder code**************************

    // start of LoRa communication code 
    if (is_exist)
    {
        int ret = 0;
        if (is_join)
        {
 
            ret = at_send_check_response("+JOIN: Network joined", 12000, "AT+JOIN\r\n");
            if (ret)
            {
                is_join = false;   //resets join check?
            }
            else
            {
                at_send_check_response("+ID: AppEui", 1000, "AT+ID\r\n");
                Serial.print("JOIN failed!\r\n\r\n");
                delay(5000);
            }
        }
        else
        {
            char cmd[128]; 
            //sprintf(cmd, "AT+CMSGHEX=\"%04X%04X\"\r\n", (int)temp, (int)humi); //original
            //****** add in CayenneLPP ecoding ******
            sprintf(cmd, "AT+CMSGHEX=\"%02X\"\r\n", (char)payload[0]);  //first part of confirmed message
            Serial.println("debug - print cmd at start of loop");
            Serial.println(cmd);
            // add data payload to LoRa message by looping thru Cayenne LPP data buffer
            char TestMsg[2];   
            for (Loop1 = 0; Loop1 < buf_size; Loop1++)
              {
                Pointer = (Loop1*2) + Offset;
                sprintf(TestMsg, "%02X", (char)payload[Loop1]);
                // write data buffer character to LoRa message
                for (Loop2 = 0; Loop2 < 2; Loop2++)
                  {
                     cmd[Loop2 + Pointer] = TestMsg[Loop2];
                  }
              }            
            // create end of message characters for LoRa message (need ",return,null characters)
            char EndMsg[20];
            sprintf(EndMsg, "test\"%02X\"\r\n", (char)payload[0]);
            // add ", return and null characters to end of LoRa message
            for (unsigned char i = 2; i < 7; i++)  
              {
                cmd[Pointer + i] = EndMsg[5 + i];
              }            
            Serial.println("debug - print cmd at end of loop");
            Serial.println(cmd);
            //******** end of CayenneLPP encodeding *********
            ret = at_send_check_response("Done", 12000, cmd);        //sends cmd command over LoRa - increase time from 5000 to 12000
            if (ret)
            {
                recv_prase(recv_buf);
            }
            else
            {
                Serial.print("Send failed!\r\n\r\n");
            }
            delay(5000); 
        }
    Serial.println(" in main loop checking LoRa then wait 5 minutes");   
    delay(300000); //main delay 300 seconds 
    }
    else
    {
        delay(1000);
    }
}

Sensor subroutines

//count anemometer pulses and calculate windspeed
void measure() {
  InterruptCounter = 0;
  attachInterrupt(digitalPinToInterrupt(SensorPin), countup, RISING);
  delay(1000 * RecordTime);
  detachInterrupt(digitalPinToInterrupt(SensorPin));
  WindSpeed = (float)InterruptCounter / (float)RecordTime * 2.4;
}

void countup() {
  InterruptCounter++;
}

//determine wind direction angle based on measured wind vane voltage
void vaneAngle() {
  angleWind = 99.9;
  if (DirWind < 270)
    {
      angleWind = 112.5;
    }
  else if ((DirWind >= 270) and (DirWind <= 310))
    {
      angleWind = 67.5  ;
    } 
   else if ((DirWind > 310) and (DirWind <= 400))
    {
      angleWind = 90.0  ;
    }    
  else if ((DirWind >= 400) and (DirWind < 600))
    {
      angleWind = 157.5  ;
    }    
  else if ((DirWind >= 600) and (DirWind < 750))
    {
      angleWind = 135.0  ;
    }    
  else if ((DirWind >= 750) and (DirWind < 850))
    {
      angleWind = 202.5  ;
    }    
  else if ((DirWind >= 850) and (DirWind < 1150))
    {
      angleWind = 180.0  ;
    }    
   else if ((DirWind >= 1150) and (DirWind < 1450))
    {
      angleWind = 22.5  ;
    }    
   else if ((DirWind >= 1450) and (DirWind < 1900))
    {
      angleWind = 45.0  ;
    }    
  else if ((DirWind >= 1900) and (DirWind < 2050))
    {
      angleWind = 247.5  ;
    }    
   else if ((DirWind >= 2050) and (DirWind < 2250))
    {
      angleWind = 225.0  ;
    }    
  else if ((DirWind >= 2250) and (DirWind < 2500))
    {
      angleWind = 337.5;
    }
  else if ((DirWind >= 2500) and (DirWind < 2700))
    {
      angleWind = 0.0;
    }
  else if ((DirWind >= 2700) and (DirWind < 2850))
    {
      angleWind = 292.5;
    }
  else if ((DirWind >= 2850) and (DirWind < 3000))
    {
      angleWind = 315.0;
    }
  else if (DirWind >= 3000)
    {
      angleWind = 270.0;
    }
}  

Latest source code for theXiao_LoraE5CayenneLPP_WindRev1.ino application is on github at below link:

Determine Grove LoRa-E5 board device EUI

In Arduino, compile and upload Xiao_LoraE5CayenneLPP_WindRev1.ino code into Seeeduino Xiao and enable Serial Monitor. Review Serial Monitor output to determine Grove LoRa-E5 device EUI.
image

Add the LoRa-E5 based sensor node to the ChirpStack LoRaWAN network server

(note: this project and below steps assume that a ChirpStack based private LoRaWAN network is active and in range of the LoRa-E5 sensor node, if not refer to TechForum post Set up a private LoRaWAN Sensor Network with Machinechat and Seeed SenseCAP)

1 - In ChirpStack, select Device-profile and Create. Name Device-profile “Seeed LoRaE5”, Select “1.0.2” for LoRaWAN MAC version, Select “A” for LoRaWAN Regional Parameters version, Select “Default ADR algorithm” for ADR algorithm, and enter “3600” for Uplink interval. In JOIN(OTAA/ABP) tab, check box for “Device supports OTAA”. In CODEC tab, select “Cayenne LPP” in CODEC dropdown list.

2 - In ChirpStack, select Applications, then select “FarmTest” and then select Create. Enter “LoRaE5wind” for Device name, enter “description” for Device description, enter Device EUI for Grove LoRa-E5 board (determined from above step “Determine Grove LoRa-E5 board device EUI”), enter “STM32WL Sensors” for Device-profile, and select CREATE DEVICE. (note: for initial testing and demonstration you may want to check Disable frame-counter validation box)

3 - Add Application key for Device. Enter Application key “2B7E151628AED2A6ABF7158809CF4F3C” (note: this is default key in LoRa-E5, to change refer to KEY section of LoRa-E5 AT Command Specification) and select SET DEVICE-KEYS.

Set up and test ChirpStack HTTP Integration with JEDI Pro Generic LoRaWAN Custom Data Collector

ChirpStack is modified to add HTTP integration for forwarding LoRaWAN meta and sensor data to a specified IP address. Machinechat’s Generic LoRaWAN Custom Data Collector Plug-in is used for listening to the specified IP address and parsing the LoRaWAN data for review (when debug enabled) and use in the JEDI Pro platform.
(note: The Custom Data Collector consists of two files, lorawan-linux.bin and config.yml that are available from Machinechat at: https://support.machinechat.io/hc/en-us/articles/6046199010327-Generic-LoRaWAN-Custom-Data-Collector-Beta-for-JEDI-PRO-Linux )

1 - Enable HTTP Integration in ChirpStack.
Select “Add” in ChirpStack Integrations screen.

2 - Configure HTTP Integration
Select “JSON” for Payload marshaler , add IP address (use same IP as in config.yml file) for Endpoint URL , and select ADD INTEGRATION

3 - Copy lorawan-linux.bin and config.yml files to the ~/jedi/plugins directory where JEDI Pro is installed on the Ubuntu Linux Mini-PC. Modify config.yml file to enable debug and specify IP listening address.
(note: if you have previously installed the lorawan-linux.bin and config.yml files for a different sensor, all you need to do is edit config.yml as shown in step 5 to add the info for the wind speed and wind angle parameters)

4 - Run Custom Plug-In on command line using “./lorawan-linux.bin ./config.yml” on terminal in Ubuntu Linux Mini-PC and monitor output data. Data should look similar to below (note: remember to make lorawan-linux.bin file executable):

5 - Edit config.yml file to map LoRaWAN data to JEDI Pro data parameters and disable debug. For this project example, edit propertyNames so LoRaWAN cSproperty: “anslogOutput.1” is mapped to mcProperty: “WindSpeed” and LoRaWAN cSproperty: “analogOutput.2” is mapped to mcProperty: “WindAngle”. Disable debugging by setting “setDebug:” to false and save file.

Set up JEDI Pro Custom Data Collector and Data Dashboard

In JEDI Pro, select “Settings” tab, then select “Data Collectors” and select “Add Collector”. (note: if you have previously added the LoRaWAN custom data collector to your JEDI application this step is not necessary)

Configure Collector as shown below. Name Data Collector “LoRaWAN” (or whatever you prefer), select Collector Type as “Custom Plug-In”, select “lorawan-linux.bin” as Plug-In Executable file, enter location of config.yml file (example: “/home/scottr/jedi/plugins/config.yml” for Plug-in Options, select check box for “Run As Background Process and Monitor” , then select “VALIDATE PLUG-IN” to verifiy functionality.

In JEDI Pro, select “Data Dashboards”, then select “+” to add a new chart.

Configure data charts for WindSpeed and WindAngle and select “Add” to include in your Data Dashboard(see example below)

Conclusion

The combination of Arduino, Seeeduino Xiao, LoRa-E5 radio, and Sparkfun’s weather meter kit provides a flexible wind sensor platform that provides wind speed and direction data over LoRa. ChirpStack’s HTTP Integration and Machinechat’s Generic LoRaWAN Custom Data Collector are then configured to bring the wind sensor data into JEDI Pro for IoT data collection, visualization, monitoring and data storage. The example code can be easily modified for other sensors as needed.

References