Combine script for RAK1906: Environment Sensor and RAK1903: Light Sensor

I would like help writing the script for the Rakwireless RAK4631: (Core), RAK1906: (Environment Sensor) and RAK1903: (Light Sensor); which provides sensor readers for Ambient light, Air pressure, Temperature, Humidity, Indoor Air Quality Index, and CO2 equivalent. The data will then be transmitted to my Rak LoRaWan gateway on the TTN every 10 secs.

I’m still very new to Arduino programming and don’t fully understand the C++ syntax yet.

I have tried to merge the two example scripts sketches found within the Arduino IDE, however, i’m getting an error message of:

/private/var/folders/br/rm5jwy_n4pd4n7sg7kmljfjr0000gn/T/.arduinoIDE-unsaved202322-41426-1582inw.mznfe/basic/basic.ino: In function 'void loop()':
/private/var/folders/br/rm5jwy_n4pd4n7sg7kmljfjr0000gn/T/.arduinoIDE-unsaved202322-41426-1582inw.mznfe/basic/basic.ino:168:55: error: invalid use of void expression

exit status 1

Compilation error: invalid use of void expression

My script is as follows:

:pencil2: Please note: I haven’t included the logic for the LoRaWAN transfer because I wanted to achieve this step first, however, if someone can support with that I would be most thankful.

#include <bsec.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h> // Click to install library: http://librarymanager/All#Adafruit_BME680
#include <ClosedCube_OPT3001.h> // Click here to get the library: http://librarymanager/All#OPT3001

// Helper functions declarations
void checkIaqSensorStatus(void);
void errLeds(void);
// Create an object of the class Bsec
Bsec iaqSensor;

String output;


Adafruit_BME680 bme;
// Might need adjustments
#define SEALEVELPRESSURE_HPA (1010.0)

// Forward declarations for functions
void printError(String text, OPT3001_ErrorCode error);
ClosedCube_OPT3001 g_opt3001;
#define OPT3001_ADDRESS 0x44

void configureSensor()
{
  OPT3001_Config newConfig;

  newConfig.RangeNumber = B1100;
  newConfig.ConvertionTime = B0;
  newConfig.Latch = B1;
  newConfig.ModeOfConversionOperation = B11;

  OPT3001_ErrorCode errorConfig = g_opt3001.writeConfig(newConfig);
  if (errorConfig != NO_ERROR)
    printError("OPT3001 configuration", errorConfig);
  else
  {
    OPT3001_Config sensorConfig = g_opt3001.readConfig();
    Serial.println("OPT3001 Current Config:");
    Serial.println("------------------------------");

    Serial.print("Conversion ready (R):");
    Serial.println(sensorConfig.ConversionReady, HEX);

    Serial.print("Conversion time (R/W):");
    Serial.println(sensorConfig.ConvertionTime, HEX);

    Serial.print("Fault count field (R/W):");
    Serial.println(sensorConfig.FaultCount, HEX);

    Serial.print("Flag high field (R-only):");
    Serial.println(sensorConfig.FlagHigh, HEX);

    Serial.print("Flag low field (R-only):");
    Serial.println(sensorConfig.FlagLow, HEX);

    Serial.print("Latch field (R/W):");
    Serial.println(sensorConfig.Latch, HEX);

    Serial.print("Mask exponent field (R/W):");
    Serial.println(sensorConfig.MaskExponent, HEX);

    Serial.print("Mode of conversion operation (R/W):");
    Serial.println(sensorConfig.ModeOfConversionOperation, HEX);

    Serial.print("Polarity field (R/W):");
    Serial.println(sensorConfig.Polarity, HEX);

    Serial.print("Overflow flag (R-only):");
    Serial.println(sensorConfig.OverflowFlag, HEX);

    Serial.print("Range number (R/W):");
    Serial.println(sensorConfig.RangeNumber, HEX);

    Serial.println("------------------------------");
  }
}

void opt3001_lux_level()
{
  OPT3001 result = g_opt3001.readResult();
  uint16_t luxLuminosity = result.lux; 
}

void opt3001_read_data()
{
  OPT3001 result = g_opt3001.readResult();
  uint16_t luminosity = result.lux;
  if (result.error == NO_ERROR)
  {
    uint16_t luminosity = result.lux;
  }
  else
  {
    printError("OPT3001", result.error);
  }
};


void printError(String text, OPT3001_ErrorCode error)
{
  Serial.print(text);
  Serial.print(": [ERROR] Code #");
  Serial.println(error);
}



// Entry point for the example
void setup(void)
{
  Serial.begin(115200);
  while (!Serial) delay(10); // wait for console
  Wire.begin();



  iaqSensor.begin(BME680_I2C_ADDR_SECONDARY, Wire);
  output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
  Serial.println(output);
  checkIaqSensorStatus();


  bsec_virtual_sensor_t sensorList[10] = {
    BSEC_OUTPUT_RAW_TEMPERATURE,
    BSEC_OUTPUT_RAW_PRESSURE,
    BSEC_OUTPUT_RAW_HUMIDITY,
    BSEC_OUTPUT_RAW_GAS,
    BSEC_OUTPUT_IAQ,
    BSEC_OUTPUT_STATIC_IAQ,
    BSEC_OUTPUT_CO2_EQUIVALENT,
    BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
  };
/* opt3001 init */
	g_opt3001.begin(OPT3001_ADDRESS);
	Serial.print("OPT3001 Manufacturer ID");
	Serial.println(g_opt3001.readManufacturerID());
	Serial.print("OPT3001 Device ID");
	Serial.println(g_opt3001.readDeviceID());
	configureSensor();

  iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
  checkIaqSensorStatus();
}


// Function that is looped forever
void loop(void)
{
  unsigned long time_trigger = millis();
  if (iaqSensor.run()) { // If new data is available
    output = String(time_trigger);
    output += ", R.Temp: " + String(iaqSensor.rawTemperature) + "°C" ;
    output += ", R.Prs: " + String(iaqSensor.pressure) + "hPa";
    output += ", R.Hum: " + String(iaqSensor.rawHumidity) + "%";
    output += ", Gas: " + String(iaqSensor.gasResistance) + "Ohms";
    output += ", IAQ: " + String(iaqSensor.iaq) + "idx";
    output += ", IAQ (acc): " + String(iaqSensor.iaqAccuracy) + "+-";
    output += ", Temp: " + String(iaqSensor.temperature) + "°C";
    output += ", Prs: " + String(iaqSensor.humidity) + "%";
    output += ", Static IAQ: " + String(iaqSensor.staticIaq) + "idx";
    output += ", CO2: " + String(iaqSensor.co2Equivalent) + "lvl";
    output += ", VOC: " + String(iaqSensor.breathVocEquivalent) + "lvl";
    g_opt3001.readResult()
    output += ", Light: " + String(opt3001_read_data()) + "lux";
  
    Serial.println(output);
  } else {
    checkIaqSensorStatus();
  }
}

// Helper function definitions
void checkIaqSensorStatus(void)
{
  if (iaqSensor.status != BSEC_OK) {
    if (iaqSensor.status < BSEC_OK) {
      output = "BSEC error code : " + String(iaqSensor.status);
      Serial.println(output);
      for (;;)
        errLeds(); /* Halt in case of failure */
    } else {
      output = "BSEC warning code : " + String(iaqSensor.status);
      Serial.println(output);
    }
  }

  if (iaqSensor.bme680Status != BME680_OK) {
    if (iaqSensor.bme680Status < BME680_OK) {
      output = "BME680 error code : " + String(iaqSensor.bme680Status);
      Serial.println(output);
      for (;;)
        errLeds(); /* Halt in case of failure */
    } else {
      output = "BME680 warning code : " + String(iaqSensor.bme680Status);
      Serial.println(output);
    }
  }
}

void errLeds(void)
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);
  delay(100);
}

Look at your function void opt3001_read_data(). There is a stray “;” at the end.
image

And you are missing a “;” in line 161 after g_opt3001.readResult()

And this line output += ", Light: " + String(opt3001_read_data()) + "lux"; makes no sense at all.

Better check your code for correct syntax.

Thank you Bernd (@beegee).

What I was trying to achieve was add a simple function that reads the ambient light sensor data and appends it to the end of the output variable. However, I’m totally new to C++. I can’t seem to get the Syntex right to create a function that calls the light sensor reading, then ensure void loop(void) calls my function and returns the reading, appending it to output.

I would appreciate support in achieving this.