Hello all,
I have a example skets that i am trying to modify so that the data is not only sent to the serial monitor, but to a MQTT topic as well. I now have it connecting to wifi then to the mosquitto correctly. I also the the massage "waiting for (new) data". But i have no idea how to send the CO2 data to the mqtt topic.
I have tryed client.publish("CCS811/CO2", (eco2) );
But that sends the text (eco2
How do i send the CO2 number? Below the modifyed example i have:
/*
ccs811basic.ino - Demo sketch printing results of the CCS811 digital gas sensor for monitoring indoor air quality from ams.
Created by Maarten Pennings 2017 Dec 11
*/
#include <Wire.h> // I2C library
#include "ccs811.h" // CCS811 library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Wiring for ESP8266 NodeMCU boards: VDD to 3V3, GND to GND, SDA to D2, SCL to D1, nWAKE to D3 (or GND)
CCS811 ccs811(D3); // nWAKE on D3
//Wifi Settings
const char* ssid = "MyWiffi SSID";
const char* password = "MyWifiPaswd";
//MQTT Settings
const char* mqttServer = "MyIp";
const int mqttPort = 1883;
const char* mqttUser = "MyUser";
const char* mqttPassword = "MyPasswd";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe("CCS811/Info");
client.subscribe("CCS811/CO2");
// Enable I2C
Wire.begin();
// Enable CCS811
ccs811.set_i2cdelay(50); // Needed for ESP8266 because it doesn't handle I2C clock stretch correctly
bool ok= ccs811.begin();
if( !ok ) Serial.println("init: CCS811 begin FAILED");
// Print CCS811 versions
Serial.print("init: hardware version: "); Serial.println(ccs811.hardware_version(),HEX);
Serial.print("init: bootloader version: "); Serial.println(ccs811.bootloader_version(),HEX);
Serial.print("init: application version: "); Serial.println(ccs811.application_version(),HEX);
// Start measuring
ok= ccs811.start(CCS811_MODE_1SEC);
if( !ok ) Serial.println("init: CCS811 start FAILED");
}
void loop() {
// Read
uint16_t eco2, etvoc, errstat, raw;
ccs811.read(&eco2,&etvoc,&errstat,&raw);
// Print measurement results based on status
if( errstat==CCS811_ERRSTAT_OK ) {
Serial.print("CCS811: ");
Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm ");
Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb ");
client.publish("CCS811/CO2", "eco2");
//Serial.print("raw6="); Serial.print(raw/1024); Serial.print(" uA ");
//Serial.print("raw10="); Serial.print(raw%1024); Serial.print(" ADC ");
//Serial.print("R="); Serial.print((1650*1000L/1023)*(raw%1024)/(raw/1024)); Serial.print(" ohm");
Serial.println();
} else if( errstat==CCS811_ERRSTAT_OK_NODATA ) {
Serial.println("CCS811: waiting for (new) data");
client.publish("CCS811/CO2", "waiting for (new) data");
} else if( errstat & CCS811_ERRSTAT_I2CFAIL ) {
Serial.println("CCS811: I2C error");
} else {
Serial.print("CCS811: errstat="); Serial.print(errstat,HEX);
Serial.print("="); Serial.println( ccs811.errstat_str(errstat) );
}
// Wait
delay(1000);
}