[SOLVED] LoRa geophone issues setting sensitivity of sensor

I am making a seismic sensor using a geophone and two LoRa modules. When the geophone reads vibration it sends the data from the sensor (geophone reading, GPS location and altitude, battery voltage and percentage, and time). I need both LoRa modules to send and receive data between the two. The sensor is functioning properly and I am trying to add a feature that allows the other LoRa module to control the level of sensitivity by entering a certain phrase in the serial monitor. When I added this feature,the sensor ignores my if statements on which level of sensitivity it should have. I know the fix is probably simple but it's been kicking my butt all day. Any help would be much appreciated.

Sensor Sketch

//Include the needed libraries for the ADS and the GPS module
#include <Wire.h>//I2C library
#include <Adafruit_ADS1X15.h>//ADS library
#include <heltec.h> //Heltec ESP32 LoRa board that is being used as the board and to transmit data
#include "TinyGPS++.h"//Gps module library

//Define the BAND frequency
#define BAND    915E6//set BAND to US which is 915E6 or 915MHz

//Declare your objects which are the GPS module and the ADS1115 in which we can read the geophone inputs
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
Adafruit_ADS1115 ads;/* Use this for the 16-bit version */

//Declare the global variables
int value = 0;
float Voltage;
float Perc;
int16_t SensorRead;
float LatRead;
float LonRead;
float AltRead;
int HourRead;
int MinRead;
int SecRead;
int LEDPin = 12;
String rssi = "RSSI --";
String packSize = "--";
String packet ;
int id = 1;
String deviceType = "GRDSEN1";
String quote = "";
String command;
int readingMax;
int readingMin;
void setup(void)
{
  Serial.begin(115200);       // use the serial port
  Serial2.begin(115200, SERIAL_8N1, 2, 17);
  //serial_connection.begin(115200);//This opens up communications to the GPS
  Serial.println("Hello!");
  Serial.println("Single-ended readings from AIN0 with >3.0V comparator");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  Serial.println("Comparator Threshold: 1000 (3.000V)");
  //                                                                ADS1015  ADS1115
  //                                                                -------  -------
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  //ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  //ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  ads.begin();
  ads.setGain(GAIN_FOUR);
  if (!ads.begin()) {
    Serial.println("Failed to initialize ADS.");
    while (1);
  }
  // Setup 3V comparator on channel 0
  ads.startComparator_SingleEnded(0, 1000);
  pinMode(LEDPin, OUTPUT);
  //WIFI Kit series V1 not support Vext control
  Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
}

void loop(void)
{

  value = analogRead(A0);
  Voltage = value * 5.0 / 1023;
  Perc = map(Voltage, 3.6, 4.2, 0, 100);
  LatRead = gps.location.lat();
  LonRead = gps.location.lng();
  AltRead = gps.altitude.feet();
  HourRead = gps.time.hour();
  MinRead = gps.time.minute();
  SecRead = gps.time.second();
  command = LoRa.readStringUntil('\n');
  SensorRead = ads.getLastConversionResults();
  String daTa = String(id) + "," + String(HourRead) + "," + String(MinRead) + "," + String(SecRead) + "," + String(LatRead, 6) + "," + String(LonRead, 6) + "," + String(AltRead) + "," + String(SensorRead) + "," + String(Voltage) + "," + String(Perc);
  sendsensitivity();
  Serial.print("GEOPHONE: "); Serial.println(SensorRead);
  if (SensorRead > readingMax or SensorRead < readingMin )
  {
    flash();
    LoRa.beginPacket();
    LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
    LoRa.print(daTa);
    LoRa.endPacket();
    Serial.print(daTa);
  }
}

void sendsensitivity()
{
  while (LoRa.available()) {
    command.trim();
    if (command.equals("level:1")) {
      readingMax = 150;
      readingMin = -150;
    }

    else if (command.equals("level:2")) {
      readingMax = 180;
      readingMin = -180;
    }
    else if (command.equals("level:3")) {
      readingMax = 200;
      readingMin = -200;
    }
    else if (command.equals("level:4")) {
      readingMax = 500;
      readingMin = -500;
    }
    else if (command.equals("level:5")) {
      readingMax = 800;
      readingMin = -800;
    }
    else {
      readingMax = 200;
      readingMin = -200;
    }
  }
}

void flash()
{
  digitalWrite(LEDPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LEDPin, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
}

Hud Sketch

#include "heltec.h"

#define BAND    915E6  //you can set band here directly,e.g. 868E6,915E6

String command;
String rc = "";
void setup() {
  //WIFI Kit series V1 not support Vext control
  Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  Serial.begin(115200);

}

void loop() {
  // try to parse packet

  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.println("Received packet '");
    // read packet
    while (LoRa.available()) {
      rc = LoRa.readString();

    }
  }
  sendsensitivity();
  showData();
}

void sendsensitivity()
{
  while (Serial.available()) {
    command = Serial.readStringUntil('\n');
    command.trim();
    if (command.equals("level:1")) {
      LoRa.beginPacket();
      LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
      LoRa.print("level:1");
      LoRa.endPacket();
    }

    else if (command.equals("level:2")) {
      LoRa.beginPacket();
      LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
      LoRa.print("level:2");
      LoRa.endPacket();
    }
    else if (command.equals("level:3")) {
      LoRa.beginPacket();
      LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
      LoRa.print("level:3");
      LoRa.endPacket();
    }
    else if (command.equals("level:4")) {
      LoRa.beginPacket();
      LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
      LoRa.print("level:4");
      LoRa.endPacket();
    }
    else if (command.equals("level:5")) {
      LoRa.beginPacket();
      LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
      LoRa.print("level:5");
      LoRa.endPacket();
    }
    else {
      Serial.println("bad command");
    }
    Serial.print("Command: ");
    Serial.println(command);
  }
}

void showData() {
  Serial.print("Data: "); Serial.println(rc);

}

Not enough information provided to make a guess.

For informed help, please read and follow the directions in the "getting started" forum post: How to get the best out of this forum

Please explain in detail what the program should do, and what it does instead.

@jremington
Sensor Sketch: HARDWARE- (Heltec LoRa ESP32 V2, Adafruit ADS1115, Sparkfun Geophone SM-24, GPS Module GPS NEO-6M, Red LED light)
purpose: To get; latitude/longitude/altitude/time readings from GPS module.
To get; geophone reading (labeled as sensorRead) from geophone connected to ADS1115.
To set the device type with the String "deviceType" and set the device ID with the String "id".
To set the sensitivity (in "sendsensitivity()" ) of the "sensorRead" by creating an ADS115 reading maximum number given by "int readingMax" and reading minimum number given by "int readingMin".
To determine the sensitivity (in "sendsensitivity()" ) of the "sensorRead" by reading received LoRa packet from the "Hud Sketch" labeled as "level:X".
To combine all retrieved data as String "daTa".
To send data from the Sensor Sketch to the Hud Sketch when "sensorRead" is greater than or less than "readingMax" or "readingMin".
To flash red LED when "sensorRead" is greater than or less than "readingMax" or "readingMin".

Funtion: I expected when the Heltec LoRa ESP32 receives power, for the micro-controller to initialize the GPS module, LED pinMode, ADS1115 (this is what the geophone is connected to) and the LoRa feature built into the micro-controller. The setup should be grabbing data from the GPS module and the Geophone. When the surface the Geophone is on top of receives enough vibration (in which the threshold is set by the commands sent by the other Heltec LoRa ESP32( programmed with the Hud Sketch) ) The micro-controller programmed with the Sensor Sketch should blink the LED to give me a real world visual confirmation that the project is working. The program should show the data it has in that moment (device type, id, latitude, longitude, altitude, time, sensor reading, battery voltage, and battery percentage) as well as send all that data in the form of a String to the Hud (the other Heltec LoRa ESP32).

Results: The serial monitor displays the data it has at the current time but instead of sending that data as a LoRa packet and flashing the LED only when the Geophone receives stimuli withing the set threshold, The LED flashes continuously without vibration, and the LoRa features does not send any data to the Hud. I have had success with this same project when the sensitivity setting is omitted and I have the readingMax as a constant of 200 and the readingMin as a constant of -200. In this version I created the Sensor Sketch only sends data to the Hud and cannot/doesn't receive any LoRa packets from the Hud. I do require the Sensor Sketch to be able to receive commands from the Hud and send data using LoRa. Is this possible? I hope this is detailed enough.

@jremington
Hud Sketch: HARDWARE- (Heltec LoRa ESP32 V2)
purpose: To get data from the "Sensor Sketch" and store that data in the String "rc" utilizing LoRa.readString
*To allow user to type and enter specific commands to set sensitivity of "Sensor Sketch" micro-controller. *
To send sensitivity levels by utilizing "if" and "else if" statements that send specific LoRa packets named "level:X".
To display recieved data from the "Sensor Sketch" micro-controller.

Function: I expected when the Heltec LoRa ESP32 receives power, for the micro-controller to initialize the LoRa feature built into the micro-controller. The setup should be receiving and displaying data from the other Heltec LoRa ESP32 micro-controller programmed with the "Sensor Sketch". The Hud micro-controller should allow for user to enter "level:X" in the serial monitor command line, in which X represents 1-5. In doing so the user is setting their preferred sensitivity of the "Sensor Sketch" micro-controller. This should be observable in the Sensor Sketch micro-controller as it should require more or less vibrations to trip the Sensor Sketch micro-controller and receive data in the Hud.

Results: The serial monitor displays the command the user inputs, but no data is sent from the "Sensor Sketch" micro-controller. The "Sensor Sketch" micro-controller blinks its LED continuously with no vibrations present. I tested the Hud Sketch in conjunction with another Heltec LoRa ESP32 loaded with the basic example LoRa Receiver program to see if the "Hud Sketch" was even sending the commands the user was imputing. The results of that test showed that the "Hud Sketch" is sending fine and the micro-controller loaded with the example LoRa Receiver sketch showed exactly what the user input in the "Hud Sketch".

Example
User input: level:4
serial monitor output from example sketch: level:4

LoRa Receiver Example below:

#include "heltec.h"

#define BAND    915E6  //you can set band here directly,e.g. 868E6,915E6
void setup() {
    //WIFI Kit series V1 not support Vext control
  Heltec.begin(true /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
  Serial.begin(115200)
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

It sounds like more than one problem exists, and with a project this complicated, your best bet is to break the project into segments, get each segment working separately, and make sure it stays working, when you add the second segment. And so forth.

That said, it may be a bit too late to start over, so consider one problem at a time.

For example, this should be straightforward to debug:

The serial monitor displays the command the user inputs, but no data is sent from the "Sensor Sketch" micro-controller.

You tried to debug that with separate code, which works, but that sheds no light at all on the reason for the failure.

There are many possible reasons for this failure with the existing code, and you need to work through them one at a time. Put in Serial.print() to verify each step of the pathway.

  1. Is the command accepted? Print it out. Check the line ending setting on the serial monitor.
  2. Is the command sent to the radio? Print the packet sent, and the transmit success flag.
  3. Is the command received at the other end? Print the received packet.
  4. Is the command carried out at the receiving end? Print something showing that the individual steps in the process are successfully executed.

I found the issue. I changed the global variables of readingMax and readingMin to be the default and added an if statement to change those global variables when the sensor received a command only. After isolating the sensitivity feature in a separate sketch I found that while the commands were being sent they were not updating the variables to be used by the sensorRead if statement.

//Include the needed libraries for the ADS and the GPS module
#include <Wire.h>//I2C library
#include <Adafruit_ADS1X15.h>//ADS library
#include <heltec.h> //Heltec ESP32 LoRa board that is being used as the board and to transmit data
#include "TinyGPS++.h"//Gps module library

//Define the BAND frequency
#define BAND    915E6//set BAND to US which is 915E6 or 915MHz

//Declare your objects which are the GPS module and the ADS1115 in which we can read the geophone inputs
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
Adafruit_ADS1115 ads;/* Use this for the 16-bit version */

//Declare the global variables
int value = 0;
float Voltage;
float Perc;
int16_t SensorRead;
float LatRead;
float LonRead;
float AltRead;
int HourRead;
int MinRead;
int SecRead;
int LEDPin = 12;
String rssi = "RSSI --";
String packSize = "--";
String packet ;
int id = 1;
String deviceType = "GRDSEN1";
String quote = "";
String command;
int readingMax = 200;
int readingMin = -200;
void setup(void)
{
  Serial.begin(115200);       // use the serial port
  Serial2.begin(115200, SERIAL_8N1, 2, 17);
  //serial_connection.begin(115200);//This opens up communications to the GPS
  Serial.println("Hello!");
  Serial.println("Single-ended readings from AIN0 with >3.0V comparator");
  Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  Serial.println("Comparator Threshold: 1000 (3.000V)");
  //                                                                ADS1015  ADS1115
  //                                                                -------  -------
  // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  //ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  //ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  ads.begin();
  ads.setGain(GAIN_FOUR);
  if (!ads.begin()) {
    Serial.println("Failed to initialize ADS.");
    while (1);
  }
  // Setup 3V comparator on channel 0
  ads.startComparator_SingleEnded(0, 1000);
  pinMode(LEDPin, OUTPUT);
  //WIFI Kit series V1 not support Vext control
  Heltec.begin(false /*DisplayEnable Enable*/, true /*Heltec.LoRa Disable*/, true /*Serial Enable*/, true /*PABOOST Enable*/, BAND /*long BAND*/);
}

void loop(void)
{

  value = analogRead(A0);
  Voltage = value * 5.0 / 1023;
  Perc = map(Voltage, 3.6, 4.2, 0, 100);
  LatRead = gps.location.lat();
  LonRead = gps.location.lng();
  AltRead = gps.altitude.feet();
  HourRead = gps.time.hour();
  MinRead = gps.time.minute();
  SecRead = gps.time.second();
  SensorRead = ads.getLastConversionResults();
  String daTa = String(id) + "," + String(HourRead) + "," + String(MinRead) + "," + String(SecRead) + "," + String(LatRead, 6) + "," + String(LonRead, 6) + "," + String(AltRead) + "," + String(SensorRead) + "," + String(Voltage) + "," + String(Perc);
  Serial.print("GEOPHONE: "); Serial.println(SensorRead);
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    command = LoRa.readStringUntil('\n');

    if (command.equals("level:1")) {
      readingMax = 150;
      readingMin = -150;
    }

    else if (command.equals("level:2")) {
      readingMax = 180;
      readingMin = -180;
    }
    else if (command.equals("level:3")) {
      readingMax = 200;
      readingMin = -200;
    }
    else if (command.equals("level:4")) {
      readingMax = 500;
      readingMin = -500;
    }
    else if (command.equals("level:5")) {
      readingMax = 800;
      readingMin = -800;
    }
  }

  if (SensorRead > readingMax or SensorRead < readingMin )
  {
    flash();
    LoRa.beginPacket();
    LoRa.setTxPower(20, RF_PACONFIG_PASELECT_PABOOST);
    LoRa.print(daTa);
    LoRa.endPacket();
    Serial.print(daTa);
  }
}

void flash()
{
  digitalWrite(LEDPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LEDPin, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.