Skip to content

Commit 31bb0c5

Browse files
committed
Added TSL2561 illuminance I2C sensor
1 parent 65f56cd commit 31bb0c5

File tree

8 files changed

+104
-7
lines changed

8 files changed

+104
-7
lines changed

History.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version 3.xxxx (August xx 2016)
2+
- Implemented: TSL2561 illuminance I2C sensor (ldrolez)
23
- Implemented: AccuWeather support
34
- Implemented: Atag One Thermostat support
45
- Implemented: Blockly/Lua Camera device, to send camera snapshots with subject after xx seconds

hardware/I2C.cpp

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ const unsigned char BMPx8x_OverSampling = 3;
9292
#define HTU21D_TEMP_DELAY 70 /* Maximum required measuring time for a complete temperature read */
9393
#define HTU21D_HUM_DELAY 36 /* Maximum required measuring time for a complete humidity read */
9494

95+
// TSL2561 registers
96+
#define TSL2561_ADDRESS 0x39 /* I2C address */
97+
#define TSL2561_INIT 0x03 /* start integrations */
98+
#define TSL2561_Channel0 0xAC /* IR+Visible lux */
99+
#define TSL2561_Channel1 0xAE /* IR only lux */
100+
101+
95102
I2C::I2C(const int ID, const int Mode1)
96103
{
97104
switch (Mode1)
@@ -102,6 +109,9 @@ I2C::I2C(const int ID, const int Mode1)
102109
case 2:
103110
device = "HTU21D";
104111
break;
112+
case 3:
113+
device = "TSL2561";
114+
break;
105115
}
106116

107117
m_stoprequested = false;
@@ -119,9 +129,6 @@ I2C::~I2C()
119129

120130
bool I2C::StartHardware()
121131
{
122-
#ifndef __arm__
123-
return false;
124-
#endif
125132
m_stoprequested = false;
126133
if (device == "BMP085")
127134
{
@@ -158,6 +165,12 @@ void I2C::Do_Work()
158165
int msec_counter = 0;
159166
int sec_counter = I2C_READ_INTERVAL - 5;
160167
_log.Log(LOG_STATUS, "%s: Worker started...", device.c_str());
168+
169+
if (device == "TSL2561")
170+
{
171+
TSL2561_Init();
172+
}
173+
161174
while (!m_stoprequested)
162175
{
163176
sleep_milliseconds(500);
@@ -183,6 +196,10 @@ void I2C::Do_Work()
183196
{
184197
HTU21D_ReadSensorDetails();
185198
}
199+
else if (device == "TSL2561")
200+
{
201+
TSL2561_ReadSensorDetails();
202+
}
186203
}
187204
catch (...)
188205
{
@@ -228,7 +245,7 @@ int I2C::i2c_Open(const char *I2CBusName)
228245
#endif
229246
}
230247

231-
// BMP085 & BMP180 Specific code
248+
// BMP085, BMP180, HTU and TSL common code
232249

233250
int I2C::ReadInt(int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRead)
234251
{
@@ -244,6 +261,10 @@ int I2C::ReadInt(int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRe
244261
struct i2c_msg htu_read_reg[1] = {
245262
{ HTU21D_ADDRESS, I2C_M_RD, bytesToRead, devValues }
246263
};
264+
struct i2c_msg tsl_read_reg[2] = {
265+
{ TSL2561_ADDRESS, 0, 1, &startReg },
266+
{ TSL2561_ADDRESS, I2C_M_RD, bytesToRead, devValues }
267+
};
247268

248269
//Build a register read command
249270
//Requires a one complete message containing a command
@@ -258,6 +279,11 @@ int I2C::ReadInt(int fd, uint8_t *devValues, uint8_t startReg, uint8_t bytesToRe
258279
messagebuffer.nmsgs = 1;
259280
messagebuffer.msgs = htu_read_reg; //load the 'read__reg' message into the buffer
260281
}
282+
else if (device == "TSL2561")
283+
{
284+
messagebuffer.nmsgs = 2;
285+
messagebuffer.msgs = tsl_read_reg; //load the 'read__reg' message into the buffer
286+
}
261287

262288
rc = ioctl(fd, I2C_RDWR, &messagebuffer); //Send the buffer to the bus and returns a send status
263289
if (rc < 0) {
@@ -282,6 +308,9 @@ int I2C::WriteCmd(int fd, uint8_t devAction)
282308
struct i2c_msg htu_write_reg[1] = {
283309
{ HTU21D_ADDRESS, 0, 1, datatosend }
284310
};
311+
struct i2c_msg tsl_write_reg[1] = {
312+
{ TSL2561_ADDRESS, 0, 1, datatosend }
313+
};
285314

286315
if (device == "BMP085")
287316
{
@@ -298,6 +327,13 @@ int I2C::WriteCmd(int fd, uint8_t devAction)
298327
//Requires one complete message containing a reg address and command
299328
messagebuffer.msgs = htu_write_reg; //load the 'write__reg' message into the buffer
300329
}
330+
else if (device == "TSL2561")
331+
{
332+
datatosend[0] = devAction;
333+
//Build a register write command
334+
//Requires one complete message containing a reg address and command
335+
messagebuffer.msgs = tsl_write_reg; //load the 'write__reg' message into the buffer
336+
}
301337

302338
messagebuffer.nmsgs = 1; //One message/action
303339
rc = ioctl(fd, I2C_RDWR, &messagebuffer); //Send the buffer to the bus and returns a send status
@@ -415,6 +451,46 @@ void I2C::HTU21D_ReadSensorDetails()
415451
SendTempHumSensor(1, 255, temperature, round(humidity), "TempHum");
416452
}
417453

454+
// TSL2561 functions
455+
void I2C::TSL2561_Init()
456+
{
457+
#ifdef __arm__
458+
int fd = i2c_Open(m_ActI2CBus.c_str());
459+
if (fd < 0) {
460+
_log.Log(LOG_ERROR, "%s: Error opening device!...", device.c_str());
461+
return;
462+
}
463+
if (WriteCmd(fd, TSL2561_INIT) != 0) {
464+
_log.Log(LOG_ERROR, "%s: Error initializing device!...", device.c_str());
465+
}
466+
close(fd);
467+
#endif
468+
}
469+
470+
void I2C::TSL2561_ReadSensorDetails()
471+
{
472+
float lux;
473+
uint8_t rValues[2];
474+
475+
#ifndef __arm__
476+
lux = 1984;
477+
#else
478+
int fd = i2c_Open(m_ActI2CBus.c_str());
479+
if (fd < 0) {
480+
_log.Log(LOG_ERROR, "%s: Error opening device!...", device.c_str());
481+
return;
482+
}
483+
if (ReadInt(fd, rValues, TSL2561_Channel0, 2) != 0) {
484+
_log.Log(LOG_ERROR, "%s: Error reading lux!...", device.c_str());
485+
close(fd);
486+
return;
487+
}
488+
close(fd);
489+
lux = rValues[1] * 256.0 + rValues[0];
490+
#endif
491+
SendLuxSensor(0, 0, 255, lux, "Lux");
492+
}
493+
418494
// BMP085 functions
419495
int I2C::bmp_Calibration(int fd)
420496
{

hardware/I2C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ class I2C : public CDomoticzHardwareBase
6565
int HTU21D_checkCRC8(uint16_t data);
6666
int HTU21D_GetHumidity(int fd, float *Pres);
6767
int HTU21D_GetTemperature(int fd, float *Temp);
68+
69+
// TSL2561 stuff
70+
void TSL2561_ReadSensorDetails();
71+
void TSL2561_Init();
6872
};

main/RFXNames.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ const char *Hardware_Type_Desc(int hType)
232232
{ HTYPE_Ec3kMeterTCP, "Energy Count 3000/ NETBSEM4/ La Crosse RT-10 LAN" },
233233
{ HTYPE_OpenWeatherMap, "Open Weather Map" },
234234
{ HTYPE_GoodweAPI, "Goodwe solar inverter via Web API" },
235+
{ HTYPE_RaspberryTSL2561, "I2C sensor TSL2561 Illuminance" },
235236
{ 0, NULL, NULL }
236237
};
237238
return findTableIDSingle1 (Table, hType);

main/RFXNames.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ enum _eHardwareTypes {
164164
HTYPE_BleBox, //82
165165
HTYPE_OpenWeatherMap, //83
166166
HTYPE_GoodweAPI, //84
167+
HTYPE_RaspberryTSL2561, //85
167168
HTYPE_END
168169
};
169170

main/WebServer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,8 @@ namespace http {
902902
#ifdef WIN32
903903
if (
904904
(ii == HTYPE_RaspberryBMP085) ||
905-
(ii == HTYPE_RaspberryHTU21D)
905+
(ii == HTYPE_RaspberryHTU21D) ||
906+
(ii == HTYPE_RaspberryTSL2561)
906907
)
907908
{
908909
bDoAdd = false;
@@ -1071,6 +1072,9 @@ namespace http {
10711072
else if (htype == HTYPE_RaspberryHTU21D) {
10721073
//all fine here!
10731074
}
1075+
else if (htype == HTYPE_RaspberryTSL2561) {
1076+
//all fine here!
1077+
}
10741078
else if (htype == HTYPE_Dummy) {
10751079
//all fine here!
10761080
}
@@ -1335,6 +1339,9 @@ namespace http {
13351339
else if (htype == HTYPE_RaspberryHTU21D) {
13361340
//All fine here
13371341
}
1342+
else if (htype == HTYPE_RaspberryTSL2561) {
1343+
//All fine here
1344+
}
13381345
else if (htype == HTYPE_Dummy) {
13391346
//All fine here
13401347
}

main/mainworker.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,9 @@ bool MainWorker::AddHardwareFromParams(
787787
case HTYPE_RaspberryHTU21D:
788788
pHardware = new I2C(ID,2);
789789
break;
790+
case HTYPE_RaspberryTSL2561:
791+
pHardware = new I2C(ID,3);
792+
break;
790793
case HTYPE_Wunderground:
791794
pHardware = new CWunderground(ID,Username,Password);
792795
break;

www/app/HardwareController.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ define(['app'], function (app) {
7676
(text.indexOf("GPIO") >= 0) ||
7777
(text.indexOf("BMP085") >= 0) ||
7878
(text.indexOf("HTU21D") >= 0) ||
79+
(text.indexOf("TSL2561") >= 0) ||
7980
(text.indexOf("Dummy") >= 0) ||
8081
(text.indexOf("System Alive") >= 0) ||
8182
(text.indexOf("PiFace") >= 0) ||
@@ -814,7 +815,8 @@ define(['app'], function (app) {
814815
(text.indexOf("TE923") >= 0) ||
815816
(text.indexOf("Volcraft") >= 0) ||
816817
(text.indexOf("BMP085") >= 0) ||
817-
(text.indexOf("HTU21D") >= 0) ||
818+
(text.indexOf("HTU21D") >= 0) ||
819+
(text.indexOf("TSL2561") >= 0) ||
818820
(text.indexOf("Dummy") >= 0) ||
819821
(text.indexOf("System Alive") >= 0) ||
820822
(text.indexOf("Kodi") >= 0) ||
@@ -4191,7 +4193,7 @@ define(['app'], function (app) {
41914193
{
41924194
SerialName="USB";
41934195
}
4194-
else if ((item.Type == 13)||(item.Type == 71))
4196+
else if ((item.Type == 13)||(item.Type == 71)||(item.Type == 85))
41954197
{
41964198
SerialName="I2C";
41974199
}
@@ -4428,6 +4430,7 @@ define(['app'], function (app) {
44284430
(data["Type"].indexOf("Volcraft") >= 0)||
44294431
(data["Type"].indexOf("BMP085") >= 0)||
44304432
(data["Type"].indexOf("HTU21D") >= 0)||
4433+
(data["Type"].indexOf("TSL2561") >= 0)||
44314434
(data["Type"].indexOf("Dummy") >= 0)||
44324435
(data["Type"].indexOf("System Alive") >= 0)||
44334436
(data["Type"].indexOf("PiFace") >= 0)||
@@ -4591,6 +4594,7 @@ define(['app'], function (app) {
45914594
(text.indexOf("Volcraft") >= 0)||
45924595
(text.indexOf("BMP085") >= 0)||
45934596
(text.indexOf("HTU21D") >= 0)||
4597+
(text.indexOf("TSL2561") >= 0)||
45944598
(text.indexOf("Dummy") >= 0)||
45954599
(text.indexOf("System Alive") >= 0)||
45964600
(text.indexOf("PiFace") >= 0))

0 commit comments

Comments
 (0)