Ethan FanStephanie Ko
Published © MIT

Smart City Waste Bin Management

Smart bin that monitors waste bin air quality, level, temperature, and humidity.

IntermediateWork in progress2 hours5,628

Things used in this project

Hardware components

SparkFun ESP8266 Thing - Dev Board
SparkFun ESP8266 Thing - Dev Board
×1
SparkFun CCS811
×1
SparkFun Humidity and Temperature Sensor Breakout - Si7021
SparkFun Humidity and Temperature Sensor Breakout - Si7021
×1
Adafruit VL53L0X
×1

Software apps and online services

Arduino IDE
Arduino IDE
Cayenne
myDevices Cayenne

Story

Read more

Schematics

Smart Waste Bin

Code

Untitled file

C/C++
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include "SparkFun_Si7021_Breakout_Library.h"
#include "Adafruit_VL53L0X.h"

#include <CCS811.h>
#define ADDR      0x5A
#define WAKE_PIN  15
#define ESP8266_LED 5

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

float humidity = 0;
float tempf = 0;
float tempc = 0;

// WiFi network info.
char ssid[] = "Your SSID";
char wifiPassword[] = "Password";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "--";
char password[] = "--";
char clientID[] = "--";

unsigned long lastMillis = 0;

//Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
Weather weatherSensor;
CCS811 airSensor;

void setup() {
  Serial.begin(9600);
  pinMode(ESP8266_LED, OUTPUT);

  Wire.begin();

  if (!airSensor.begin(uint8_t(ADDR), uint8_t(WAKE_PIN)))    
  Serial.println("Initialization failed.");

  //Initialize the I2C sensors and ping them
  weatherSensor.begin();

  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while (1);
  }


  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  //Get readings from all sensors
  getWeather();
  printTemp();
  printAirQuality();

  VL53L0X_RangingMeasurementData_t measure;
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }

  Cayenne.loop();
  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    //Cayenne.virtualWrite(0, lastMillis);
    //Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    Cayenne.virtualWrite(1, tempf, TYPE_TEMPERATURE, UNIT_FAHRENHEIT);
    Cayenne.virtualWrite(2, humidity);
    if (measure.RangeStatus != 4) {  // phase failures have incorrect data
      Cayenne.virtualWrite(3, measure.RangeMilliMeter / 10, TYPE_PROXIMITY, UNIT_CENTIMETER);
    } else {
      Cayenne.virtualWrite(3, 100, TYPE_PROXIMITY, UNIT_CENTIMETER);
    }

    Cayenne.virtualWrite(4, airSensor.readCO2()); //CO2
    Cayenne.virtualWrite(5, airSensor.readTVOC()); //TVOC
  }

  delay(2000);
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
    digitalWrite(ESP8266_LED, LOW);
  }
  else {
    digitalWrite(ESP8266_LED, HIGH);
  }
}

//---------------------------------------------------------------
void getWeather()
{
  // Measure Relative Humidity from the HTU21D or Si7021
  humidity = weatherSensor.getRH();

  // Measure Temperature from the HTU21D or Si7021
  tempf = weatherSensor.getTempF();
  tempc = weatherSensor.getTemp();
  // Temperature is measured every time RH is requested.
  // It is faster, therefore, to read it from previous RH
  // measurement with getTemp() instead with readTemp()
}
//---------------------------------------------------------------
void printTemp()
{
//This function prints the weather data out to the default Serial Port

  Serial.print("Temp:");
  Serial.print(tempf);
  Serial.print("F, ");

  Serial.print("Humidity:");
  Serial.print(humidity);
  Serial.println("%");
}

void printAirQuality(){
  //airSensor.compensate(tempc, humidity);  
  // replace with t and rh values from sensor  
  airSensor.getData();  
  Serial.print("CO2 concentration : "); 
  Serial.print(airSensor.readCO2()); Serial.println(" ppm");  
  Serial.print("TVOC concentration : "); 
  Serial.print(airSensor.readTVOC()); 
  Serial.println(" ppb");  
  Serial.println(); 
}

Credits

Ethan Fan

Ethan Fan

9 projects • 36 followers
Mobile, Watch, and Glass Developer
Stephanie Ko

Stephanie Ko

2 projects • 10 followers
Mobile & IOT developer

Comments