浏览量: 328 次浏览

ccs811空气质量检测

2020年2月2日 2 作者 Nie Hen

ccs811

能够检测 空气中的CO2 以及 VOC(挥发性有机物)

arduino开发

安装Adafruit_CCS811 库
GitHub链接

使用代码 CCS811_test 示例代码
按照下图连线

enter description here
使用的I2C通信
SDA—-数据线 SCL—–控制线. 是SCL\SDA是I2C总线的信号线. SDA是双向数据线,SCL是时钟线SCL.

enter description here

示例代码

#include "Adafruit_CCS811.h"

Adafruit_CCS811 ccs;

void setup() {
  Serial.begin(9600);

  Serial.println("CCS811 test");

  if(!ccs.begin()){
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }

  // Wait for the sensor to be ready
  while(!ccs.available());
}

void loop() {
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm, TVOC: ");
      Serial.print(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  delay(500);
}

连接esp8266

enter description here

通过GPIO4 SDA,GPIO5 SCL 两个GPIO口 实现与CCS811的连接获取到相应的值并返回

使用的代码与上面在arduino中用到的基本一致改改引脚 就可以
不太会的同学 可以参考学习
esp8266 mqtt使用arduino 开发

esp8266 mqtt 使用arduino开发教程

esp8266-12f使用介绍

esp8266-12f介绍与使用

enter description here
参考

enter description here

参考代码

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Adafruit_CCS811.h"

const char* ssid = "nihen_pc";
const char* password = "*";
const char* mqtt_server = "192.168.137.41";

Adafruit_CCS811 ccs;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 2000;  // 发送值的间隔时间 ms
int val = 0;
String tmpTopic;
String strplayload;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {

 tmpTopic = String((char *)topic);
 payload[length] = '\0';
 strplayload  = String((char*)payload);

 client.publish("older", "ccs811 get the older");

  if (strplayload == "sleep") {
      value = 10000;
     client.publish("older", "ccs811 will go sleep");
  } else if (strplayload == "work") {
      value = 2000;
     client.publish("older", "ccs811 will go work");
  } 
}                  

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...

      client.publish("ccs811/status", "init...");
      // ... and resubscribe
      client.subscribe("older");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {

  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  if(!ccs.begin()){
    Serial.println("Failed to start sensor! Please check your wiring.");

  }
  //calibrate temperature sensor
  if(ccs.available()){
  float temp = ccs.calculateTemperature();
  ccs.setTempOffset(temp - 25.0);
  }
}

void loop() {

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

  long now = millis();
  if (now - lastMsg > value) {
    if(ccs.available()){
    float temp = ccs.calculateTemperature();
    if(!ccs.readData()){
      Serial.println("CO2: "+String(ccs.geteCO2())+"ppm, TVOC: "+String(ccs.getTVOC())+"ppb   Temp:"+String(temp));
      char get_co2[5];
      char get_voc[5];
      dtostrf(ccs.geteCO2(),3,3,get_co2);
      dtostrf(ccs.getTVOC(),3,3,get_voc);
      client.publish("co2/value", get_co2);
      client.publish("voc/value", get_voc);
    }
    else{ 
      client.publish("ccs811/status", "Sensor read ERROR!");
      Serial.println("Sensor read ERROR!");
      ccs.readData();
    }
  }

  }
}