Thanks.
Here is the code - with warts - not everything planned is implemented (sqlite3 and gas sensor) and some pieces that didn't work the way I thought they would are commented out. Any comments or criticisms are welcome.
This Oak is currently on a breadboard with connections to my Windows 7 desktop via the FTDI to USB only. It is connected to a powered hub and is the only device on the hub.
This code is currently running on my other Oak.
// Oak environmental monitor
// Interfaces with photoresistor, DHT22 temperature/humidity sensor,
// MQ135 gas sensor, and on-board LED (with console using usb-serial adapter)
/*-----------------------------------------
* DHT22 Wiring
* 1 -- 3V3 -- 3V3 -- Red
* 2 -- Data -- D7 -- White
* 3 -- .. -- nc -- ..
* 4 -- GND -- GND -- Black
* 4.7 Kohm resistor between pins 1 and 2
*
* BMP180 Wiring
* 1 -- SDA -- 0
* 2 -- SCL -- 2
* 3 -- GND
* 4 -- 3V3
*/
#include "DHT.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <SPI.h>
#include "sqlite3.h"
#define DEBUG 1
#define DHTPIN 5
#define DHTTYPE DHT22
// variables to store our readings
int light = 0;
int count = 0;
double tempc = 0.0;
double tempf = 0.0;
double humidity = 0.0;
double dewpt = 0.0;
double heatidx = 0.0;
double BMPtemp = 0.0;
float bmptemp = 0.0;
double BMPpressure = 0.0;
float bmppress = 0.0;
DHT dht(DHTPIN, DHTTYPE);
void readDHT22(){
humidity = dht.readHumidity();
tempc = dht.readTemperature(); // Read as Celsius
tempf = tempc * 1.8 + 32.0; // convert to Fahrenheit
dewpt = dewPoint(tempc, humidity);
heatidx = dht.computeHeatIndex(tempf, humidity);
//heatidx = dht.computeHeatIndex(tempc, humidity);
}
// Virtual Pins of the BMP180 widget.
#define BAROMETER_PIN V1
#define TEMPERATURE_PIN V2
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10180);
bool bmpSensorDetected = true;
void readBMP180(){
if (bmpSensorDetected){
bmp.getTemperature(&bmptemp);
bmptemp = bmptemp * 1.8 + 32.0; // convert to F
BMPtemp = bmptemp;
bmp.getPressure(&bmppress);
bmppress = bmppress * 0.0002953337; // convert to inHg
BMPpressure = bmppress;
}
}
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP / 0.61078); // temp var
return ((241.88 * T) / (17.558 - T))*1.8 + 32.0; // change to Fahrenheit
}
void setup()
{
// enable the on-board LED as an output
pinMode(1, OUTPUT);
// initialize the analog pin as an input
pinMode(A0, INPUT);
// start serial communication and create a variable for particle.io access
Serial.begin(9600);
dht.begin();
if (!bmp.begin()) {
Serial.println("No BMP sensor detected");
bmpSensorDetected = false;
}
Particle.variable("light", light);
Particle.variable("count", count);
Particle.variable("temp", tempf);
Particle.variable("humidity", humidity);
Particle.variable("dewpoint", dewpt);
Particle.variable("heatindex", heatidx);
Particle.variable("BMPtemp", BMPtemp);
Particle.variable("BMPpress", BMPpressure);
}
// the loop() routine runs over and over again
void loop()
{
// take a reading
light = analogRead(A0);
// print it out
Serial.println("Light: " + String(light));
readDHT22();
Serial.println("DHT22: Temp: " + String(tempf) + " Humidity: " + String(humidity));
Serial.println("DHT22: Dewpoint: " + String(dewpt) + " Heat Index: " + String(heatidx));
readBMP180();
Serial.println("BMP180: Temp: " + String(bmptemp) + " Pressure: " + String(bmppress));
// Battv = adc_single_channel_read(adc_single_ch6);
// Solarv = adc_single_channel_read(adc_single_ch7);
// Serial.println("Battery: " + String(Battv) + " Solar: " + String(Solarv));
// turn on/off the LED based on light reading
if(light < 350) { digitalWrite(1, HIGH); }
if(light >= 350) {digitalWrite(1, LOW); }
count++;
// a short delay
delay(15000);
}