Skip to content

Commit 35404b4

Browse files
brentrubrentru
authored andcommitted
add multi-sensor i2c example for upcoming learn guide (#60)
bump library version
1 parent 8878fec commit 35404b4

File tree

3 files changed

+235
-1
lines changed

3 files changed

+235
-1
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Adafruit IO Environmental Data Logger
2+
// Tutorial Link: https://learn.adafruit.com/adafruit-io-air-quality-monitor
3+
//
4+
// Adafruit invests time and resources providing this open source code.
5+
// Please support Adafruit and open source hardware by purchasing
6+
// products from Adafruit!
7+
//
8+
// Written by Brent Rubell for Adafruit Industries
9+
// Copyright (c) 2018 Adafruit Industries
10+
// Licensed under the MIT license.
11+
//
12+
// All text above must be included in any redistribution.
13+
14+
/************************** Adafruit IO Configuration ***********************************/
15+
16+
// edit the config.h tab and enter your Adafruit IO credentials
17+
// and any additional configuration needed for WiFi, cellular,
18+
// or ethernet clients.
19+
#include "config.h"
20+
21+
/**************************** Sensor Configuration ***************************************/
22+
#include <Wire.h>
23+
#include <Adafruit_Sensor.h>
24+
#include <Adafruit_BME280.h>
25+
#include "Adafruit_VEML6070.h"
26+
#include "Adafruit_SGP30.h"
27+
28+
// BME280 Sensor Definitions
29+
#define BME_SCK 13
30+
#define BME_MISO 12
31+
#define BME_MOSI 11
32+
#define BME_CS 10
33+
#define SEALEVELPRESSURE_HPA (1013.25)
34+
35+
// Instanciate the sensors
36+
Adafruit_BME280 bme;
37+
Adafruit_VEML6070 uv = Adafruit_VEML6070();
38+
Adafruit_SGP30 sgp;
39+
40+
/**************************** Example ***************************************/
41+
// Delay between sensor reads, in seconds
42+
#define READ_DELAY 10
43+
44+
// DHT22 Data
45+
int temperatureReading;
46+
int pressureReading;
47+
48+
// SGP30 Data
49+
int tvocReading = 0;
50+
int ecO2Reading = 0;
51+
52+
// BME280 Data
53+
int altitudeReading = 0;
54+
int humidityReading = 0;
55+
56+
// VEML6070 Data
57+
int uvReading = 0;
58+
59+
// set up the feeds for the BME280
60+
AdafruitIO_Feed *temperatureFeed = io.feed("temperature");
61+
AdafruitIO_Feed *humidityFeed = io.feed("humidity");
62+
AdafruitIO_Feed *pressureFeed = io.feed("pressure");
63+
AdafruitIO_Feed *altitudeFeed = io.feed("altitude");
64+
65+
// set up feed for the VEML6070
66+
AdafruitIO_Feed *uvFeed = io.feed("uv");
67+
68+
// set up feeds for the SGP30
69+
AdafruitIO_Feed *tvocFeed = io.feed("tvoc");
70+
AdafruitIO_Feed *ecO2Feed = io.feed("ecO2");
71+
72+
void setup() {
73+
// start the serial connection
74+
Serial.begin(9600);
75+
76+
// wait for serial monitor to open
77+
while (!Serial);
78+
79+
Serial.println("Adafruit IO Environmental Logger");
80+
81+
// set up BME280
82+
setupBME280();
83+
// set up SGP30
84+
setupSGP30();
85+
// setup VEML6070
86+
uv.begin(VEML6070_1_T);
87+
88+
// connect to io.adafruit.com
89+
Serial.print("Connecting to Adafruit IO");
90+
io.connect();
91+
92+
// wait for a connection
93+
while (io.status() < AIO_CONNECTED)
94+
{
95+
Serial.print(".");
96+
delay(500);
97+
}
98+
99+
// we are connected
100+
Serial.println();
101+
Serial.println(io.statusText());
102+
}
103+
104+
void loop() {
105+
// io.run(); is required for all sketches.
106+
// it should always be present at the top of your loop
107+
// function. it keeps the client connected to
108+
// io.adafruit.com, and processes any incoming data.
109+
io.run();
110+
111+
Serial.println("Reading Sensors...");
112+
113+
// Read the temperature from the BME280
114+
temperatureReading = bme.readTemperature();
115+
116+
// convert from celsius to degrees fahrenheit
117+
temperatureReading = temperatureReading * 1.8 + 32;
118+
119+
Serial.print("Temperature = "); Serial.print(temperatureReading); Serial.println(" *F");
120+
121+
// Read the pressure from the BME280
122+
pressureReading = bme.readPressure() / 100.0F;
123+
Serial.print("Pressure = "); Serial.print(pressureReading); Serial.println(" hPa");
124+
125+
// Read the altitude from the BME280
126+
altitudeReading = bme.readAltitude(SEALEVELPRESSURE_HPA);
127+
Serial.print("Approx. Altitude = "); Serial.print(altitudeReading); Serial.println(" m");
128+
129+
// Read the humidity from the BME280
130+
humidityReading = bme.readHumidity();
131+
Serial.print("Humidity = "); Serial.print(humidityReading); Serial.println("%");
132+
133+
// VEML6070
134+
uvReading = uv.readUV();
135+
Serial.print("UV Light Level: "); Serial.println(uvReading);
136+
137+
if(! sgp.IAQmeasure()){
138+
tvocReading = -1;
139+
ecO2Reading = -1;
140+
}
141+
else
142+
{
143+
tvocReading = sgp.TVOC;
144+
ecO2Reading = sgp.eCO2;
145+
}
146+
147+
Serial.print("TVOC: "); Serial.print(tvocReading); Serial.print(" ppb\t");
148+
Serial.print("eCO2: "); Serial.print(ecO2Reading); Serial.println(" ppm");
149+
150+
// send data to Adafruit IO feeds
151+
temperatureFeed->save(temperatureReading);
152+
humidityFeed->save(humidityReading);
153+
altitudeFeed->save(altitudeReading);
154+
pressureFeed->save(pressureReading);
155+
uvFeed->save(uvReading);
156+
ecO2Feed->save(ecO2Reading);
157+
tvocFeed->save(tvocReading);
158+
159+
// delay the polled loop
160+
delay(READ_DELAY * 1000);
161+
}
162+
163+
// Set up the SGP30 sensor
164+
void setupSGP30() {
165+
if (!sgp.begin())
166+
{
167+
Serial.println("Sensor not found :(");
168+
while (1);
169+
}
170+
Serial.print("Found SGP30 serial #");
171+
Serial.print(sgp.serialnumber[0], HEX);
172+
Serial.print(sgp.serialnumber[1], HEX);
173+
Serial.println(sgp.serialnumber[2], HEX);
174+
175+
// If you previously calibrated the sensor in this environment,
176+
// you can assign it to self-calibrate (replace the values with your baselines):
177+
// sgp.setIAQBaseline(0x8E68, 0x8F41);
178+
}
179+
180+
// Set up the BME280 sensor
181+
void setupBME280() {
182+
bool status;
183+
status = bme.begin();
184+
if (!status)
185+
{
186+
Serial.println("Could not find a valid BME280 sensor, check wiring!");
187+
while (1);
188+
}
189+
Serial.println("BME Sensor is set up!");
190+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
14+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
15+
// - Feather WICED -> https://www.adafruit.com/products/3056
16+
17+
#define WIFI_SSID "your_ssid"
18+
#define WIFI_PASS "your_pass"
19+
20+
// comment out the following two lines if you are using fona or ethernet
21+
#include "AdafruitIO_WiFi.h"
22+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
23+
24+
25+
/******************************* FONA **************************************/
26+
27+
// the AdafruitIO_FONA client will work with the following boards:
28+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
29+
30+
// uncomment the following two lines for 32u4 FONA,
31+
// and comment out the AdafruitIO_WiFi client in the WIFI section
32+
// #include "AdafruitIO_FONA.h"
33+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
34+
35+
36+
/**************************** ETHERNET ************************************/
37+
38+
// the AdafruitIO_Ethernet client will work with the following boards:
39+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
40+
41+
// uncomment the following two lines for ethernet,
42+
// and comment out the AdafruitIO_WiFi client in the WIFI section
43+
// #include "AdafruitIO_Ethernet.h"
44+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit IO Arduino
2-
version=2.7.14
2+
version=2.7.15
33
author=Adafruit
44
maintainer=Adafruit <adafruitio@adafruit.com>
55
sentence=Arduino library to access Adafruit IO.

0 commit comments

Comments
 (0)