sensor code runs on V15 but not V12

Post Reply
strevens
Posts: 5
Joined: Mon Oct 30, 2017 1:17 pm
Company: University of Edinbu

sensor code runs on V15 but not V12

Post by strevens » Mon Oct 30, 2017 5:03 pm

Hi,
I have Libelium smart environment Pro (one is V12 and one is V15)

I have the following code. It appears to operate correctly on the V15 (with the exception that O3 is always zero). When I attempt to run the code on the V12, it always show the measurements as -1.

Could you please advice?

Thanks

Mark

Code: Select all

// Put your libraries here (#include ...)
#include <WaspLoRaWAN.h>
#include <WaspSensorGas_Pro.h>
#include <WaspOPC_N2.h>
#include <WaspFrame.h>

#define debug_flag 0

// Device parameters for Back-End registration
////////////////////////////////////////////////////////////
char DEVICE_EUI[]  = "DEVICE_EUI";
char DEVICE_ADDR[] = "DEVICE_ADDR";
char NWK_SESSION_KEY[] = "NWK_SESSION_KEY";
char APP_SESSION_KEY[] = "APP_SESSION_KEY";
char moteID[] = "moteID";
////////////////////////////////////////////////////////////
uint8_t socket = SOCKET0;
uint8_t error;
// Define port to use in Back-End: from 1 to 223
uint8_t PORT = 3;

// Define time to heat the sensors
char time_to_heat [] = "00:00:05:00"; // time [dd:hh:mm:ss]

// Define objects for sensors: gas_PRO_sensor
Gas CO2_sensor(SOCKET_A);
Gas SO2_sensor(SOCKET_B);
Gas NO2_sensor(SOCKET_C);
Gas O3_sensor(SOCKET_F);

// Sensor variables
float temperature;  // Stores the temperature in ºC
float humidity;   // Stores the realitve humidity in %RH
float pressure;   // Stores the pressure in Pa
float CO2_concentration; // Stores the  CO concentration level in ppm
float SO2_concentration;  // Stores the  SO2 concentration level in ppm
float NO2_concentration;  // Stores the  NO2 concentration level in ppm
float O3_concentration; // Stores the  O3 concentration level in ppm
char t_str[12];
char h_str[12];
char p_str[12];
char co_str[12];
char so_str[12];
char no_str[12];
char o_str[12];

    
// Flag variables
bool send_location_flag = false;
bool acc_flag = false;

// Sleep time variables
uint8_t battery;
int32_t time_to_sleep = 1;
uint32_t TIME_DEFAULT = 60 * 60; // Every 60 min
uint32_t iterTime;
timestamp_t time;

// Other variables

char data_hex[100];
char message[50];

void setup()
{

  USB.ON();
  USB.println(F("LoRaWAN Sending gas measurements"));


  USB.println(F("------------------------------------"));
  USB.println(F("Module configuration"));
  USB.println(F("------------------------------------\n"));


 //////////////////////////////////////////////
  // 1. Switch on
  //////////////////////////////////////////////

  error = LoRaWAN.ON(socket);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("1. Switch ON OK"));     
  }
  else 
  {
    USB.print(F("1. Switch ON error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 2. Set Device EUI
  //////////////////////////////////////////////

  error = LoRaWAN.setDeviceEUI(DEVICE_EUI);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("2. Device EUI set OK"));     
  }
  else 
  {
    USB.print(F("2. Device EUI set error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 3. Set Device Address
  //////////////////////////////////////////////

  error = LoRaWAN.setDeviceAddr(DEVICE_ADDR);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("3. Device address set OK"));     
  }
  else 
  {
    USB.print(F("3. Device address set error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 4. Set Network Session Key
  //////////////////////////////////////////////

  error = LoRaWAN.setNwkSessionKey(NWK_SESSION_KEY);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("4. Network Session Key set OK"));     
  }
  else 
  {
    USB.print(F("4. Network Session Key set error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 5. Set Application Session Key
  //////////////////////////////////////////////

  error = LoRaWAN.setAppSessionKey(APP_SESSION_KEY);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("5. Application Session Key set OK"));     
  }
  else 
  {
    USB.print(F("5. Application Session Key set error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 6. Save configuration
  //////////////////////////////////////////////

  error = LoRaWAN.saveConfig();

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("6. Save configuration OK"));     
  }
  else 
  {
    USB.print(F("6. Save configuration error = ")); 
    USB.println(error, DEC);
  }

  USB.println(F("\n------------------------------------"));
  USB.println(F("Module configured"));
  USB.println(F("------------------------------------\n"));

  LoRaWAN.getDeviceEUI();
  USB.print(F("Device EUI: "));
  USB.println(LoRaWAN._devEUI);  

  LoRaWAN.getDeviceAddr();
  USB.print(F("Device Address: "));
  USB.println(LoRaWAN._devAddr);  

  USB.println();
  
  
  // Configures the I2C isolator
    pinMode(I2C_MAIN_EN, OUTPUT);
    frame.setID(moteID);
// Enable the communication with the board
  digitalWrite(I2C_MAIN_EN, HIGH);

// Power on the temperature, humidity and pressure sensor.
  BME.ON();

// 1.1.2 Power ON gas sensors
  USB.println ("...... Heating gas sensors");
  CO2_sensor.ON();
  SO2_sensor.ON();
  NO2_sensor.ON();
  O3_sensor.ON();
  PWR.deepSleep(time_to_heat, RTC_OFFSET, RTC_ALM1_MODE1, ALL_ON);
  RTC.ON();
  USB.ON();

}




void loop()
{
  // put your main code here, to run repeatedly:
   USB.ON();
    USB.println(F("\n***************************************"));
    USB.println(F("New iteration"));
    USB.println(F("***************************************"));
    // Turn on RTC and get starting time
    RTC.ON();

    // Check battery level
    battery = PWR.getBatteryLevel();
    USB.print(F("Battery level: "));
    USB.println(battery, DEC);

    readBoardSensors();
    
}

void readBoardSensors(void)
{
  frame.setID(moteID);
  // 1.1.1 Reading BME sensor (SOCKET E)
  USB.println ("...... 1.2.1 MEASURING BME sensor (SOCKET E)");

  
  // Read enviromental variables
  temperature = BME.getTemperature(BME280_OVERSAMP_16X, BME280_FILTER_COEFF_OFF);
  humidity = BME.getHumidity(BME280_OVERSAMP_16X);
  pressure = BME.getPressure(BME280_OVERSAMP_16X, BME280_FILTER_COEFF_OFF);

  // 1.1.3 Reading CO sensor (SOCKET A)
  USB.println ("...... 1.2.3 MEASURING CO sensor (SOCKET A)");
  CO2_concentration = CO2_sensor.getConc();

  #if debug_flag == 1
    if(CO2_concentration == -1){
      USB.println(F("...... [DEBUG] CO Sensor not initialized"));
    }else{
      USB.println(F("...... [DEBUG] CO sensor OK"));
    }
  #endif

  // 1.1.4 Reading SO2 sensor (SOCKET B)
  USB.println ("...... 1.2.4 MEASURING SO2 sensor (SOCKET B)");
  SO2_concentration = SO2_sensor.getConc();

  #if debug_flag == 1
    if(SO2_concentration == -1){
      USB.println(F("...... [DEBUG] SO2 Sensor not initialized"));
    }else{
      USB.println(F("...... [DEBUG] SO2 sensor OK"));
    }
  #endif

  // 1.1.5 Reading NO2 sensor (SOCKET C)
  USB.println ("...... 1.2.5 MEASURING NO2 sensor (SOCKET C)");
  NO2_concentration = NO2_sensor.getConc();

  #if debug_flag == 1
    if(NO2_concentration == -1){
      USB.println(F("...... [DEBUG] NO2 Sensor not initialized"));
    }else{
      USB.println(F("...... [DEBUG] NO2 sensor OK"));
    }
  #endif

  // 1.1.6 Reading O3 sensor (SOCKET F)
  USB.println ("...... 1.2.6 MEASURING O3 sensor (SOCKET F)");
  O3_concentration = O3_sensor.getConc();

  #if debug_flag == 1
    if(O3_concentration == -1){
      USB.println(F("...... [DEBUG] O3 Sensor not initialized"));
    }else{
      USB.println(F("...... [DEBUG] O3 sensor OK"));
    }
  #endif

  // Turning the board OFF
  //CO_sensor.OFF();
  //SO2_sensor.OFF();
  //NO2_sensor.OFF();
  //O3_sensor.OFF();


  // 1.1.8 Showing results of the measurement
  USB.println(F("...... 1.2.8 MEASUREMENT RESULTS..."));
  USB.println(F("...... *************************************"));
  USB.print(F("...... Ambient temperature --> "));
  USB.print(temperature);
  USB.println(F(" ºC"));
  USB.print(F("...... Ambient Humidity --> "));
  USB.print(humidity);
  USB.println(F(" %"));
  USB.print(F("...... Ambient pressure --> "));
  USB.print(pressure);
  USB.println(F(" Pa"));
  USB.print(F("...... CO2 concentration: "));
  USB.print(CO2_concentration);
  USB.println(F(" ppm"));
  USB.print(F("...... SO2 concentration: "));
  USB.print(SO2_concentration);
  USB.println(F(" ppm"));
  USB.print(F("...... NO2 concentration: "));
  USB.print(NO2_concentration);
  USB.println(F(" ppm"));
  USB.print(F("...... O3 concentration: "));
  USB.print(O3_concentration);
  USB.println(F(" ppm"));
  USB.println(F("...... *************************************"));

// Send Data

  //////////////////////////////////////////////
  // 1. Creating a new frame
  //////////////////////////////////////////////
  USB.println(F("Creating an ASCII frame"));

    // use dtostrf() to convert from float to string: 
    // '1' refers to minimum width
    // '3' refers to number of decimals (after the point)

dtostrf(temperature, 1, 3, t_str);
dtostrf(humidity, 1, 3, h_str);  
dtostrf(pressure, 1, 3, p_str);
dtostrf(CO2_concentration, 1, 3, co_str);
dtostrf(SO2_concentration, 1, 3, so_str);
dtostrf(NO2_concentration, 1, 3, no_str);
dtostrf(O3_concentration, 1, 3, o_str);

snprintf( message, sizeof(message), "%s,%s,%s,%s,%s,%s,%s", t_str,h_str,p_str,co_str,so_str,no_str,o_str);

Utils.hex2str((uint8_t*)message, data_hex, strlen(message));

USB.print(message);

  //////////////////////////////////////////////
  // 1. Switch on
  //////////////////////////////////////////////

  error = LoRaWAN.ON(socket);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("1. Switch ON OK"));     
  }
  else 
  {
    USB.print(F("1. Switch ON error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 2. Join network
  //////////////////////////////////////////////

  error = LoRaWAN.joinABP();

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("2. Join network OK"));     

    //////////////////////////////////////////////
    // 3. Send confirmed packet 
    //////////////////////////////////////////////
//    data = [temperature, humidity, pressure, CO_concentration, SO2_concentration,NO2_concentration, O3_concentration];

    error = LoRaWAN.sendUnconfirmed( PORT, data_hex);


    // Error messages:
    /*
     * '6' : Module hasn't joined a network
     * '5' : Sending error
     * '4' : Error with data length   
     * '2' : Module didn't response
     * '1' : Module communication error   
     */
    // Check status
    if( error == 0 ) 
    {
      USB.println(F("3. Send unconfirmed packet OK"));  
      if (LoRaWAN._dataReceived == true)
      { 
        USB.print(F("   There's data on port number "));
        USB.print(LoRaWAN._port,DEC);
        USB.print(F(".\r\n   Data: "));
        USB.println(LoRaWAN._data);
      }   
    }
    else 
    {
      USB.print(F("3. Send unconfirmed packet error = ")); 
      USB.println(error, DEC);
    } 
  }
  else 
  {
    USB.print(F("2. Join network error = ")); 
    USB.println(error, DEC);
  }


  //////////////////////////////////////////////
  // 4. Switch off
  //////////////////////////////////////////////

  error = LoRaWAN.OFF(socket);

  // Check status
  if( error == 0 ) 
  {
    USB.println(F("4. Switch OFF OK"));     
  }
  else 
  {
    USB.print(F("4. Switch OFF error = ")); 
    USB.println(error, DEC);
    USB.println();
  }

  
  delay(100000);
}

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: sensor code runs on V15 but not V12

Post by libelium-dev » Tue Oct 31, 2017 9:54 am

Hi,

O3 sensor showing zero could be normal due to low concentration of these gas on the environment. You should test the sensor close close to a diesel exhaust pipe car. We saw your post in another topic, please also try to modify the libraries as we said there.

On the other hand, are you using sensors for v12 in the P&S v12? Keep in mind that the sensors for v15 aren't compatible with v12.

Regards

strevens
Posts: 5
Joined: Mon Oct 30, 2017 1:17 pm
Company: University of Edinbu

Re: sensor code runs on V15 but not V12

Post by strevens » Wed Nov 01, 2017 9:04 am

Ok, Thanks.

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: sensor code runs on V15 but not V12

Post by libelium-dev » Thu Nov 02, 2017 10:19 am

You're welcome :)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest