-->
Page 1 of 1

help ccs811 posting values to mqtt

PostPosted: Wed Jan 17, 2018 11:37 pm
by papas_17
hi i tried to make a code to send the values of the ccs811/bme280 sensor from sparkfun but i am always getting an error when trying to compile it can you please check my code and see what i have done wrong ?
thanks i advance for every help
Code: Select all/******************************************************************************
  BME280Compensated.ino

  Marshall Taylor @ SparkFun Electronics

  April 4, 2017

  https://github.com/sparkfun/CCS811_Air_Quality_Breakout
  https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library

  This example uses a BME280 to gather environmental data that is then used
  to compensate the CCS811.

  Hardware Connections (Breakoutboard to Arduino):
  3.3V to 3.3V pin
  GND to GND pin
  SDA to A4
  SCL to A5

  Resources:
  Uses Wire.h for i2c operation

  Development environment specifics:
  Arduino IDE 1.8.1

  This code is released under the [MIT License](http://opensource.org/licenses/MIT).

  Please review the LICENSE.md file included with this example. If you have any questions
  or concerns with licensing, please contact techsupport@sparkfun.com.

  Distributed as-is; no warranty is given.
******************************************************************************/
#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define CCS811_ADDR 0x5B //Default I2C Address
//#define CCS811_ADDR 0x5A //Alternate I2C Address

#define PIN_NOT_WAKE 5

//Global sensor objects
CCS811 myCCS811(CCS811_ADDR);
BME280 myBME280;

// Connect to the WiFi
const char* ssid = "*********";
const char* password = "*******!";
const char* mqtt_server = "192.168.1.177";

WiFiClient espClient;
PubSubClient client(espClient);

boolean reconnect() {  // **********************************************************

  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print (F("Contacting MQTT server..."));
    // Attempt to connect
    if (client.connect("E_C_G","username", "password")) {     //assign a "client name".  Each wemos must have a unique name
      Serial.println (F("connected"));

      // ... SUBSCRIBE TO TOPICS
      //      client.subscribe("Upstairs/Mbr/BME280/TempF");
      //      client.subscribe("Upstairs/Mbr/BME280/TempC");
      //      client.subscribe("Upstairs/Mbr/BME280/Humidity");
      //      client.subscribe("Upstairs/Mbr/BME280/Pressure");

      return client.connected();

      Serial.print (F("Failed to connect. "));
      Serial.println (F(" Attempting connection again in 3 seconds"));
      // Wait 3 seconds before retrying
      //      delay(3000);
      return 0;
    }
  }
}

void setup(){
{
  {
    Serial.begin(9600);
    client.setServer(mqtt_server, 1883);
  }
    // Connect to WiFinetwork
  Serial.println();
  Serial.println();
  Serial.print (F("Connecting to "));
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.begin(9600);
    Serial.print (F("."));
  }
  Serial.println (F(""));
  Serial.println (F("WiFi connected"));
  // Print the IP address
  Serial.print (F("Local IP: "));
  Serial.println(WiFi.localIP());
}
    //  *********************************************************************************

 
{  Serial.println();
  Serial.println("Apply BME280 data to CCS811 for compensation.");

  //This begins the CCS811 sensor and prints error status of .begin()
  CCS811Core::status returnCode = myCCS811.begin();
  Serial.print("CCS811 begin exited with: ");
  //Pass the error code to a function to print the results
  printDriverError( returnCode );
  Serial.println();

  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x77;

  //Initialize BME280
  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x77;
  myBME280.settings.runMode = 3; //Normal mode
  myBME280.settings.tStandby = 0;
  myBME280.settings.filter = 4;
  myBME280.settings.tempOverSample = 5;
  myBME280.settings.pressOverSample = 5;
  myBME280.settings.humidOverSample = 5;

  //Calling .begin() causes the settings to be loaded
  delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
  myBME280.begin();


 }
}
//---------------------------------------------------------------
void loop(){
  if (!client.connected()) {
    reconnect();
  }

{
  //Check to see if data is available
  if (myCCS811.dataAvailable())
  {
    //Calling this function updates the global tVOC and eCO2 variables
    myCCS811.readAlgorithmResults();
    //printInfoSerial fetches the values of tVOC and eCO2
    printInfoSerial();

    float BMEtempC = myBME280.readTempC();
    float BMEhumid = myBME280.readFloatHumidity();

    Serial.print("Applying new values (deg C, %): ");
    Serial.print(BMEtempC);
    Serial.print(",");
    Serial.println(BMEhumid);
    Serial.println();

    //This sends the temperature data to the CCS811
    myCCS811.setEnvironmentalData(BMEhumid, BMEtempC);
  }
  else if (myCCS811.checkForStatusError())
  {
    //If the CCS811 found an internal error, print it.
    printSensorError();
  }

  delay(2000); //Wait for next reading
}

//---------------------------------------------------------------
void printInfoSerial()
{
  //getCO2() gets the previously read data from the library
 
  Serial.println("CCS811 data:");
  Serial.print(" CO2 concentration : ");
  Serial.print(myCCS811.getCO2());
  Serial.println(" ppm");
  client.publish("enviroment/sensor/E_C_G/CO2", (myCCS811.getCO2()))

  //getTVOC() gets the previously read data from the library
  Serial.print(" TVOC concentration : ");
  Serial.print(myCCS811.getTVOC());
  Serial.println(" ppb");
  client.publish("enviroment/sensor/E_C_G/TVOC_ppb", (myCCS811.getTVOC()));

  Serial.println("BME280 data:");
  Serial.print(" Temperature: ");
  Serial.print(myBME280.readTempC(), 2);
  Serial.println(" degrees C");
  client.publish("enviroment/sensor/E_C_G/TempC",(myBME280.readTempC(), 2));

  Serial.print(" Temperature: ");
  Serial.print(myBME280.readTempF(), 2);
  Serial.println(" degrees F");
  client.publish("enviroment/sensor/E_C_G/TempF", (buffermyBME280.readTempF(), 2));
 
  Serial.print(" Pressure: ");
  Serial.print(myBME280.readFloatPressure(), 2);
  Serial.println(" Pa");
  client.publish("enviroment/sensor/E_C_G/Pressure_Pa", (myBME280.readFloatPressure(), 2));

  Serial.print(" Pressure: ");
  Serial.print((myBME280.readFloatPressure() * 0.0002953), 2);
  Serial.println(" InHg");
  client.publish("enviroment/sensor/E_C_G/Pressure_InHg",(myBME280.readFloatPressure() * 0.0002953), 2));
 
  Serial.print(" Altitude: ");
  Serial.print(myBME280.readFloatAltitudeMeters(), 2);
  Serial.println("m");
  client.publish("enviroment/sensor/E_C_G/AltitudeMeters",(myBME280.readFloatAltitudeMeters(), 2));
 
  Serial.print(" Altitude: ");
  Serial.print(myBME280.readFloatAltitudeFeet(), 2);
  Serial.println("ft");
 
  Serial.print(" %RH: ");
  Serial.print(myBME280.readFloatHumidity(), 2);
  Serial.println(" %");
  client.publish("enviroment/sensor/E_C_G/Humidity",(myBME280.readFloatHumidity(), 2));
 
  Serial.println();


}

//printDriverError decodes the CCS811Core::status type and prints the
//type of error to the serial terminal.
//
//Save the return value of any function of type CCS811Core::status, then pass
//to this function to see what the output was.
void printDriverError( CCS811Core::status errorCode )
{
  switch ( errorCode )
  {
    case CCS811Core::SENSOR_SUCCESS:
      Serial.print("SUCCESS");
      break;
    case CCS811Core::SENSOR_ID_ERROR:
      Serial.print("ID_ERROR");
      break;
    case CCS811Core::SENSOR_I2C_ERROR:
      Serial.print("I2C_ERROR");
      break;
    case CCS811Core::SENSOR_INTERNAL_ERROR:
      Serial.print("INTERNAL_ERROR");
      break;
    case CCS811Core::SENSOR_GENERIC_ERROR:
      Serial.print("GENERIC_ERROR");
      break;
    default:
      Serial.print("Unspecified error.");
  }
}

//printSensorError gets, clears, then prints the errors
//saved within the error register.
void printSensorError()
{
  uint8_t error = myCCS811.getErrorRegister();

  if ( error == 0xFF ) //comm error
  {
    Serial.println("Failed to get ERROR_ID register.");
  }
  else
  {
    Serial.print("Error: ");
    if (error & 1 << 5) Serial.print("HeaterSupply");
    if (error & 1 << 4) Serial.print("HeaterFault");
    if (error & 1 << 3) Serial.print("MaxResistance");
    if (error & 1 << 2) Serial.print("MeasModeInvalid");
    if (error & 1 << 1) Serial.print("ReadRegInvalid");
    if (error & 1 << 0) Serial.print("MsgInvalid");
    Serial.println();
  }
}

Re: help ccs811 posting values to mqtt

PostPosted: Fri Jan 19, 2018 9:08 pm
by papas_17
ok so here is the fixed code i managed to have everything working exept i cant get the actual values published cause i get this error so i had to put "" to all stop the errors but this is not what i want
can you please help me with this

Code: Select allIn function 'void printInfoSerial()':

Enviroment_Gym_Control:184: error: invalid conversion from 'uint16_t {aka short unsigned int}' to 'const char*' [-fpermissive]

   client.publish("enviroment/sensor/group1/CO2", (myCCS811.getCO2()));


C:\Users\Chris\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: error:   initializing argument 2 of 'boolean PubSubClient::publish(const char*, const char*)' [-fpermissive]

    boolean publish(const char* topic, const char* payload);

            ^

exit status 1
invalid conversion from 'uint16_t {aka short unsigned int}' to 'const char*' [-fpermissive]


Code: Select all******************************************************************************/
#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define CCS811_ADDR 0x5B //Default I2C Address
//#define CCS811_ADDR 0x5A //Alternate I2C Address

#define PIN_NOT_WAKE 5

//Global sensor objects
CCS811 myCCS811(CCS811_ADDR);
BME280 myBME280;

// Connect to the WiFi
const char* ssid = "@@@@@";
const char* password = "@@@@@";
const char* mqtt_server = "192.168.1.177";

WiFiClient espClient;
PubSubClient client(espClient);

boolean reconnect() {// Loop until we're reconnected
  while (!client.connected()) {
    Serial.print (F("Contacting MQTT server..."));
    // Attempt to connect
    if (client.connect("group1","@@@@","@@@@@")) {     //assign a "client name".  Each wemos must have a unique name
      Serial.println (F("connected"));

      // ... SUBSCRIBE TO TOPICS
      //      client.subscribe("Upstairs/Mbr/BME280/TempF");
      //      client.subscribe("Upstairs/Mbr/BME280/TempC");
      //      client.subscribe("Upstairs/Mbr/BME280/Humidity");
      //      client.subscribe("Upstairs/Mbr/BME280/Pressure");

      return client.connected();

      Serial.print (F("Failed to connect. "));
      Serial.println (F(" Attempting connection again in 3 seconds"));
      // Wait 3 seconds before retrying
      //      delay(3000);
      return 0;
    }
  }
}

void setup(){
{
  {
    Serial.begin(9600);
    client.setServer(mqtt_server, 1883);
  }
    // Connect to WiFinetwork
  Serial.println();
  Serial.println();
  Serial.print (F("Connecting to "));
  Serial.println(ssid);

  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //Serial.begin(9600);
    Serial.print (F("."));
  }
  Serial.println (F(""));
  Serial.println (F("WiFi connected"));
  // Print the IP address
  Serial.print (F("Local IP: "));
  Serial.println(WiFi.localIP());
}
    //  *********************************************************************************

 
{  Serial.println();
  Serial.println("Apply BME280 data to CCS811 for compensation.");

  //This begins the CCS811 sensor and prints error status of .begin()
  CCS811Core::status returnCode = myCCS811.begin();
  Serial.print("CCS811 begin exited with: ");
  //Pass the error code to a function to print the results
  printDriverError( returnCode );
  Serial.println();

  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x77;

  //Initialize BME280
  //For I2C, enable the following and disable the SPI section
  myBME280.settings.commInterface = I2C_MODE;
  myBME280.settings.I2CAddress = 0x77;
  myBME280.settings.runMode = 3; //Normal mode
  myBME280.settings.tStandby = 0;
  myBME280.settings.filter = 4;
  myBME280.settings.tempOverSample = 5;
  myBME280.settings.pressOverSample = 5;
  myBME280.settings.humidOverSample = 5;

  //Calling .begin() causes the settings to be loaded
  delay(10);  //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up.
  myBME280.begin();


 }
}
//---------------------------------------------------------------
void loop(){
  if (!client.connected()) {
    reconnect();
  }

//{
  //Check to see if data is available
  if (myCCS811.dataAvailable())
  {
    //Calling this function updates the global tVOC and eCO2 variables
    myCCS811.readAlgorithmResults();
    //printInfoSerial fetches the values of tVOC and eCO2
    printInfoSerial();

    float BMEtempC = myBME280.readTempC();
    float BMEhumid = myBME280.readFloatHumidity();

    Serial.print("Applying new values (deg C, %): ");
    Serial.print(BMEtempC);
    Serial.print(",");
    Serial.println(BMEhumid);
    Serial.println();

    //This sends the temperature data to the CCS811
    myCCS811.setEnvironmentalData(BMEhumid, BMEtempC);
  }
  else if (myCCS811.checkForStatusError())
  {
    //If the CCS811 found an internal error, print it.
    printSensorError();
  }

  delay(30000); //Wait for next reading
}

//---------------------------------------------------------------
void printInfoSerial()
{
  //getCO2() gets the previously read data from the library
 
  Serial.println("CCS811 data:");
  Serial.print(" CO2 concentration : ");
  Serial.print(myCCS811.getCO2());
  Serial.println(" ppm");
  client.publish("enviroment/sensor/group1/CO2", (myCCS811.getCO2()));

  //getTVOC() gets the previously read data from the library
  Serial.print(" TVOC concentration : ");
  Serial.print(myCCS811.getTVOC());
  Serial.println(" ppb");
  client.publish("enviroment/sensor/group1/TVOC_ppb", "o");

  Serial.println("BME280 data:");
  Serial.print(" Temperature: ");
  Serial.print(myBME280.readTempC(), 2);
  Serial.println(" degrees C");
  client.publish("enviroment/sensor/group1/TempC","(myBME280.readTempC(), 2)");

  Serial.print(" Temperature: ");
  Serial.print(myBME280.readTempF(), 2);
  Serial.println(" degrees F");
  client.publish("enviroment/sensor/group1/TempF", "myBME280.readTempF(), 2");
 
  Serial.print(" Pressure: ");
  Serial.print(myBME280.readFloatPressure(), 2);
  Serial.println(" Pa");
  client.publish("enviroment/sensor/BME280/Pressure_Pa", "(myBME280.readFloatPressure(), 2)");
  Serial.print(" Pressure: ");
  Serial.print((myBME280.readFloatPressure() * 0.0002953), 2);
  Serial.println(" InHg");
  client.publish("enviroment/sensor/group1/Pressure_InHg","(myBME280.readFloatPressure() * 0.0002953), 2)");
 
  Serial.print(" Altitude: ");
  Serial.print(myBME280.readFloatAltitudeMeters(), 2);
  Serial.println("m");
  client.publish("enviroment/sensor/group1/AltitudeMeters","(myBME280.readFloatAltitudeMeters(), 2)");
 
  Serial.print(" Altitude: ");
  Serial.print(myBME280.readFloatAltitudeFeet(), 2);
  Serial.println("ft");
 
  Serial.print(" %RH: ");
  Serial.print(myBME280.readFloatHumidity(), 2);
  Serial.println(" %");
  client.publish("enviroment/sensor/group1/Humidity","0000");
 
  Serial.println();


}

//printDriverError decodes the CCS811Core::status type and prints the
//type of error to the serial terminal.
//
//Save the return value of any function of type CCS811Core::status, then pass
//to this function to see what the output was.
void printDriverError( CCS811Core::status errorCode )
{
  switch ( errorCode )
  {
    case CCS811Core::SENSOR_SUCCESS:
      Serial.print("SUCCESS");
      break;
    case CCS811Core::SENSOR_ID_ERROR:
      Serial.print("ID_ERROR");
      break;
    case CCS811Core::SENSOR_I2C_ERROR:
      Serial.print("I2C_ERROR");
      break;
    case CCS811Core::SENSOR_INTERNAL_ERROR:
      Serial.print("INTERNAL_ERROR");
      break;
    case CCS811Core::SENSOR_GENERIC_ERROR:
      Serial.print("GENERIC_ERROR");
      break;
    default:
      Serial.print("Unspecified error.");
  }
}

//printSensorError gets, clears, then prints the errors
//saved within the error register.
void printSensorError()
{
  uint8_t error = myCCS811.getErrorRegister();

  if ( error == 0xFF ) //comm error
  {
    Serial.println("Failed to get ERROR_ID register.");
  }
  else
  {
    Serial.print("Error: ");
    if (error & 1 << 5) Serial.print("HeaterSupply");
    if (error & 1 << 4) Serial.print("HeaterFault");
    if (error & 1 << 3) Serial.print("MaxResistance");
    if (error & 1 << 2) Serial.print("MeasModeInvalid");
    if (error & 1 << 1) Serial.print("ReadRegInvalid");
    if (error & 1 << 0) Serial.print("MsgInvalid");
    Serial.println();
  }
}