Hi Rob,
Here's my Arduino sketch. Could probably be a bit tidier but it works
.
I'll zip the VB code and put it on google drive for you. You might also need the excel spreadsheet to see how the calibration works and add your own data points.
//
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include "Adafruit_TSL2591.h"
#include "Adafruit_SHT31.h"
#include "DHT.h"
Adafruit_BMP085 bmp;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
Adafruit_SHT31 sht31 = Adafruit_SHT31();
#define DHTPIN 12 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int LightSensorPin = A3; // select the input pin for LDR
float lsv = 0; // variable to store the value coming from the sensor
int RainSensorPin = A1; // select the input pin for Rain
float rsv = 0; // variable to store the rain sensor value
int WindSensorPin = A2; // select the input pin for Wind
float wsv = 0; // variable to store the wind sensor value
boolean debug = false;
uint16_t ir;
uint16_t full;
float IR;
float FULL;
float vis;
float lux;
float del;
float last;
float current = 10000;
float nightTh = 10;
float dayTh = 10000;
bool allowChange = true;
int readCount = 0;
String mode = "DAY";
int tslm=1;
char val; // data received from serial port
int ledPin = 13;
boolean ledState = LOW;
boolean contact = false;
int waitTime;
int delayTime = 0;
unsigned long startTime;
unsigned long finishTime;
void setup() {
pinMode(ledPin, OUTPUT); // set ledPin pin as output
Serial.begin(9600);
dht.begin();
mlx.begin();
bmp.begin();
tsl.begin();
sht31.begin();
// start with TSL sensor in DAY mode
tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
tsl.setTiming(TSL2591_INTEGRATIONTI ME_500MS);
establishContact(); // send an 'A' down the serial port until the PC responds
}
void loop() {
startTime=millis();
if (Serial.available() > 0){
val = Serial.read();
delayTime = val - 48;
}
if (delayTime==0){
ledState = LOW; // turn the LED off
contact = false;
digitalWrite(ledPin, ledState);
waitTime = 1000;
}
if (delayTime>0){
ledState = HIGH; // turn the LED on
contact = true;
digitalWrite(ledPin, ledState);
waitTime = 1000 * delayTime;
}
//perform the TSL light readings and gain control functions
last = current;
sensorRead(); // perform sensor read routine
// Serial.print(F("Mode: ")); Serial.print(mode); Serial.print(F(" "));
// Serial.print(F("ReadCount: ")); Serial.print(readCount); Serial.print(F(" "));
// Serial.print(F("AllowChange: ")); Serial.print(allowChange); Serial.print(F(" "));
// Serial.print(F("Last: ")); Serial.print(last); Serial.print(F(" "));
// Serial.print(F("Current: ")); Serial.println(current);
if((current < last) && (mode == "DAY")){
// getting darker and in Day mode - consider night mode
//Serial.println(F("running night mode routine"));
nightMode();
}
else if ((current >= last) && (mode == "NIGHT")){
// getting lighter and in Night mode - consider day mode
//Serial.println(F("running day mode routine"));
dayMode();
}
if(readCount > 6){
allowChange = true;
}
// read the other sensors
// Read humidity and temperature from the SHT31
float sh = sht31.readHumidity();
float st = sht31.readTemperature();
// Read humidity and temperature from the DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
// Read ambinet and sky temperature from the MLX
float a = mlx.readAmbientTempC();
float s = mlx.readObjectTempC();
// Read barometric pressure at ambient temperature from BMP
float at = bmp.readTemperature();
float p = bmp.readPressure() / 100;
// read the value from the LDR light sensor
lsv = analogRead(LightSensorPin);
// read the value from the rain sensor
rsv = analogRead(RainSensorPin);
// read the value from the wind sensor
wsv = analogRead(WindSensorPin);
// random number to include in checksum
int rcs = int(random(0,99));
// checksum based on BMP pressure, DHT Humidity, Rain, Light, Vis and IR values
// int chksum = int(st)+int(t)+int(a)+int(at)+int(r sv)+int(wsv)+rcs;
int chksum = int(p)+int(IR)+int(vis)+int(tslm)+i nt(rsv)+int(wsv)+rcs;
if (contact==true) {
// set up print string for PC interface
String dataTrans = "V:";
dataTrans += String(st,1); // SHT31 ambient temp
dataTrans += String(':');
dataTrans += String(t,1); // DHT ambient temp
dataTrans += String(':');
dataTrans += String(a,1); // MLX ambient temp
dataTrans += String(':');
dataTrans += String(at,1); // BMP ambient temp
dataTrans += String(':');
dataTrans += String(p,0); // barometric pressure
dataTrans += String(':');
dataTrans += String(sh,1); // SHT31 humidity
dataTrans += String(':');
dataTrans += String(h,1); // DHT humidity
dataTrans += String(':');
dataTrans += String(s,1); // sky temperature
dataTrans += String(':');
dataTrans += String(lsv,0); // LDR coarse light sensor
dataTrans += String(':');
dataTrans += String(IR,0); // TSL light sensor IR
dataTrans += String(':');
dataTrans += String(vis,0); // TSL light sensor visible
dataTrans += String(':');
dataTrans += String(FULL,0); // TSL light sensor full
dataTrans += String(':');
dataTrans += String(lux,4); // TSL light sensor lux
dataTrans += String(':');
dataTrans += String(tslm); // TSL mode (0=night, 1=day)
dataTrans += String(':');
dataTrans += String(rsv,0); // rain sensor value
dataTrans += String(':');
dataTrans += String(wsv,0); // wind sensor value
dataTrans += String(':');
dataTrans += String(rcs); // random integer
dataTrans += String(':');
dataTrans += String(chksum); // checksum value
dataTrans += String(';');
//send the string to the PC
Serial.println(dataTrans); //
}
else {
Serial.println("A");
}
finishTime=millis();
int elapsedTime = int(finishTime - startTime);
if (waitTime > elapsedTime){
// Pause between measurements.
delay(waitTime - elapsedTime);
}
}
void establishContact() {
while (Serial.available() <= 0){
Serial.println("A");
delay(1000);
}
}
void sensorRead() {
// Advanced data read. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
uint32_t lum = tsl.getFullLuminosity();
uint16_t ir, full;
ir = lum >> 16;
full = lum & 0xFFFF;
lux = tsl.calculateLux(full, ir);
IR = float(ir);
FULL = float(full);
vis = full - ir;// TSL visible light
current = FULL;
//Serial.print(F("IR: ")); Serial.print(ir); Serial.print(F(" "));
//Serial.print(F("full: ")); Serial.print(full); Serial.print(F(" "));
//Serial.print(F("vis: ")); Serial.print(vis); Serial.print(F(" "));
//Serial.print(F("lux: ")); Serial.println(lux);
readCount = readCount + 1;
return;
}
void nightMode() {
if(debug) Serial.println(F("running night mode routine"));
if((current < nightTh) && (allowChange = true)){
if(debug) Serial.println(F("met criteria - change to night mode"));
// Serial.print(F("Mode: ")); Serial.print(mode); Serial.print(F(" "));
// Serial.print(F("Current: ")); Serial.print(current); Serial.print(F(" "));
// Serial.print(F("nightTh: ")); Serial.print(nightTh); Serial.print(F(" "));
// Serial.print(F("allow change: ")); Serial.println(allowChange);
tsl.setGain(TSL2591_GAIN_HIGH); // switch to night mode 428x gain
mode = "NIGHT";
tslm = 0;
allowChange = false; // switch allow change to false
readCount = 0;
return;
}
return;
}
void dayMode() {
if(debug) Serial.println(F("running day mode routine"));
if((current > dayTh) && (allowChange = true)){
if(debug) Serial.println(F("met criteria - change to day mode"));
// Serial.print(F("Mode: ")); Serial.print(mode); Serial.print(F(" "));
// Serial.print(F("Current: ")); Serial.print(current); Serial.print(F(" "));
// Serial.print(F("dayTh: ")); Serial.print(dayTh); Serial.print(F(" "));
// Serial.print(F("allow change: ")); Serial.println(allowChange);
tsl.setGain(TSL2591_GAIN_LOW); // switch to day mode 1x gain
mode = "DAY";
tslm = 1;
allowChange = false; // switch allow change to false
readCount = 0;
return;
}
return;
}