Last active
July 18, 2020 23:56
-
-
Save savannahostrowski/b09225ebe0aaaf78524e6f591ad6d9f9 to your computer and use it in GitHub Desktop.
HCDE 598 - In-Class Exercise #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*In-Class Exercise #3 | |
* | |
A simple sketch to log DHT22 temperature and relative humidity data and MPL115A2 pressure data | |
to a 128x32 I2C OLED and Adafruit IO dashboard (https://io.adafruit.com/sostrows/dashboards/ice-3) | |
Savannah Ostrowski | |
*/ | |
/************************** Configuration ***********************************/ | |
#include "config.h" // you will need your own config.h file to run this code | |
/************************ Code Starts Here *******************************/ | |
#include <Wire.h> | |
#include <SPI.h> | |
#include <DHT.h> | |
#include <DHT_U.h> | |
#include <Adafruit_MPL115A2.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
Adafruit_MPL115A2 mpl115a2; // Instantiate an MPL115A2 sensor object | |
#define DATA_PIN 12 // Set pin 12 as connected to DH22 data line | |
#define DEBUG 0 // DEBUG mode: if 1 we are not logging to Adafruit, if 0 we are posting data! | |
DHT_Unified dht(DATA_PIN, DHT22); // Instantiate a DHT22 object | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 32 // OLED display height, in pixels | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Configure display | |
// set up the 'temperature', 'humidity' and 'pressure' feeds | |
AdafruitIO_Feed *temperature = io.feed("temperature"); | |
AdafruitIO_Feed *humidity = io.feed("humidity"); | |
AdafruitIO_Feed *pressure = io.feed("pressure"); | |
void setup(void) | |
{ | |
// set the data rate in bits per second for serial data transmission and print that the program is starting | |
Serial.begin(115200); | |
Serial.println("Starting!"); | |
// wait for serial monitor to open then print build details | |
while(! Serial); | |
// initialize dht22 | |
dht.begin(); | |
// initialize mpl115a2 | |
mpl115a2.begin(); | |
// if not in debug mode | |
if (!DEBUG){ | |
// print and then connect to io.adafruit.com | |
Serial.print("Connecting to Adafruit IO"); | |
io.connect(); | |
// wait for a connection | |
while(io.status() < AIO_CONNECTED) { | |
// print a dot every 500ms | |
Serial.print("."); | |
delay(500); | |
} | |
// we are connected (print the status) | |
Serial.println(); | |
Serial.println(io.statusText()); | |
} | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 | |
Serial.println(F("SSD1306 allocation failed")); // Oops something went wrong ;) | |
for (;;); // Don't proceed, loop forever | |
} | |
// set up the OLED display | |
// by default, we'll generate the high voltage from the 3.3v line internally | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) | |
display.setTextSize(1); // Normal 1:1 pixel scale | |
display.setTextColor(SSD1306_WHITE); // Draw white text | |
display.clearDisplay(); // clear the display | |
display.setCursor(0, 0); // set the cursor to start printing in the top-left corner (at 0,0) | |
display.println("Started up."); // print that the display started | |
delay(2000); // pause for 2000ms | |
} | |
void loop(void) | |
{ | |
// io.run(); is required for all sketches. | |
// it should always be present at the top of your loop | |
// function. it keeps the client connected to | |
// io.adafruit.com, and processes any incoming data. | |
if (!DEBUG){ | |
io.run(); | |
} | |
// Get temperature | |
sensors_event_t event; // Get the event object for the sensor's reading | |
dht.temperature().getEvent(&event); // Get the current DHT22 temperature value | |
float celsius = event.temperature; // Get temperature from the event object (representing a moment in time) | |
Serial.print("celsius: "); // Print celsius: <some value here>C to serial monitor | |
Serial.print(celsius); | |
Serial.println("C"); | |
temperature->save(celsius); // Save data to Adafruit IO temperature feed | |
// Get relative humidity | |
dht.humidity().getEvent(&event); // Get humidity from the event object | |
float rel_humidity = event.relative_humidity; // Get the current DHT22 humidity value | |
Serial.print("humidity: "); | |
Serial.print(event.relative_humidity); | |
Serial.println("%"); | |
humidity->save(event.relative_humidity); // Save data to Adafruit IO humidity feed | |
// Get pressure | |
float pressureKPA = 0; // Reset the pressureKPA value (to keep data fresh) | |
pressureKPA = mpl115a2.getPressure(); // Grab the pressure off of the MPL115A2 sensor | |
Serial.print("Pressure (kPa): "); // Print Pressure (kPa): <some value here> to the serial monitor with up to four decimal places | |
Serial.print(pressureKPA, 4); | |
Serial.println(" kPa"); | |
pressure->save(pressureKPA); // Save data to Adafruit IO pressure feed | |
// Log data to OLED | |
display.clearDisplay(); // clear the display at the beginning of the loop (we are writing new data to the OLED) | |
display.setCursor(0, 0); // Start at top-left corner | |
display.print("Pressure(kPa): "); // Print Pressure(kPa): <some value here> | |
display.println(pressureKPA); | |
display.print("Temp(C): "); // Print Temp(C): <some value here> | |
display.println(celsius); | |
display.print("Rel Hum(%): "); // Print Rel Hum(%): <some value here> | |
display.println(rel_humidity); | |
display.display(); // Set display with data | |
delay(7000); // Pause for 7000ms | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment