The code waits ten seconds between sensor reads and publishing to Adafruit IO. We can adjust this by increasing or decreasing the READ_DELAY variable at the top of our code. 

// Delay between sensor reads, in seconds
#define READ_DELAY 10

The next chunk of code sets up feed instances to hold the data produced by the sensors.

// set up the feeds for the BME280
AdafruitIO_Feed *temperatureFeed = io.feed("temperature");
AdafruitIO_Feed *humidityFeed = io.feed("humidity");
AdafruitIO_Feed *pressureFeed = io.feed("pressure");
AdafruitIO_Feed *altitudeFeed = io.feed("altitude");
// set up feed for the VEML6070
AdafruitIO_Feed *uvFeed = io.feed("uv");
// set up feeds for the SGP30
AdafruitIO_Feed *tvocFeed = io.feed("tvoc");
AdafruitIO_Feed *ecO2Feed = io.feed("ecO2");

In the setup function, we make calls to setupBME280() and setupSGP30(), which set up both of these sensors. We're also going to set up the VEML6070 by calling uv.begin(), then connect to Adafruit IO. The code will wait until you have a valid connection to Adafruit IO before continuing with the sketch.

If you have any issues connecting, check config.h for any typos in your username, key, or WiFi credentials. 

void setup() {
  // start the serial connection
  Serial.begin(9600);

  // wait for serial monitor to open
  while (!Serial);

  Serial.println("Adafruit IO Environmental Logger");

  // set up BME280
  setupBME280();
  // set up SGP30
  setupSGP30();
  // setup VEML6070
  uv.begin(VEML6070_1_T);

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

  // wait for a connection
  while (io.status() < AIO_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());
}

The code inside the loop() obtains values from the sensors and saves their current state.  The SGP30 sometimes fails on read (due to timing). If this occurs, we'll set the measurements of tvocReading and ECO2Reading to -1  so we can catch this type of error in our dashboard. 

Serial.println("Reading Sensors...");

  // Read the temperature from the BME280
  temperatureReading = bme.readTemperature();

  // convert from celsius to degrees fahrenheit
  temperatureReading = temperatureReading * 1.8 + 32;
  
  Serial.print("Temperature = "); Serial.print(temperatureReading); Serial.println(" *F");

  // Read the pressure from the BME280
  pressureReading = bme.readPressure() / 100.0F;
  Serial.print("Pressure = "); Serial.print(pressureReading); Serial.println(" hPa");

  // Read the altitude from the BME280
  altitudeReading = bme.readAltitude(SEALEVELPRESSURE_HPA);
  Serial.print("Approx. Altitude = "); Serial.print(altitudeReading); Serial.println(" m");
  
  // Read the humidity from the BME280
  humidityReading = bme.readHumidity();
  Serial.print("Humidity = "); Serial.print(humidityReading); Serial.println("%");

  // VEML6070
  uvReading = uv.readUV();
  Serial.print("UV Light Level: "); Serial.println(uvReading);
  if(! sgp.IAQmeasure()){
    tvocReading = -1;
    ecO2Reading = -1;  
  } else {
    tvocReading = sgp.TVOC;
    ecO2Reading = sgp.eCO2;  
  }

The final chunk of the loop() function sends sensor data to the feeds associated with that data. We also delay the polled loop to wait for the temperature to change.

  // send data to Adafruit IO feeds
  temperatureFeed->save(temperatureReading);
  humidityFeed->save(humidityReading);
  altitudeFeed->save(altitudeReading);
  pressureFeed->save(pressureReading);
  uvFeed->save(uvReading);
  ecO2Feed->save(ecO2Reading);
  tvocFeed->save(tvocReading);

Upload the sketch to your board and open the Arduino Serial Monitor (Tools -> Serial Monitor). You should see the board connecting to Adafruit IO, obtain sensor readings, and send them to Adafruit IO:

Adafruit IO Environmental Logger
BME Sensor is set up!
Found SGP30 serial #064F41A
Connecting to Adafruit IOAdafruitIO::connect()
.
Adafruit IO connected.
Reading Sensors...
Temperature = 82 *F
Pressure = 1006 hPa
Approx. Altitude = 56 m
Humidity = 43%
UV Light Level: 3
TVOC: 0 ppb	eCO2: 400 ppm

Check your Dashboard on Adafruit IO and you should see your dashboard populated with values. You should also see them change every ten seconds.

This guide was first published on Aug 16, 2018. It was last updated on Nov 07, 2023.

This page (Arduino Code) was last updated on Aug 09, 2018.

Text editor powered by tinymce.