-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to restore the WiFi connection after it was interrupted by the ro…
…uter
- Loading branch information
1 parent
0849380
commit b012e09
Showing
6 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "BH1750.h" | ||
|
||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
hp_BH1750::hp_BH1750(TwoWire *myWire){ // constructor | ||
_wire = myWire; | ||
m_sensitivity = SENSITIVITY_ADJ_DEFAULT; | ||
m_measurementTime = 120; | ||
m_resolution = 1.0; | ||
} | ||
hp_BH1750::~hp_BH1750(){ // destructor | ||
_wire->end(); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::setSensitivity(uint8_t sensitivity){ | ||
if(sensitivity < SENSITIVITY_ADJ_MIN) return false; | ||
if(sensitivity > SENSITIVITY_ADJ_MAX) return false; | ||
m_sensitivity = sensitivity; | ||
return writeMtreg(m_sensitivity); // Set standard sensitivity | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::setResolutionMode(uint8_t resolutionMode){ | ||
if(resolutionMode == ONE_TIME_H_RESOLUTION_MODE) { m_resolution = 1.0; m_measurementTime = 120; m_resolutionMode = resolutionMode; return true;} | ||
if(resolutionMode == ONE_TIME_H_RESOLUTION_MODE2){ m_resolution = 0.5; m_measurementTime = 120; m_resolutionMode = resolutionMode; return true;} | ||
if(resolutionMode == ONE_TIME_L_RESOLUTION_MODE) { m_resolution = 4.0; m_measurementTime = 16; m_resolutionMode = resolutionMode; return true;} | ||
return false; | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::begin(uint8_t address, int8_t sda, int8_t scl) { | ||
_address = address; // Store one of the two available addresses | ||
_wire->begin(sda, scl, 400000); // Initialisation of wire object with standard SDA/SCL lines | ||
return writeMtreg(BH1750_MTREG_DEFAULT); // Set standard sensitivity | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::powerOn(){ | ||
return writeByte(0x1); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
// For low power consuming. This mode is entered automatically if a measurement is finished | ||
bool hp_BH1750::powerOff() { | ||
return writeByte(0x0); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::reset() { | ||
if(powerOn() == false) return false; | ||
return writeByte(0x7); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::writeByte(uint8_t b){ // Sends command to sensor | ||
_wire->beginTransmission(_address); | ||
_wire->write(b); | ||
return (_wire->endTransmission() == 0); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
int32_t hp_BH1750::readValue() { | ||
uint8_t buff[2]; | ||
uint16_t req = _wire->requestFrom((int)_address, (int)2); // request two bytes | ||
if(req < 2 || _wire->available() < 2) { | ||
return -1; // Sensor not ready | ||
} | ||
buff[0] = _wire->read(); // Receive one byte | ||
buff[1] = _wire->read(); // Receive one byte | ||
return ((buff[0] << 8) | buff[1]); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::writeMtreg(uint8_t mtreg) { // Change sensitivity measurement time | ||
uint8_t hiByte = mtreg >> 5; | ||
hiByte |= 0b01000000;// High bit: 01000_MT[7,6,5] | ||
writeByte(hiByte); | ||
uint8_t loByte = mtreg & 0b00011111; | ||
loByte |= 0b01100000;// Low bit: 011_MT[4,3,2,1,0] | ||
return writeByte(loByte); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
bool hp_BH1750::start(){ // Start a single shot measurement with given resolution and sensitivity | ||
reset(); // Reset the last result in data register to zero (0) | ||
return writeByte(m_resolutionMode); | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— | ||
uint16_t hp_BH1750::getBrightness(){ | ||
return (float)readValue() / ((float)SENSITIVITY_ADJ_DEFAULT / m_sensitivity) * m_resolution;; | ||
} | ||
//———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// light sensor BH1750 | ||
#include <Arduino.h> | ||
#include <Wire.h> | ||
|
||
#pragma once | ||
|
||
static const unsigned int BH1750_SATURATED = 65535; | ||
enum BH1750Quality { | ||
BH1750_QUALITY_HIGH = 0x20, | ||
BH1750_QUALITY_HIGH2 = 0x21, | ||
BH1750_QUALITY_LOW = 0x23, | ||
}; | ||
enum BH1750CalResult { | ||
BH1750_CAL_OK = 0, | ||
BH1750_CAL_MTREG_CHANGED = 1, | ||
BH1750_CAL_TOO_BRIGHT = 2, | ||
BH1750_CAL_TOO_DARK = 3, | ||
BH1750_CAL_COMMUNICATION_ERROR = 4, | ||
}; | ||
struct BH1750Timing { | ||
uint8_t mtregLow; | ||
uint8_t mtregHigh; | ||
unsigned int mtregLow_qualityHigh; | ||
unsigned int mtregHigh_qualityHigh; | ||
unsigned int mtregLow_qualityLow; | ||
unsigned int mtregHigh_qualityLow; | ||
}; | ||
|
||
enum BH1750MtregLimit { | ||
BH1750_MTREG_LOW = 31, // the datashet specifies 31 as minimum value | ||
// but you can go even lower (depending on your specific chip) | ||
// BH1750_MTREG_LOW=5 works with my chip and enhances the range | ||
// from 121.556,8 Lux to 753.652,5 Lux. | ||
BH1750_MTREG_HIGH = 254, | ||
BH1750_MTREG_DEFAULT = 69 | ||
}; | ||
|
||
enum BH1750Address { BH1750_TO_GROUND = 0x23, BH1750_TO_VCC = 0x5C }; | ||
extern TwoWire Wire; /**< Forward declaration of Wire object */ | ||
|
||
class hp_BH1750 { | ||
|
||
public: | ||
hp_BH1750(TwoWire* myWire); | ||
~hp_BH1750(); | ||
|
||
public: | ||
const uint8_t ONE_TIME_H_RESOLUTION_MODE = 0b00100000; // Start measurement at 1lx resolution. Measurement Time is typically 120ms. Power Down mode after measurement. | ||
const uint8_t ONE_TIME_H_RESOLUTION_MODE2 = 0b00100001; // Start measurement at 0.5lx resolution. Measurement Time is typically 120ms. Power Down mode after measurement. | ||
const uint8_t ONE_TIME_L_RESOLUTION_MODE = 0b00100011; // Start measurement at 4lx resolution. Measurement Time is typically 16ms. Power Down mode after measurement. | ||
|
||
const uint8_t ADDR_TO_GROUND = 0x23; | ||
const uint8_t ADDR_TO_VCC = 0x5C; | ||
|
||
const uint8_t SENSITIVITY_ADJ_MIN = 31; // Adjust measurement result for influence of optical window. (sensor sensitivity adjusting) | ||
const uint8_t SENSITIVITY_ADJ_DEFAULT = 69; | ||
const uint8_t SENSITIVITY_ADJ_MAX = 254; | ||
|
||
|
||
|
||
|
||
bool setSensitivity(uint8_t sensitivity); | ||
bool setResolutionMode(uint8_t resolution); | ||
bool begin(uint8_t address, int8_t sda, int8_t scl); | ||
bool writeByte(uint8_t b); | ||
int32_t readValue(); | ||
bool reset(); | ||
bool powerOn(); | ||
bool powerOff(); | ||
bool writeMtreg(uint8_t mtreg); | ||
|
||
|
||
|
||
void setQuality(BH1750Quality quality); | ||
bool start(); | ||
float getLux(); | ||
uint16_t getBrightness(); | ||
|
||
|
||
unsigned int getMtregTime() const; | ||
// unsigned int getMtregTime(byte mtreg) const; | ||
unsigned int getMtregTime(uint8_t mtreg, BH1750Quality quality) const; | ||
|
||
|
||
|
||
private: | ||
TwoWire* _wire; | ||
uint8_t _address; | ||
uint8_t _mtreg; | ||
uint8_t _percent = 50; | ||
bool _processed = false; | ||
unsigned int _mtregTime; | ||
unsigned long _startMillis; | ||
unsigned long _resultMillis; | ||
unsigned long _timeoutMillis; | ||
unsigned long _timeout = 10; | ||
int _offset = 0; | ||
unsigned int _nReads; | ||
unsigned int _time; | ||
unsigned int _value; | ||
float _qualFak = 0.5; | ||
float luxCache; | ||
BH1750Quality _quality; | ||
BH1750Timing _timing; | ||
|
||
uint8_t checkMtreg(uint8_t mtreg); | ||
|
||
|
||
// unsigned int readValue(); | ||
unsigned int readChange(uint8_t mtreg, BH1750Quality quality, bool change); | ||
|
||
uint8_t m_sensitivity = SENSITIVITY_ADJ_DEFAULT; | ||
float m_resolution = 1.0; | ||
uint8_t m_resolutionMode = ONE_TIME_H_RESOLUTION_MODE; | ||
uint8_t m_measurementTime = 120; | ||
|
||
const uint8_t m_Power_Down = 0b00000000; // No active state. | ||
const uint8_t m_Power_On = 0b00000001; // Waiting for measurement command. | ||
const uint8_t m_Reset = 0b00000111; // Reset Data register value. Reset command is not acceptable in Power Down mode. | ||
|
||
|
||
|
||
}; |
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
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
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