diff --git a/Arduino/Sketches/ST_Anything_I2C_ESP8266WiFi/ST_Anything_I2C_ESP8266WiFi.ino b/Arduino/Sketches/ST_Anything_I2C_ESP8266WiFi/ST_Anything_I2C_ESP8266WiFi.ino index 1b5aeef3..07e8d2ff 100644 --- a/Arduino/Sketches/ST_Anything_I2C_ESP8266WiFi/ST_Anything_I2C_ESP8266WiFi.ino +++ b/Arduino/Sketches/ST_Anything_I2C_ESP8266WiFi/ST_Anything_I2C_ESP8266WiFi.ino @@ -14,6 +14,8 @@ // - 1 x BMP280 Temperature and Pressure sensor (same I2C address as BEP280) // - 1 x TCS34725 Color Illuminance sensor // - 1 x TSL2561 Illuminance sensor +// - 1 x MAX44009 Illuminance sensor +// - 1 x BH1750 Illuminance sensor // // Change History: // @@ -21,6 +23,7 @@ // ---- --- ---- // 2015-01-03 Dan & Daniel Original Creation // 2018-07-02 Dan Ogorchock Revised to demonstrate I2C sensors +// 2018-07-04 Dan Ogorchock Added MAX44009 and BH1750 Lux Sensors // //****************************************************************************************** //****************************************************************************************** @@ -57,6 +60,8 @@ #include //Implements a Polling Sensor (PS) to measure Temperature and humidity using AM2320 via I2C #include //Implements a Polling Sensor (PS) to measure Color Illuminance using TCS34725 via I2C #include //Implements a Polling Sensor (PS) to measure Illuminance using TSL2561 via I2C +#include //Implements a Polling Sensor (PS) to measure Illuminance using MAX44009 via I2C +#include //Implements a Polling Sensor (PS) to measure Illuminance using BH1750 via I2C //************************************************************************************************* //NodeMCU v1.0 ESP8266-12e Pin Definitions (makes it much easier as these match the board markings) @@ -141,10 +146,12 @@ void setup() //Polling Sensors (eaxmples of various I2C sensors supported in ST_Anything) static st::PS_AdafruitBME280_TempHumidPress sensor1(F("BME280_1"), 60, 0, "temperature1", "humidity1", "pressure1", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time - //static st::PS_AdafruitBMP280_TempPress sensor2(F("BMP280_1"), 60, 10, "temperature2", "pressure2", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time +// static st::PS_AdafruitBMP280_TempPress sensor2(F("BMP280_1"), 60, 10, "temperature2", "pressure2", false, 100, 0x77); //both BME280 and BMP280 use address 0x77 - only use one at a time static st::PS_AdafruitAM2320_TempHumid sensor3(F("AM2320_1"), 60, 20, "temperature3", "humidity3", false, 100); static st::PS_AdafruitTCS34725_Illum_Color sensor4(F("illuminancergb1"), 60, 30, TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_4X); static st::PS_AdafruitTSL2561_Illuminance sensor5(F("illuminance1"), 60, 40, TSL2561_ADDR_FLOAT, TSL2561_INTEGRATIONTIME_13MS, TSL2561_GAIN_1X); + static st::PS_MAX44009_Illuminance sensor6(F("illuminance2"), 60, 50, MAX44009_A0_LOW); + static st::PS_BH1750_Illuminance sensor7(F("illuminance3"), 60, 55, BH1750_ADDR_LOW); //Executors @@ -181,13 +188,14 @@ void setup() //***************************************************************************** //Add each sensor to the "Everything" Class //***************************************************************************** - st::Everything::addSensor(&sensor1); -// st::Everything::addSensor(&sensor2); + st::Everything::addSensor(&sensor1); //if uncommented, must comment out sensor2 below as they both use same I2C address by default +// st::Everything::addSensor(&sensor2); //if uncommented, must comment out sensor1 above as they both use same I2C address by default st::Everything::addSensor(&sensor3); st::Everything::addSensor(&sensor4); st::Everything::addSensor(&sensor5); - - + st::Everything::addSensor(&sensor6); + st::Everything::addSensor(&sensor7); + //***************************************************************************** //Add each executor to the "Everything" Class //***************************************************************************** @@ -210,4 +218,4 @@ void loop() //Execute the Everything run method which takes care of "Everything" //***************************************************************************** st::Everything::run(); -} +} \ No newline at end of file diff --git a/Arduino/libraries/BH1750/.gitignore b/Arduino/libraries/BH1750/.gitignore new file mode 100644 index 00000000..e43b0f98 --- /dev/null +++ b/Arduino/libraries/BH1750/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/Arduino/libraries/BH1750/.travis.yml b/Arduino/libraries/BH1750/.travis.yml new file mode 100644 index 00000000..6b937cef --- /dev/null +++ b/Arduino/libraries/BH1750/.travis.yml @@ -0,0 +1,27 @@ +language: cpp +env: + global: + - ARDUINO_PACKAGE_VERSION=1.8.5 + - DISPLAY=:1.0 + +before_install: + # arduino requires an X server even with command line + - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/xvfb_$TRAVIS_JOB_NUMBER.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16 + +install: + - wget -q -O- http://downloads.arduino.cc/arduino-$ARDUINO_PACKAGE_VERSION-linux64.tar.xz | tar -Jxf - + - sudo mv arduino-$ARDUINO_PACKAGE_VERSION /usr/local/share/arduino + - sudo ln -s /usr/local/share/arduino/arduino /usr/local/bin/arduino + # Add esp8266 board support + # - arduino --pref "boardsmanager.additional.urls=http://arduino.esp8266.com/stable/package_esp8266com_index.json" --save-prefs + # - arduino --install-boards esp8266:esp8266 + # link project directory into Arduino libraries area. + - ln -s $PWD /usr/local/share/arduino/libraries/BH1750 + +script: + # uno + - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750test/BH1750test.ino + - arduino --verify --board arduino:avr:uno $PWD/examples/BH1750advanced/BH1750advanced.ino + # esp8266 + # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570test/BH1750test.ino + # - arduino --verify --board esp8266:esp8266:generic $PWD/examples/BH1570advanced/BH1750advanced.ino diff --git a/Arduino/libraries/BH1750/BH1750.cpp b/Arduino/libraries/BH1750/BH1750.cpp new file mode 100644 index 00000000..f82c3707 --- /dev/null +++ b/Arduino/libraries/BH1750/BH1750.cpp @@ -0,0 +1,207 @@ +/* + + This is a library for the BH1750FVI Digital Light Sensor breakout board. + + The BH1750 board uses I2C for communication. Two pins are required to + interface to the device. Configuring the I2C bus is expected to be done + in user code. The BH1750 library doesn't do this automatically. + + Written by Christopher Laws, March, 2013. + + Modified by Dan Ogorchock on 2018-07-04 for ST_Anything compatability + +*/ + +#include "BH1750.h" + +// Define milliseconds delay for ESP8266 platform +#if defined(ESP8266) + + #include + #define _delay_ms(ms) delayMicroseconds((ms) * 1000) + +// Use _delay_ms from utils for AVR-based platforms +#elif defined(__avr__) + #include + +// Use Wiring's delay for compability with another platforms +#else + #define _delay_ms(ms) delay(ms) +#endif + + +// Legacy Wire.write() function fix +#if (ARDUINO >= 100) + #define __wire_write(d) Wire.write(d) +#else + #define __wire_write(d) Wire.send(d) +#endif + + +// Legacy Wire.read() function fix +#if (ARDUINO >= 100) + #define __wire_read() Wire.read() +#else + #define __wire_read() Wire.receive() +#endif + + +/** + * Constructor + * @params addr Sensor address (0x23 or 0x5C, see datasheet) + * + * Default for BH1750 is 0x23 + */ +BH1750::BH1750(byte addr) { + + BH1750_I2CADDR = addr; + +} + + +/** + * Configure sensor + * @param mode Measurement mode + */ +bool BH1750::begin(Mode mode) { + + // I2C is expected to be initialized outside this library + //DGO Revision for ST_Anything compatability - all other I2C sensor initialize I2C. + // This one was the exception, but I have modified it to be like the others. + Wire.begin(); + // Configure sensor in specified mode + return configure(mode); + +} + + +/** + * Configure BH1750 with specified mode + * @param mode Measurement mode + */ +bool BH1750::configure(Mode mode) { + + // default transmission result to a value out of normal range + byte ack = 5; + + // Check measurement mode is valid + switch (mode) { + + case BH1750::CONTINUOUS_HIGH_RES_MODE: + case BH1750::CONTINUOUS_HIGH_RES_MODE_2: + case BH1750::CONTINUOUS_LOW_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + case BH1750::ONE_TIME_LOW_RES_MODE: + + // Send mode to sensor + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + ack = Wire.endTransmission(); + + // Wait a few moments to wake up + _delay_ms(10); + break; + + default: + // Invalid measurement mode + Serial.println(F("[BH1750] ERROR: Invalid mode")); + break; + + } + + // Check result code + switch (ack) { + case 0: + BH1750_MODE = mode; + return true; + case 1: // too long for transmit buffer + Serial.println(F("[BH1750] ERROR: too long for transmit buffer")); + case 2: // received NACK on transmit of address + Serial.println(F("[BH1750] ERROR: received NACK on transmit of address")); + case 3: // received NACK on transmit of data + Serial.println(F("[BH1750] ERROR: received NACK on transmit of data")); + case 4: // other error + Serial.println(F("[BH1750] ERROR: other error")); + default: + Serial.println(F("[BH1750] ERROR: undefined error")); + break; + } + + return false; + +} + + +/** + * Read light level from sensor + * @return Light level in lux (0 ~ 65535) + */ +uint16_t BH1750::readLightLevel(bool maxWait) { + + if (BH1750_MODE == UNCONFIGURED) { + Serial.println(F("[BH1750] Device is not configured!")); + return 0; + } + + // Measurement result will be stored here + uint16_t level=65535; + + // Send mode to sensor + Wire.beginTransmission(BH1750_I2CADDR); + __wire_write((uint8_t)BH1750_MODE); + Wire.endTransmission(); + + // Wait for measurement to be taken. + // Measurements have a maximum measurement time and a typical measurement + // time. The maxWait argument determines which measurement wait time is + // used when a one-time mode is being used. The typical (shorter) + // measurement time is used by default and if maxWait is set to True then + // the maximum measurement time will be used. See data sheet pages 2, 5 + // and 7 for more details. + // A continuous mode measurement can be read immediately after re-sending + // the mode command. + + switch (BH1750_MODE) { + + case BH1750::ONE_TIME_LOW_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE: + case BH1750::ONE_TIME_HIGH_RES_MODE_2: + + if (BH1750_MODE == BH1750::ONE_TIME_LOW_RES_MODE) { + maxWait ? _delay_ms(24) : _delay_ms(16); + } + else { + maxWait ? _delay_ms(180) :_delay_ms(120); + } + break; + default: break; + } + + // Read two bytes from the sensor, which are low and high parts of the sensor + // value + Wire.requestFrom(BH1750_I2CADDR, 2); + if (Wire.available() == 2) { + level = __wire_read(); + level <<= 8; + level |= __wire_read(); + } + + // Print raw value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Raw value: ")); + Serial.println(level); + #endif + + // Convert raw value to lux + level /= 1.2; + + // Print converted value if debug enabled + #ifdef BH1750_DEBUG + Serial.print(F("[BH1750] Converted value: ")); + Serial.println(level); + #endif + + return level; + +} diff --git a/Arduino/libraries/BH1750/BH1750.h b/Arduino/libraries/BH1750/BH1750.h new file mode 100644 index 00000000..ca68afb7 --- /dev/null +++ b/Arduino/libraries/BH1750/BH1750.h @@ -0,0 +1,76 @@ +/* + + This is a library for the BH1750FVI Digital Light Sensor + breakout board. + + The BH1750 board uses I2C for communication. Two pins are required to + interface to the device. Configuring the I2C bus is expected to be done + in user code. The BH1750 library doesn't do this automatically. + + Datasheet: http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf + + Written by Christopher Laws, March, 2013. + + Modified by Dan Ogorchock on 2018-07-04 for ST_Anything compatability +*/ + +#ifndef BH1750_h +#define BH1750_h + +#if (ARDUINO >= 100) + #include +#else + #include +#endif + +#include "Wire.h" + +// Uncomment, to enable debug messages +// #define BH1750_DEBUG + +// No active state +#define BH1750_POWER_DOWN 0x00 + +// Wating for measurement command +#define BH1750_POWER_ON 0x01 + +// Reset data register value - not accepted in POWER_DOWN mode +#define BH1750_RESET 0x07 + +//I2C Addresses +#define BH1750_ADDR_LOW 0x23 +#define BH1750_ADDR_HIGH 0x5C + +class BH1750 { + + public: + + enum Mode + { + UNCONFIGURED = 0, + // Measurement at 1lx resolution. Measurement time is approx 120ms. + CONTINUOUS_HIGH_RES_MODE = 0x10, + // Measurement at 0.5lx resolution. Measurement time is approx 120ms. + CONTINUOUS_HIGH_RES_MODE_2 = 0x11, + // Measurement at 4lx resolution. Measurement time is approx 16ms. + CONTINUOUS_LOW_RES_MODE = 0x13, + // Measurement at 1lx resolution. Measurement time is approx 120ms. + ONE_TIME_HIGH_RES_MODE = 0x20, + // Measurement at 0.5lx resolution. Measurement time is approx 120ms. + ONE_TIME_HIGH_RES_MODE_2 = 0x21, + // Measurement at 1lx resolution. Measurement time is approx 120ms. + ONE_TIME_LOW_RES_MODE = 0x23 + }; + + BH1750(byte addr = BH1750_ADDR_LOW); + bool begin(Mode mode = CONTINUOUS_HIGH_RES_MODE); + bool configure(Mode mode); + uint16_t readLightLevel(bool maxWait = false); + + private: + int BH1750_I2CADDR; + Mode BH1750_MODE = UNCONFIGURED; + +}; + +#endif diff --git a/Arduino/libraries/BH1750/LICENSE b/Arduino/libraries/BH1750/LICENSE new file mode 100644 index 00000000..604d8386 --- /dev/null +++ b/Arduino/libraries/BH1750/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 claws + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Arduino/libraries/BH1750/README.md b/Arduino/libraries/BH1750/README.md new file mode 100644 index 00000000..3ec43275 --- /dev/null +++ b/Arduino/libraries/BH1750/README.md @@ -0,0 +1,142 @@ +# BH1750 + +[![Build Status](https://travis-ci.org/claws/BH1750.svg?branch=master)](https://travis-ci.org/claws/BH1750)
+ +This package contains an Arduino library for digital light sensor breakout boards containing the +BH1750FVI IC. + +The BH1750 board uses I2C for communication which requires two pins to +communicate with the device. Configuring the I2C bus must be done in user code +(not library code). This approach has been adopted so it can be done once and +will better support the various options for different platforms. + +A common module containing the BH1750 component is the GY-30 shown below. + +![GY-30 Module image](resources/gy30-module.jpg) + + +## Overview + +The BH1750 has six different measurement modes which are divided in two groups; +continuous and one-time measurements. In continuous mode the sensor +continuously measures lightness value. In one-time mode, the sensor makes only +one measurement and then goes into Power Down mode. + +Each mode has three different precisions: + + - Low Resolution Mode - (4 lx precision, 16ms measurement time) + - High Resolution Mode - (1 lx precision, 120ms measurement time) + - High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time) + +By default, this library uses Continuous High Resolution Mode, but you can +change this to a different mode by passing the mode argument to +BH1750.begin(). + +When the One-Time mode is used your sensor will go into Power Down mode when +it completes the measurement and you've read it. When the sensor is powered up +again it returns to the default mode which means it needs to be reconfigured +back into One-Time mode. This library has been implemented to automatically +reconfigure the sensor when you next attempt a measurement so you should not +have to worry about such low level details. + +The datasheet for the BH1750 chip can be obtained [here](http://www.elechouse.com/elechouse/images/product/Digital%20light%20Sensor/bh1750fvi-e.pdf) + + +## Installation + +Click "Clone or download" -> "Download ZIP" button. + + - **(For Arduino >= 1.5.x)** Use the way above, or Library Manager. Open Arduino + IDE, click `Sketch -> Include library -> Add .ZIP library ` and select the + downloaded archive. + + - **(For Arduino < 1.5.x)** Extract the archive to + ``/My Documents/Arduino/libraries/`` folder and rename it + to `BH1750`. Restart IDE. + +The following YouTube [video](https://youtu.be/ACTMQvPVMLs) (specifically from +7:20 onwards) provides a good overview of installing this library and loading +an example using the Arduino IDE. + +[![BH1750 Video Tutorial](https://img.youtube.com/vi/ACTMQvPVMLs/0.jpg)](https://youtu.be/ACTMQvPVMLs?t=437) + +Additional info, about library installation process - https://www.arduino.cc/en/Guide/Libraries + + +## Example + +An example using the BH1750 library in conjunction with the GY-30 board +(which contains the BH1750 component) is presented below. The example +code uses the BH1750 library in the default continuous high precision +mode when making light measurements. + +### Wiring + +Connections: + + - VCC -> 3V3 or 5V + - GND -> GND + - SCL -> SCL (A5 on Arduino Nano, Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) + - SDA -> SDA (A4 on Arduino Nano, Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) + - ADD -> NC/GND or VCC (see below) + +The ADD pin is used to set the sensor I2C address. By default (if ADD voltage +less than 0.7 * VCC) the sensor address will be 0x23. If it has voltage +greater or equal to 0.7VCC voltage (e.g. you've connected it to VCC) the +sensor address will be 0x5C. + +Wiring up the GY-30 sensor board to an Arduino is shown in the diagram below. + +![Example wiring diagram image](resources/wiring-diagram-gy30-module.png) + +*The image above was created using [Fritzing](http://fritzing.org/home/) and +the GY-30 module was obtained from [here](http://omnigatherum.ca/wp/?p=6)*. + +### Code + +Upload the BH1750 test code to your Arduino. + +``` c++ +#include +#include + +BH1750 lightMeter; + +void setup(){ + + Serial.begin(9600); + + // Initialize the I2C bus (BH1750 library doesn't do this automatically) + // On esp8266 devices you can select SCL and SDA pins using Wire.begin(D4, D3); + Wire.begin(); + + lightMeter.begin(); + Serial.println(F("BH1750 Test")); + +} + +void loop() { + + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} +``` + +### Output + +Moving the sensor to face more light results in the lux measurements increasing. +``` +BH1750 Test +Light: 70 lx +Light: 70 lx +Light: 59 lx +Light: 328 lx +Light: 333 lx +Light: 335 lx +Light: 332 lx +``` +There are more examples in the examples directory. diff --git a/Arduino/libraries/BH1750/build-examples.bash b/Arduino/libraries/BH1750/build-examples.bash new file mode 100644 index 00000000..5ec70f09 --- /dev/null +++ b/Arduino/libraries/BH1750/build-examples.bash @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# +# A simple script to automate building BH1750 examples. +# +# Example (MacOSX): +# $ ARDUINO_IDE_PATH=/Applications/Arduino.app/Contents/Java ./build-examples.bash +# + +# Path to script directory. +SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )" +LIBNAME="$(basename "$SCRIPTPATH")" + +if [[ -z "${ARDUINO_IDE_PATH}" ]]; then + echo "ARDUINO_IDE_PATH env var is not set" + exit 1 +fi + +# Link BH1750 library into Arduino libraries directory +ln -s $SCRIPTPATH $ARDUINO_IDE_PATH/libraries/ + +cd $ARDUINO_IDE_PATH + +for sketch in `find $SCRIPTPATH/examples -name '*.ino'` +do + echo "Compiling $sketch" + ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn arduino:avr:uno $sketch + # ./arduino-builder -hardware ./hardware -tools ./hardware/tools/avr -tools ./tools-builder -libraries ./libraries -fqbn esp8266:esp8266:generic $sketch +done + +# Unlink BH1750 library from Arduino libraries directory +unlink $ARDUINO_IDE_PATH/libraries/$LIBNAME diff --git a/Arduino/libraries/BH1750/component.mk b/Arduino/libraries/BH1750/component.mk new file mode 100644 index 00000000..27ad11a7 --- /dev/null +++ b/Arduino/libraries/BH1750/component.mk @@ -0,0 +1 @@ +COMPONENT_ADD_INCLUDEDIRS := . diff --git a/Arduino/libraries/BH1750/examples/BH1750advanced/BH1750advanced.ino b/Arduino/libraries/BH1750/examples/BH1750advanced/BH1750advanced.ino new file mode 100644 index 00000000..811ee753 --- /dev/null +++ b/Arduino/libraries/BH1750/examples/BH1750advanced/BH1750advanced.ino @@ -0,0 +1,96 @@ +/* + + Advanced BH1750 library usage example + + This example has some comments about advanced usage features. + + Connection: + + VCC -> 3V3 or 5V + GND -> GND + SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) + SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) + ADD -> (not connected) or GND + + ADD pin is used to set sensor I2C address. If it has voltage greater or equal to + 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be + 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will + be 0x23 (by default). + +*/ + +#include +#include + +/* + + BH1750 can be physically configured to use two I2C addresses: + - 0x23 (most common) (if ADD pin had < 0.7VCC voltage) + - 0x5C (if ADD pin had > 0.7VCC voltage) + + Library uses 0x23 address as default, but you can define any other address. + If you had troubles with default value - try to change it to 0x5C. + +*/ +BH1750 lightMeter(0x23); + +void setup(){ + + Serial.begin(9600); + + // Initialize the I2C bus (BH1750 library doesn't do this automatically) + Wire.begin(); + // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); + + /* + + BH1750 has six different measurement modes. They are divided in two groups; + continuous and one-time measurements. In continuous mode, sensor continuously + measures lightness value. In one-time mode the sensor makes only one + measurement and then goes into Power Down mode. + + Each mode, has three different precisions: + + - Low Resolution Mode - (4 lx precision, 16ms measurement time) + - High Resolution Mode - (1 lx precision, 120ms measurement time) + - High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time) + + By default, the library uses Continuous High Resolution Mode, but you can + set any other mode, by passing it to BH1750.begin() or BH1750.configure() + functions. + + [!] Remember, if you use One-Time mode, your sensor will go to Power Down + mode each time, when it completes a measurement and you've read it. + + Full mode list: + + BH1750_CONTINUOUS_LOW_RES_MODE + BH1750_CONTINUOUS_HIGH_RES_MODE (default) + BH1750_CONTINUOUS_HIGH_RES_MODE_2 + + BH1750_ONE_TIME_LOW_RES_MODE + BH1750_ONE_TIME_HIGH_RES_MODE + BH1750_ONE_TIME_HIGH_RES_MODE_2 + + */ + + // begin returns a boolean that can be used to detect setup problems. + if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { + Serial.println(F("BH1750 Advanced begin")); + } + else { + Serial.println(F("Error initialising BH1750")); + } + +} + + +void loop() { + + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} diff --git a/Arduino/libraries/BH1750/examples/BH1750onetime/BH1750onetime.ino b/Arduino/libraries/BH1750/examples/BH1750onetime/BH1750onetime.ino new file mode 100644 index 00000000..116f5d8e --- /dev/null +++ b/Arduino/libraries/BH1750/examples/BH1750onetime/BH1750onetime.ino @@ -0,0 +1,42 @@ +/* + + Example of BH1750 library usage. + + This example initialises the BH1750 object using the high resolution + one-time mode and then makes a light level reading every second. + + The BH1750 component starts up in default mode when it next powers up. + The BH1750 library automatically reconfigures the one-time mode in + preparation for the next measurement. + +*/ + +#include +#include + +BH1750 lightMeter; + +void setup(){ + + Serial.begin(9600); + + // Initialize the I2C bus (BH1750 library doesn't do this automatically) + Wire.begin(); + // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); + + lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE); + + Serial.println(F("BH1750 One-Time Test")); + +} + + +void loop() { + + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} diff --git a/Arduino/libraries/BH1750/examples/BH1750test/BH1750test.ino b/Arduino/libraries/BH1750/examples/BH1750test/BH1750test.ino new file mode 100644 index 00000000..a47e99e4 --- /dev/null +++ b/Arduino/libraries/BH1750/examples/BH1750test/BH1750test.ino @@ -0,0 +1,52 @@ +/* + + Example of BH1750 library usage. + + This example initialises the BH1750 object using the default high resolution + continuous mode and then makes a light level reading every second. + + Connection: + + VCC -> 3V3 or 5V + GND -> GND + SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable) + SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable) + ADD -> (not connected) or GND + + ADD pin is used to set sensor I2C address. If it has voltage greater or equal to + 0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be + 0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will + be 0x23 (by default). + +*/ + +#include +#include + +BH1750 lightMeter; + + +void setup(){ + + Serial.begin(9600); + + // Initialize the I2C bus (BH1750 library doesn't do this automatically) + Wire.begin(); + // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3); + + lightMeter.begin(); + + Serial.println(F("BH1750 Test begin")); + +} + + +void loop() { + + uint16_t lux = lightMeter.readLightLevel(); + Serial.print("Light: "); + Serial.print(lux); + Serial.println(" lx"); + delay(1000); + +} diff --git a/Arduino/libraries/BH1750/keywords.txt b/Arduino/libraries/BH1750/keywords.txt new file mode 100644 index 00000000..2813d176 --- /dev/null +++ b/Arduino/libraries/BH1750/keywords.txt @@ -0,0 +1,33 @@ +####################################### +# Syntax Coloring Map For BH1750 +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +BH1750 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +begin KEYWORD2 +configure KEYWORD2 +readLightLevel KEYWORD2 + + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### +BH1750_CONTINUOUS_HIGH_RES_MODE LITERAL1 +BH1750_CONTINUOUS_HIGH_RES_MODE_2 LITERAL1 +BH1750_CONTINUOUS_LOW_RES_MODE LITERAL1 +BH1750_ONE_TIME_HIGH_RES_MODE LITERAL1 +BH1750_ONE_TIME_HIGH_RES_MODE_2 LITERAL1 +BH1750_ONE_TIME_LOW_RES_MODE LITERAL1 diff --git a/Arduino/libraries/BH1750/library.json b/Arduino/libraries/BH1750/library.json new file mode 100644 index 00000000..aba1d2e9 --- /dev/null +++ b/Arduino/libraries/BH1750/library.json @@ -0,0 +1,11 @@ +{ + "name": "BH1750", + "keywords": "light, sensor", + "description": "Digital light sensor breakout boards containing the BH1750FVI IC", + "repository": { + "type": "git", + "url": "https://github.com/claws/BH1750.git" + }, + "frameworks": "arduino", + "platforms": ["atmelavr", "atmelsam", "espressif8266"] +} diff --git a/Arduino/libraries/BH1750/library.properties b/Arduino/libraries/BH1750/library.properties new file mode 100644 index 00000000..ef0d1405 --- /dev/null +++ b/Arduino/libraries/BH1750/library.properties @@ -0,0 +1,10 @@ +name=BH1750 +version=1.1.3 +author=Christopher Laws +maintainer=Christopher Laws +sentence=Digital light sensor breakout boards containing the BH1750FVI IC +paragraph=Pretty simple and robust BH1750 library +category=Sensors +url=https://github.com/claws/BH1750 +architectures=* +includes=BH1750.h diff --git a/Arduino/libraries/BH1750/resources/gy30-module.jpg b/Arduino/libraries/BH1750/resources/gy30-module.jpg new file mode 100644 index 00000000..15cf0995 Binary files /dev/null and b/Arduino/libraries/BH1750/resources/gy30-module.jpg differ diff --git a/Arduino/libraries/BH1750/resources/wiring-diagram-gy30-module.png b/Arduino/libraries/BH1750/resources/wiring-diagram-gy30-module.png new file mode 100644 index 00000000..f8efe5cc Binary files /dev/null and b/Arduino/libraries/BH1750/resources/wiring-diagram-gy30-module.png differ diff --git a/Arduino/libraries/Max44009/Max44009.h b/Arduino/libraries/Max44009/Max44009.h new file mode 100644 index 00000000..e240efde --- /dev/null +++ b/Arduino/libraries/Max44009/Max44009.h @@ -0,0 +1,98 @@ +#ifndef MAX44009_H +#define MAX44009_H +// +// FILE: Max44009.h +// AUTHOR: Rob dot Tillaart at gmail dot com +// VERSION: 0.1.9 +// PURPOSE: library for MAX44009 lux sensor Arduino +// HISTORY: See Max440099.cpp +// +// Released to the public domain +// + +#include "Wire.h" + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#define MAX44009_LIB_VERSION "0.1.9" + +// I2C ADDRESSES +#define MAX44009_A0_LOW 0x4A +#define MAX44009_A0_HIGH 0x4B + +// REGISTERS +#define MAX44009_INTERRUPT_STATUS 0x00 +#define MAX44009_INTERRUPT_ENABLE 0x01 +#define MAX44009_CONFIGURATION 0x02 +#define MAX44009_LUX_READING_HIGH 0x03 +#define MAX44009_LUX_READING_LOW 0x04 +#define MAX44009_THRESHOLD_HIGH 0x05 +#define MAX44009_THRESHOLD_LOW 0x06 +#define MAX44009_THRESHOLD_TIMER 0x07 + +// CONFIGURATION MASKS +#define MAX44009_CFG_CONTINUOUS 0x80 +#define MAX44009_CFG_MANUAL 0x40 +#define MAX44009_CFG_CDR 0x08 +#define MAX44009_CFG_TIMER 0x07 + + +class Max44009 +{ +public: + // dataPin and clockPin are only used by ESP8266 + // for UNO ignore these (and its warning) + Max44009(const uint8_t address, const uint8_t dataPin = 5, const uint8_t clockPin = 4); + Max44009(const uint8_t address, bool continuousMode); + + float getLux(); + int getError(); + + void setHighThreshold(const float); + float getHighThreshold(void); + void setLowThreshold(const float); + float getLowThreshold(void); + void setThresholdTimer(const uint8_t); + uint8_t getThresholdTimer(); + + void enableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 1); }; + void disableInterrupt() { write(MAX44009_INTERRUPT_ENABLE, 0); }; + bool interruptEnabled() { return read(MAX44009_INTERRUPT_ENABLE) & 0x01; }; + uint8_t getInterruptStatus() { return read(MAX44009_INTERRUPT_STATUS) & 0x01; }; + + // check datasheet for detailed behavior + void setConfiguration(uint8_t); + uint8_t getConfiguration(); + void setAutomaticMode(); + void setContinuousMode(); + // CDR = Current Divisor Ratio + // CDR = 1 ==> only 1/8th is measured + // TIM = Time Integration Measurement (table) + // 000 800ms + // 001 400ms + // 010 200ms + // 011 100ms + // 100 50ms + // 101 25ms + // 110 12.5ms + // 111 6.25ms + void setManualMode(uint8_t CDR, uint8_t TIM); + +private: + void setThreshold(uint8_t, float); + float getThreshold(uint8_t); + + uint8_t read(uint8_t reg); + void write(uint8_t, uint8_t); + + uint8_t _address; + uint8_t _data; + int _error; +}; +#endif + +// END OF FILE \ No newline at end of file diff --git a/Arduino/libraries/Max44009/examples/max44009_test01/max44009_test01.ino b/Arduino/libraries/Max44009/examples/max44009_test01/max44009_test01.ino new file mode 100644 index 00000000..ecdc808a --- /dev/null +++ b/Arduino/libraries/Max44009/examples/max44009_test01/max44009_test01.ino @@ -0,0 +1,47 @@ +// +// FILE: max44009_test01.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.1 +// PURPOSE: demo of max44009 library +// DATE: 2015-08-06 +// URL: ? +// +// Released to the public domain +// + +#include "Wire.h" +#include "Max44009.h" + +Max44009 myLux(0xCB); // default addr + +uint32_t lastDisplay = 0; + +void setup() +{ + Serial.begin(115200); + Serial.print("Start max44009_test01 : "); + Serial.println(MAX44009_LIB_VERSION); +} + +void loop() +{ + if (millis() - lastDisplay >= 1000) + { + lastDisplay += 1000; + float lux = myLux.getLux(); + int err = myLux.getError(); + if (err != 0) + { + Serial.print("Error:\t"); + Serial.println(err); + } + else + { + Serial.print("lux:\t"); + Serial.println(lux); + } + } + +} + + diff --git a/Arduino/libraries/Max44009/keywords.txt b/Arduino/libraries/Max44009/keywords.txt new file mode 100644 index 00000000..c4449fc9 --- /dev/null +++ b/Arduino/libraries/Max44009/keywords.txt @@ -0,0 +1,36 @@ +####################################### +# Syntax Coloring Map For Max44009 +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### + +Max44009 KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +getLux KEYWORD2 +getError KEYWORD2 +setHighThreshold KEYWORD2 +getHighThreshold KEYWORD2 +setLowThreshold KEYWORD2 +getLowThreshold KEYWORD2 +setThresholdTimer KEYWORD2 +getThresholdTimer KEYWORD2 +setConfiguration KEYWORD2 +getConfiguration KEYWORD2 +setAutomaticMode KEYWORD2 +setContinuousMode KEYWORD2 +setManualMode KEYWORD2 + +####################################### +# Instances (KEYWORD2) +####################################### + + +####################################### +# Constants (LITERAL1) +####################################### diff --git a/Arduino/libraries/Max44009/library.json b/Arduino/libraries/Max44009/library.json new file mode 100644 index 00000000..0f10a756 --- /dev/null +++ b/Arduino/libraries/Max44009/library.json @@ -0,0 +1,24 @@ +{ + "name": "Max44009", + "keywords": "MAX44009,Lux,light", + "description": "library for MAX44009 Lux sensor Arduino.", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/Arduino.git" + }, + "version":"0.1.9", + "frameworks": "arduino", + "platforms": "*", + "export": { + "include": "libraries/Max44009" + } +} diff --git a/Arduino/libraries/Max44009/library.properties b/Arduino/libraries/Max44009/library.properties new file mode 100644 index 00000000..8899bd71 --- /dev/null +++ b/Arduino/libraries/Max44009/library.properties @@ -0,0 +1,9 @@ +name=Max44009 +version=0.1.9 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Library for MAX44009 lux sensor Arduino. +paragraph= +category=Sensors +url=https://github.com/RobTillaart/Arduino/tree/master/libraries/ +architectures=* \ No newline at end of file diff --git a/Arduino/libraries/Max44009/max44009.cpp b/Arduino/libraries/Max44009/max44009.cpp new file mode 100644 index 00000000..b386ed07 --- /dev/null +++ b/Arduino/libraries/Max44009/max44009.cpp @@ -0,0 +1,201 @@ +// +// FILE: Max44009.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.9 +// PURPOSE: library for MAX44009 lux sensor Arduino +// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries +// +// Released to the public domain +// +// 0.1.9 - 2018-07-01 issue #108 Fix shift math +// (thanks Roland vandecook) +// 0.1.8 - 2018-05-13 issue #105 Fix read register +// (thanks morxGrillmeister) +// 0.1.7 - 2018-04-02 issue #98 extend constructor for ESP8266 +// 0.1.6 - 2017-07-26 revert double to float +// 0.1.5 - updated history +// 0.1.4 - added setAutomaticMode() to max44009.h (thanks debsahu) +// 0.1.03 - added configuration +// 0.1.02 - added threshold code +// 0.1.01 - added interrupt code +// 0.1.00 - initial version + +#include "Max44009.h" + + +Max44009::Max44009(const uint8_t address, const uint8_t dataPin, const uint8_t clockPin) +{ + _address = address; +#ifdef ESP8266 + Wire.begin(dataPin, clockPin); +#else // other platforms + Wire.begin(); +#endif + // TWBR = 12; // Wire.setClock(400000); + _data = 0; + _error = 0; +} + +Max44009::Max44009(const uint8_t address, bool continuousMode) +{ + _address = address; + Wire.begin(); + // TWBR = 12; // Wire.setClock(400000); + _data = 0; + _error = 0; + if (continuousMode) + { + setContinuousMode(); + } + else + { + setAutomaticMode(); + } +} + +float Max44009::getLux(void) +{ + uint8_t dhi = read(MAX44009_LUX_READING_HIGH); + uint8_t dlo = read(MAX44009_LUX_READING_LOW); + uint8_t e = dhi >> 4; + uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F); + m <<= e; + float val = m * 0.045; + return val; +} + +int Max44009::getError() +{ + int e = _error; + _error = 0; + return e; +} + +void Max44009::setHighThreshold(const float value) +{ + setThreshold(MAX44009_THRESHOLD_HIGH, value); +} + +float Max44009::getHighThreshold(void) +{ + return getThreshold(MAX44009_THRESHOLD_HIGH); +} + +void Max44009::setLowThreshold(const float value) +{ + setThreshold(MAX44009_THRESHOLD_LOW, value); +} + +float Max44009::getLowThreshold(void) +{ + return getThreshold(MAX44009_THRESHOLD_LOW); +} + +void Max44009::setThresholdTimer(const uint8_t value) +{ + write(MAX44009_THRESHOLD_TIMER, value); +} + +uint8_t Max44009::getThresholdTimer() +{ + return read(MAX44009_THRESHOLD_TIMER); +} + +void Max44009::setConfiguration(const uint8_t value) +{ + write(MAX44009_CONFIGURATION, value); +} + +uint8_t Max44009::getConfiguration() +{ + return read(MAX44009_CONFIGURATION); +} + +void Max44009::setAutomaticMode() +{ + uint8_t config = read(MAX44009_CONFIGURATION); + config &= ~MAX44009_CFG_CONTINUOUS; // off + config &= ~MAX44009_CFG_MANUAL; // off + write(MAX44009_CONFIGURATION, config); +} + +void Max44009::setContinuousMode() +{ + uint8_t config = read(MAX44009_CONFIGURATION); + config |= MAX44009_CFG_CONTINUOUS; // on + config &= ~MAX44009_CFG_MANUAL; // off + write(MAX44009_CONFIGURATION, config); +} + +void Max44009::setManualMode(uint8_t CDR, uint8_t TIM) +{ + if (CDR !=0) CDR = 1; + if (TIM > 7) TIM = 7; + uint8_t config = read(MAX44009_CONFIGURATION); + config &= ~MAX44009_CFG_CONTINUOUS; // off + config |= MAX44009_CFG_MANUAL; // on + config &= 0xF0; // clear CDR & TIM bits + config |= CDR << 3 | TIM; + write(MAX44009_CONFIGURATION, config); +} + +/////////////////////////////////////////////////////////// +// +// PRIVATE +// +void Max44009::setThreshold(const uint8_t reg, const float value) +{ + // TODO CHECK RANGE + uint32_t m = round(value / 0.045); // mulitply * 22.22222222 is faster. + uint8_t e = 0; + while (m > 255) + { + m >>= 1; + e++; + }; + m = (m >> 4) & 0x0F; + e <<= 4; + write(reg, e | m); +} + +float Max44009::getThreshold(uint8_t reg) +{ + uint8_t data = read(reg); + uint8_t e = (data & 0xF0) >> 4; + uint32_t m = ((data & 0x0F) << 4) + 0x0F; + m <<= e; + float val = m * 0.045; + return val; +} + +uint8_t Max44009::read(uint8_t reg) +{ + Wire.beginTransmission(_address); + Wire.write(reg); + _error = Wire.endTransmission(); + if (_error != 0) + { + return _data; // last value + } + if (Wire.requestFrom(_address, (uint8_t) 1) != 1) + { + _error = 10; + return _data; // last value + } +#if (ARDUINO < 100) + _data = Wire.receive(); +#else + _data = Wire.read(); +#endif + return _data; +} + +void Max44009::write(uint8_t reg, uint8_t value) +{ + Wire.beginTransmission(_address); + Wire.write(reg); + Wire.write(value); + _error = Wire.endTransmission(); +} + +// --- END OF FILE --- \ No newline at end of file diff --git a/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.cpp b/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.cpp index d5047f8e..77979ac0 100644 --- a/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.cpp +++ b/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.cpp @@ -95,7 +95,6 @@ namespace st } void PS_AdafruitTCS34725_Illum_Color::init() { - firstTime = true; Serial.println("Initializing the TCS34725 sensor..."); if (tcs.begin()) { Serial.println("Found sensor. tcs.begin = true"); @@ -103,6 +102,8 @@ namespace st else { Serial.println("No TCS34725 found... check your connections"); } + + delay(1000); //give sensor time to get its first data sample ready to be read getData(); } @@ -123,11 +124,9 @@ namespace st String strSensorValue = String(m_nlux, DEC) + ':' + String(m_ncolorTemp, DEC) + ':' + String(m_nred, DEC) + ':' + String(m_ngreen, DEC) + ':' + String(m_nblue, DEC) + ':' + String(m_nclear, DEC); - //Sensor data is invalid for first measurement, therefore do not send it to SmartThings/Hubitat - if (!firstTime) { - Everything::sendSmartString(getName() + " " + strSensorValue); - } - firstTime = false; + //Send data to SmartThings/Hubitat + Everything::sendSmartString(getName() + " " + String(m_nlux)); + } } diff --git a/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.h b/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.h index e7126eac..9659dba7 100644 --- a/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.h +++ b/Arduino/libraries/ST_Anything_AdafruitTCS34725_Illum_Color/PS_AdafruitTCS34725_Illum_Color.h @@ -66,7 +66,6 @@ namespace st uint16_t m_ngreen; //green value uint16_t m_nblue; //blue value uint16_t m_nclear; //clear value - bool firstTime; //First reading is not good, so don't send it public: //constructor - called in your sketch's global variable declaration section diff --git a/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.cpp b/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.cpp new file mode 100644 index 00000000..aba9fe22 --- /dev/null +++ b/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.cpp @@ -0,0 +1,108 @@ +//****************************************************************************************** +// File: PS_BH1750_Illuminance.cpp +// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son) +// +// Summary: PS_BH1750_Illuminance is a class which implements the SmartThings "Illuminance Measurement" device capability. +// It inherits from the st::PollingSensor class. It uses I2C communication to measure the Illuminace from a MAX44009 sensor. +// +// Create an instance of this class in your sketch's global variable section +// For Example: st::PS_BH1750_Illuminance sensor1(F("illuminance1"), 60, 0); (simple, uses defaults) +// For Example: st::PS_BH1750_Illuminance sensor1(F("illuminance1"), 60, 0, BH1750_ADDR_LOW); (full user control of settings) +// +// st::PS_BH1750_Illuminance() constructor requires the following arguments +// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name +// - long interval - REQUIRED - the polling interval in seconds +// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time +// - uint8_t addr -OPTIONAL - defaults to BH1750_ADDR_LOW +// +// +// I2C address options +// BH1750_ADDR_LOW 0x23 //< Pin A0 pulled Low +// BH1750_ADDR_HIGH 0x5C //< Pin A0 pulled Hi +// +// +// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference +// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to +// the ST Shield. For PollingSensors, this data is handled in the beSMart() function. +// +// TODO: Determine a method to persist the ST Cloud's Polling Interval data +// +// Change History: +// +// Date Who What +// ---- --- ---- +// 2018-07-03 Dan Ogorchock Original Creation +// +// +//****************************************************************************************** + +#include "PS_BH1750_Illuminance.h" + +#include "Constants.h" +#include "Everything.h" + +namespace st +{ +//private + +//public + //constructor - called in your sketch's global variable declaration section + PS_BH1750_Illuminance::PS_BH1750_Illuminance(const __FlashStringHelper *name, unsigned int interval, int offset, uint8_t addr) : + PollingSensor(name, interval, offset), + myLux(addr) + { + + } + + //destructor + PS_BH1750_Illuminance::~PS_BH1750_Illuminance() + { + + } + + //SmartThings Shield data handler (receives configuration data from ST - polling interval, and adjusts on the fly) + void PS_BH1750_Illuminance::beSmart(const String &str) + { + String s = str.substring(str.indexOf(' ') + 1); + + if (s.toInt() != 0) { + st::PollingSensor::setInterval(s.toInt() * 1000); + if (st::PollingSensor::debug) { + Serial.print(F("PS_BH1750_Illuminance::beSmart set polling interval to ")); + Serial.println(s.toInt()); + } + } + else { + if (st::PollingSensor::debug) + { + Serial.print(F("PS_BH1750_Illuminance::beSmart cannot convert ")); + Serial.print(s); + Serial.println(F(" to an Integer.")); + } + } + } + + void PS_BH1750_Illuminance::init() { + + //initialize the BH1750 + myLux.begin(); + + //give sensor time to get its first data sample ready to be read + delay(1000); + + //read and transmit initial data from sensor + getData(); + } + + //function to get data from sensor and queue results for transfer to ST Cloud + void PS_BH1750_Illuminance::getData() + { + /* Get a new sensor data */ + m_nLux = myLux.readLightLevel(); + + //Send data to SmartThings/Hubitat + Everything::sendSmartString(getName() + " " + String(m_nLux)); + + } + +} diff --git a/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.h b/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.h new file mode 100644 index 00000000..3f0601f9 --- /dev/null +++ b/Arduino/libraries/ST_Anything_BH1750_Illuminance/PS_BH1750_Illuminance.h @@ -0,0 +1,75 @@ +//****************************************************************************************** +// File: PS_BH1750_Illuminance.h +// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son) +// +// Summary: PS_BH1750_Illuminance is a class which implements the SmartThings "Illuminance Measurement" device capability. +// It inherits from the st::PollingSensor class. It uses I2C communication to measure the Illuminace from a MAX44009 sensor. +// +// Create an instance of this class in your sketch's global variable section +// For Example: st::PS_BH1750_Illuminance sensor1(F("illuminance1"), 60, 0); (simple, uses defaults) +// For Example: st::PS_BH1750_Illuminance sensor1(F("illuminance1"), 60, 0, BH1750_ADDR_LOW); (full user control of settings) +// +// st::PS_BH1750_Illuminance() constructor requires the following arguments +// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name +// - long interval - REQUIRED - the polling interval in seconds +// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time +// - uint8_t addr -OPTIONAL - defaults to BH1750_ADDR_LOW +// +// +// I2C address options +// BH1750_ADDR_LOW 0x23 //< Pin A0 pulled Low +// BH1750_ADDR_HIGH 0x5C //< Pin A0 pulled Hi +// +// +// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference +// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to +// the ST Shield. For PollingSensors, this data is handled in the beSMart() function. +// +// TODO: Determine a method to persist the ST Cloud's Polling Interval data +// +// Change History: +// +// Date Who What +// ---- --- ---- +// 2018-07-03 Dan Ogorchock Original Creation +// +// +//****************************************************************************************** + +#ifndef ST_PS_BH1750_Illuminance_H +#define ST_PS_BH1750_Illuminance_H + +#include "PollingSensor.h" +#include "BH1750.h" + +namespace st +{ + class PS_BH1750_Illuminance: public PollingSensor + { + private: + BH1750 myLux; //BH1750 object + uint16_t m_nLux; //lux + + public: + //constructor - called in your sketch's global variable declaration section + PS_BH1750_Illuminance(const __FlashStringHelper *name, unsigned int interval, int offset, uint8_t addr = BH1750_ADDR_LOW); + + //destructor + virtual ~PS_BH1750_Illuminance(); + + //SmartThings Shield data handler (receives configuration data from ST - polling interval, and adjusts on the fly) + virtual void beSmart(const String &str); + + //initialization routine + virtual void init(); + + //function to get data from sensor and queue results for transfer to ST Cloud + virtual void getData(); + + //gets + + //sets + + }; +} +#endif diff --git a/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.cpp b/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.cpp new file mode 100644 index 00000000..6e8817f9 --- /dev/null +++ b/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.cpp @@ -0,0 +1,117 @@ +//****************************************************************************************** +// File: PS_MAX44009_Illuminance.cpp +// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son) +// +// Summary: PS_MAX44009_Illuminance is a class which implements the SmartThings "Illuminance Measurement" device capability. +// It inherits from the st::PollingSensor class. It uses I2C communication to measure the Illuminace from a MAX44009 sensor. +// +// Create an instance of this class in your sketch's global variable section +// For Example: st::PS_MAX44009_Illuminance sensor1(F("illuminance1"), 60, 0); (simple, uses defaults) +// For Example: st::PS_MAX44009_Illuminance sensor1(F("illuminance1"), 60, 0, MAX44009_A0_LOW); (full user control of settings) +// +// st::PS_MAX44009_Illuminance() constructor requires the following arguments +// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name +// - long interval - REQUIRED - the polling interval in seconds +// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time +// - uint8_t addr -OPTIONAL - defaults to MAX44009_A0_LOW +// +// +// I2C address options +// MAX44009_A0_LOW 0x4A //< Pin A0 pulled Low +// MAX44009_A0_HIGH 0x4B //< Pin A0 pulled Hi +// +// +// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference +// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to +// the ST Shield. For PollingSensors, this data is handled in the beSMart() function. +// +// TODO: Determine a method to persist the ST Cloud's Polling Interval data +// +// Change History: +// +// Date Who What +// ---- --- ---- +// 2018-07-03 Dan Ogorchock Original Creation +// +// +//****************************************************************************************** + +#include "PS_MAX44009_Illuminance.h" + +#include "Constants.h" +#include "Everything.h" + +namespace st +{ +//private + +//public + //constructor - called in your sketch's global variable declaration section + PS_MAX44009_Illuminance::PS_MAX44009_Illuminance(const __FlashStringHelper *name, unsigned int interval, int offset, uint8_t addr) : + PollingSensor(name, interval, offset), + myLux(addr, false) + { + + } + + //destructor + PS_MAX44009_Illuminance::~PS_MAX44009_Illuminance() + { + + } + + //SmartThings Shield data handler (receives configuration data from ST - polling interval, and adjusts on the fly) + void PS_MAX44009_Illuminance::beSmart(const String &str) + { + String s = str.substring(str.indexOf(' ') + 1); + + if (s.toInt() != 0) { + st::PollingSensor::setInterval(s.toInt() * 1000); + if (st::PollingSensor::debug) { + Serial.print(F("PS_MAX44009_Illuminance::beSmart set polling interval to ")); + Serial.println(s.toInt()); + } + } + else { + if (st::PollingSensor::debug) + { + Serial.print(F("PS_MAX44009_Illuminance::beSmart cannot convert ")); + Serial.print(s); + Serial.println(F(" to an Integer.")); + } + } + } + + void PS_MAX44009_Illuminance::init() { + + //Set mode of the MAX44009 to its default state + // From MAX44009 Datasheet: "Default mode. The IC measures lux intensity only + // once every 800ms regardless of integration time. This mode allows the part // to operate at its lowest possible supply current. myLux.setAutomaticMode(); + + //read and transmit initial data from sensor + getData(); + } + + //function to get data from sensor and queue results for transfer to ST Cloud + void PS_MAX44009_Illuminance::getData() + { + /* Get a new sensor data */ + m_fLux = myLux.getLux(); + int err = myLux.getError(); + if (err != 0) + { + if (st::PollingSensor::debug) + { + Serial.print(F("MAX44009 sensor Error = ")); + Serial.println(err); + Serial.println(F("Check your wiring and I2C address.")); + } + } + else + { + //send data to SmartThings/Hubitat + Everything::sendSmartString(getName() + " " + String(m_fLux)); + } + } + +} diff --git a/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.h b/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.h new file mode 100644 index 00000000..3347c084 --- /dev/null +++ b/Arduino/libraries/ST_Anything_MAX44009_Illuminance/PS_MAX44009_Illuminance.h @@ -0,0 +1,75 @@ +//****************************************************************************************** +// File: PS_MAX44009_Illuminance.h +// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son) +// +// Summary: PS_MAX44009_Illuminance is a class which implements the SmartThings "Illuminance Measurement" device capability. +// It inherits from the st::PollingSensor class. It uses I2C communication to measure the Illuminace from a MAX44009 sensor. +// +// Create an instance of this class in your sketch's global variable section +// For Example: st::PS_MAX44009_Illuminance sensor1(F("illuminance1"), 60, 0); (simple, uses defaults) +// For Example: st::PS_MAX44009_Illuminance sensor1(F("illuminance1"), 60, 0, MAX44009_A0_LOW); (full user control of settings) +// +// st::PS_MAX44009_Illuminance() constructor requires the following arguments +// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name +// - long interval - REQUIRED - the polling interval in seconds +// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time +// - uint8_t addr -OPTIONAL - defaults to MAX44009_A0_LOW +// +// +// I2C address options +// MAX44009_A0_LOW 0x4A //< Pin A0 pulled Low +// MAX44009_A0_HIGH 0x4B //< Pin A0 pulled Hi +// +// +// This class supports receiving configuration data from the SmartThings cloud via the ST App. A user preference +// can be configured in your phone's ST App, and then the "Configure" tile will send the data for all sensors to +// the ST Shield. For PollingSensors, this data is handled in the beSMart() function. +// +// TODO: Determine a method to persist the ST Cloud's Polling Interval data +// +// Change History: +// +// Date Who What +// ---- --- ---- +// 2018-07-03 Dan Ogorchock Original Creation +// +// +//****************************************************************************************** + +#ifndef ST_PS_MAX44009_Illuminance_H +#define ST_PS_MAX44009_Illuminance_H + +#include "PollingSensor.h" +#include "Max44009.h" + +namespace st +{ + class PS_MAX44009_Illuminance: public PollingSensor + { + private: + Max44009 myLux; //MAX44009 object + float m_fLux; //lux + + public: + //constructor - called in your sketch's global variable declaration section + PS_MAX44009_Illuminance(const __FlashStringHelper *name, unsigned int interval, int offset, uint8_t addr = MAX44009_A0_LOW); + + //destructor + virtual ~PS_MAX44009_Illuminance(); + + //SmartThings Shield data handler (receives configuration data from ST - polling interval, and adjusts on the fly) + virtual void beSmart(const String &str); + + //initialization routine + virtual void init(); + + //function to get data from sensor and queue results for transfer to ST Cloud + virtual void getData(); + + //gets + + //sets + + }; +} +#endif