Skip to content

Instantly share code, notes, and snippets.

@V1DEOJAMES
Last active August 4, 2018 05:40
Show Gist options
  • Save V1DEOJAMES/1b58b96edec79937f611ece7d0b867c8 to your computer and use it in GitHub Desktop.
Save V1DEOJAMES/1b58b96edec79937f611ece7d0b867c8 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>
#include <SoftwareSerial.h>
#define CCS811_ADDR 0x5B //Default I2C Address
#define LENG 31 //0x42 + 31 bytes equal to 32 bytes#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
#define TX D7
#define RX D6
CCS811 myCCS811(CCS811_ADDR);
BME280 myBME280;
SoftwareSerial PMSerial(RX, TX); // RX, TX
const char* ssid = "CN5151-TabA";
const char* password = "81Paw2dud2&5ddddddaP5";
const char* mqtt_server= "192.168.43.57";
unsigned char buf[LENG];
int PM01Value=0; //define PM1.0 value of the air detector module
int PM2_5Value=0; //define PM2.5 value of the air detector module
int PM10Value=0; //define PM10 value of the air detector module
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char tmp[50];
char pm1[50];
char pm2[50];
char pm10[50];
int value = 0;
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) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
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("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} 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()
{
PMSerial.begin(9600);
PMSerial.setTimeout(1500);
Serial.begin(9600);
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();
if (returnCode != CCS811Core::SENSOR_SUCCESS)
{
Serial.println("Problem with CCS811");
printDriverError(returnCode);
}
else
{
Serial.println("CCS811 online");
}
//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.
byte id = myBME280.begin(); //Returns ID of 0x60 if successful
if (id != 0x60)
{
Serial.println("Problem with BME280");
}
else
{
Serial.println("BME280 online");
}
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void printData()
{
Serial.print("CO2: ");
Serial.print(myCCS811.getCO2());
Serial.println(" ppm");
Serial.print("TVOC: ");
Serial.print(myCCS811.getTVOC());
Serial.println(" ppb");
Serial.print("Temp: ");
Serial.print(myBME280.readTempC(), 1);
Serial.println(" C");
Serial.print("Press: ");
Serial.print(myBME280.readFloatPressure(), 2);
Serial.println(" Pa");
Serial.print("Humid: ");
Serial.print(myBME280.readFloatHumidity(), 0);
Serial.println(" %");
}
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.");
}
}
void loop()
{
if (myCCS811.dataAvailable()) //Check to see if CCS811 has new data (it's the slowest sensor)
{
myCCS811.readAlgorithmResults(); //Read latest from CCS811 and update tVOC and CO2 variables
//getWeather(); //Get latest humidity/pressure/temp data from BME280
printData(); //Pretty print all the data
}
else if (myCCS811.checkForStatusError()) //Check to see if CCS811 has thrown an error
{
Serial.println(myCCS811.getErrorRegister()); //Prints whatever CSS811 error flags are detected
}
if(PMSerial.find(0x42)){
PMSerial.readBytes(buf,LENG);
if(buf[0] == 0x4d){
if(checkValue(buf,LENG)){
PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
}
}
}
static unsigned long OledTimer=millis();
if (millis() - OledTimer >=1000)
{
OledTimer=millis();
Serial.print("PM1.0: ");
Serial.print(PM01Value);
Serial.println(" ug/m3");
Serial.print("PM2.5: ");
Serial.print(PM2_5Value);
Serial.println(" ug/m3");
Serial.print("PM1 0: ");
Serial.print(PM10Value);
Serial.println(" ug/m3");
Serial.println();
}
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
//snprintf (pmv, 75, String(PM2_5Value), value);
dtostrf(PM01Value,0, 0, pm1);
dtostrf(PM2_5Value,0, 0, pm2);
dtostrf(PM10Value,0, 0, pm10);
Serial.print("Publishing messages: ");
client.publish("pm1", pm1);
client.publish("pm2", pm2);
client.publish("pm10", pm10);
}
delay(10000);
}
char checkValue(unsigned char *thebuf, char leng)
{
char receiveflag=0;
int receiveSum=0;
for(int i=0; i<(leng-2); i++){
receiveSum=receiveSum+thebuf[i];
}
receiveSum=receiveSum + 0x42;
if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
{
receiveSum = 0;
receiveflag = 1;
}
return receiveflag;
}
int transmitPM01(unsigned char *thebuf)
{
int PM01Val;
PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
return PM01Val;
}
//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
int PM2_5Val;
PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
return PM2_5Val;
}
//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
int PM10Val;
PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
return PM10Val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment