-
Notifications
You must be signed in to change notification settings - Fork 12
/
_P010_BH1750.ino
83 lines (71 loc) · 2.36 KB
/
_P010_BH1750.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//#######################################################################################################
//#################################### Plugin-010: LuxRead ############################################
//#######################################################################################################
MyMessage *msgLux010;
#define PLUGIN_010
#define PLUGIN_ID_010 10
#define PLUGIN_NAME_010 "Luminosity - BH1750"
#define PLUGIN_VALUENAME1_010 "Lux"
#define BH1750_ADDRESS 0x23
boolean Plugin_010_init = false;
boolean Plugin_010(byte function, struct EventStruct *event, String& string)
{
boolean success=false;
switch(function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_010;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].ValueCount = 1;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_010);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_010));
break;
}
case PLUGIN_INIT:
{
if (!msgLux010)
msgLux010 = new MyMessage(event->BaseVarIndex, V_LIGHT_LEVEL);
present(event->BaseVarIndex, V_LIGHT_LEVEL);
Serial.print("Present BH1750: ");
Serial.println(event->BaseVarIndex);
success = true;
break;
}
case PLUGIN_READ:
{
if (!Plugin_010_init)
{
Plugin_010_init=true;
Wire.beginTransmission(BH1750_ADDRESS);
Wire.write(0x10); // 1 lx resolution
Wire.endTransmission();
}
Wire.requestFrom(BH1750_ADDRESS, 2);
byte b1 = Wire.read();
byte b2 = Wire.read();
float val=0;
val=((b1<<8)|b2)/1.2;
UserVar[event->BaseVarIndex] = val;
String log = F("LUX : Light intensity: ");
send(msgLux010->set((uint16_t)UserVar[event->BaseVarIndex]));
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO,log);
success=true;
break;
}
}
return success;
}