element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Just Encase
  • Challenges & Projects
  • Design Challenges
  • Just Encase
  • More
  • Cancel
Just Encase
Blog Monitoring and Protection an Ecological Area #9 - Modifying Receiver Code for Multiple Sensors
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Just Encase requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
Author: guillengap
Date Created: 18 Jan 2022 11:58 PM
Views: 293
Likes: 5
Comments: 0
  • Arduino MKR WAN 1300
  • lora
  • Monitoring and Protection an Ecological Area
  • just_encase
Related
Recommended

Monitoring and Protection an Ecological Area #9 - Modifying Receiver Code for Multiple Sensors

guillengap
guillengap
18 Jan 2022

Table of Contents

  1. Project Introduction
  2. Getting Started
  3. Communication Between Two MKR WAN 1300
  4. LoRaWAN System Range Testing
  5. Assembling and Adding LCD Display to the Transmitter
  6. Assembling and Adding LCD Display to the Receiver
  7. LoRaWAN´s Field Test with a DS18B20 Sensor
  8. Adding Multiple Sensors: DS18B20, DHT22 and MQ-135
  9. Modifying Receiver Code for Multiple Sensors
  10. LoRaWAN´s Field Test with Multiple Sensors
  11. Connecting LoRaWAN to an IoT Provider through the Arduino NANO 33 IoT
  12. LoRaWAN and IoT Connection with a DS18B20 Sensor
  13. Testing LoRaWAN and IoT Connection with a DS18B20 Sensor
  14. LoRaWAN and IoT Connection with Multiple Sensors
  15. Testing LoRaWAN and IoT Connection with Multiple Sensors
  16. Project Report Updated

**********************************************************************************************************************

Modifying Receiver Code for Multiple Sensors

Receiver Schematic Diagram

In the figure below I show you the electrical diagram of the receiver device

As you can see, there are no hardware changes, however the code must have changes since we will receive data from three sensors and these must be separated and shown on the display. All this through code. In the figure below I show you the transmitter and the receiver.

Receiver code

There were no hardware changes on the receiving device. However, in the software there were changes, and these are shown in the code below:

LoRaReceiver_v3.ino

// AUTHOR: GUILLERMO PEREZ GUILLEN

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.

char cadena[30]; //We create an array that will store the characters that we will write in the PC console. We assign a limit of characters, in this case 30
byte posicion=0;  //Variable to change the position of the characters in the array
int valor;  //Integer Variable

void setup() {
  lcd.init();  // Initiate the LCD:
  lcd.backlight();  
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LoRa Receiver");
  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    memset(cadena, 0,sizeof(cadena));//memset deletes the contents of the array "cadena" from position 0 to the end sizeof   
    // received a packet
    Serial.print("Received packet... ");    
    // read packet
    while (LoRa.available()) {
      char dedos= (char)LoRa.read();
      Serial.print(dedos);
      cadena[posicion]=dedos;//Read a character from the string "cadena" from "posicion", then read the next character with "posicion++"
      posicion++;
    }
    posicion=0;
    int signal_rx = LoRa.packetRssi();          
    // print RSSI of packet
    Serial.print(" with RSSI ");
    Serial.println(signal_rx);
    int parte1 = getValue(cadena,',',0).toInt();
    int parte2 = getValue(cadena,',',1).toInt();
    int parte3 = getValue(cadena,',',2).toInt();
    Serial.println(parte1);
    delay(100);
    Serial.println(parte2);
    delay(100);
    Serial.println(parte3);
    delay(100);    
    // LCD data
    lcd.clear();
    lcd.setCursor(0, 0); 
    lcd.print("RSSI =");
    lcd.setCursor(7, 0); 
    lcd.print(signal_rx);
    lcd.setCursor(11, 0); 
    lcd.print("dBm");        
    lcd.setCursor(0, 1); 
    lcd.print("Temp =");
    lcd.setCursor(8, 1); 
    lcd.print(parte1);
    lcd.setCursor(11, 1); 
    lcd.print("C");    
    lcd.setCursor(0, 2); 
    lcd.print("Hum =");
    lcd.setCursor(8, 2); 
    lcd.print(parte2);
    lcd.setCursor(11, 2); 
    lcd.print("%");        
    lcd.setCursor(0, 3); 
    lcd.print("CO2 =");
    lcd.setCursor(8, 3); 
    lcd.print(parte3);
    lcd.setCursor(11, 3); 
    lcd.print("PPM");                       
    digitalWrite(LED_BUILTIN, HIGH);
    delay(2000);
    digitalWrite(LED_BUILTIN, LOW); 
  }
}

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;
  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

Analysis:

First you have to receive the data in a character string.

    // read packet
    while (LoRa.available()) {
      char dedos= (char)LoRa.read();
      Serial.print(dedos);
      cadena[posicion]=dedos;//Read a character from the string "cadena" from "posicion", then read the next character with "posicion++"
      posicion++;
    }

How did I separate the data from the three sensors? There are several methods to do it, I did it using the comma (,). The code below shows you this.

String getValue(String data, char separator, int index)
{
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

In the next post we will see a demonstration of how this project works.

Download section:

  • You can get the code from this post below:

modifying_receiver_code_for_multiple_sensors.zip

Anonymous
Element14

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube