Ik heb een sensor om licht intensiteit te meten. De binnengekregen data is tot drie cijfers na de komma.
Ik wens af te ronden tot voor de komma. Alleen heb ik echt geen kaas gegeten van deze materie. De sensor is gekoppeld aan een wemos D1 mini. De sensor waarde wordt uitgelezen op een http pagina.
Kan iemand me op weg zetten? Onderstaande is de library van de sensor.
Ik wens af te ronden tot voor de komma. Alleen heb ik echt geen kaas gegeten van deze materie. De sensor is gekoppeld aan een wemos D1 mini. De sensor waarde wordt uitgelezen op een http pagina.
Kan iemand me op weg zetten? Onderstaande is de library van de sensor.
code:
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
| #ifdef USE_I2C #ifdef USE_TSL2561 /*********************************************************************************************\ * TSL2561 - Light Intensity * * Using library https://github.com/joba-1/Joba_Tsl2561 * * I2C Addresses: 0x29 (low), 0x39 (float) or 0x49 (high) \*********************************************************************************************/ #include <Tsl2561Util.h> Tsl2561 Tsl(Wire); void Tsl2561Detect() { if (!Tsl.available()) { Tsl.begin(); if (Tsl.available()) { snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "TSL2561", Tsl.address()); AddLog(LOG_LEVEL_DEBUG); } } } #ifdef USE_WEBSERVER const char HTTP_SNS_TSL2561[] PROGMEM = "%s{s}TSL2561 " D_ILLUMINANCE "{m}%u.%03u " D_UNIT_LUX "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr> #endif // USE_WEBSERVER void Tsl2561Show(boolean json) { uint8_t id; bool gain; Tsl2561::exposure_t exposure; uint16_t scaledFull, scaledIr; uint32_t full, ir; uint32_t milliLux; if (Tsl.available()) { if (Tsl.on()) { if( Tsl.id(id) && Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr) && Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr) && Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id))) { snprintf_P(log_data, sizeof(log_data), PSTR(D_ILLUMINANCE " g:%d, e:%d, f:%u, i:%u -> %u.%03u " D_UNIT_LUX), gain, exposure, full, ir, milliLux/1000, milliLux%1000); AddLog(LOG_LEVEL_DEBUG); if (json) { snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"), mqtt_data, milliLux/1000, milliLux%1000); #ifdef USE_DOMOTICZ if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, (milliLux+500)/1000); #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TSL2561, mqtt_data, milliLux/1000, milliLux%1000); #endif // USE_WEBSERVER } } Tsl.off(); } } } /*********************************************************************************************\ * Interface \*********************************************************************************************/ #define XSNS_16 boolean Xsns16(byte function) { boolean result = false; if (i2c_flg) { switch (function) { case FUNC_PREP_BEFORE_TELEPERIOD: Tsl2561Detect(); break; case FUNC_JSON_APPEND: Tsl2561Show(1); break; #ifdef USE_WEBSERVER case FUNC_WEB_APPEND: Tsl2561Show(0); break; #endif // USE_WEBSERVER } } return result; } #endif // USE_TSL2561 #endif // USE_I2C |