This repository was archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeMCULightSensor.ino
More file actions
147 lines (112 loc) · 2.96 KB
/
NodeMCULightSensor.ino
File metadata and controls
147 lines (112 loc) · 2.96 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "StopWatch.h"
//#include <Wire.h>
#include <Adafruit_TCS34725.h>
#include "MQTT.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
StopWatch ReportValue;
StopWatch SendStateTime;
StopWatch CalTime(false);
void setup()
{
DM_MASK = DM_INFO | DM_ERROR;
Serial.begin(921600);
MQTT_Setup();
tcs.setGain(TCS34725_GAIN_60X);
tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_2_4MS);
if (!tcs.begin()) {
DebugMessage(DM_ERROR, "No Light Sensor Detected");
}
pinMode(D5, OUTPUT);
digitalWrite(D5, LOW);
pinMode(D6, INPUT_PULLUP);
}
uint16_t clear_state, red_state, green_state, blue_state;
uint64_t red_acc, green_acc, blue_acc;
uint16_t samples;
float red_cal = 1, green_cal = 1, blue_cal = 1;
void loop()
{
MQTT_Loop();
accumulate();
if (!digitalRead(D6))
{
//Run Once on entry
if (!CalTime.isRunning)
{
DebugMessage(DM_INFO, "Start Calibration");
CalTime.start();
digitalWrite(D5, HIGH);
samples = 1;
red_acc = 1;
green_acc = 1;
blue_acc = 1;
}
}//Run Once on exit
else if (CalTime.isRunning)
{
DebugMessage(DM_INFO, "End Calibration: " + String(samples) + " samples");
CalTime.stop();
digitalWrite(D5, LOW);
uint32_t min;
min = red_acc;
if (green_acc < min) min = green_acc;
if (blue_acc < min) min = blue_acc;
red_cal = (float)min / red_acc;
green_cal = (float)min / green_acc;
blue_cal = (float)min / blue_acc;
DebugMessage(DM_INFO, "Red: " + String(red_cal) + " Green: " + String(green_cal) + " Blue: " + String(blue_cal));
}
else if (SendStateTime.getTime() > 60000)
{
uint64_t max;
//calibrate the values
red_acc *= red_cal;
green_acc *= green_cal;
blue_acc *= blue_cal;
//find the max value
max = red_acc;
if (green_acc > max) max = green_acc;
if (blue_acc > max) max = blue_acc;
//scale to 256
red_acc *= 0xFF;
green_acc *= 0xFF;
blue_acc *= 0xFF;
//divide out the max value
red_state = red_acc / max;
green_state = green_acc / max;
blue_state = blue_acc / max;
//Send the state and reset the timer
DebugMessage(DM_INFO, "Samples: " + String(samples) + " Max: " + String((float)max));
DebugMessage(DM_INFO, String(red_state) + ", " + String(green_state) + ", " + String(blue_state));
sendState();
SendStateTime.reset();
samples = 1;
red_acc = 1;
green_acc = 1;
blue_acc = 1;
}
}
void accumulate()
{
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
red_acc += r;
green_acc += g;
blue_acc += b;
samples++;
}
void sendState() {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["state"] = "ON";
JsonObject& color = root.createNestedObject("color");
color["r"] = red_state;
color["g"] = green_state;
color["b"] = blue_state;
root["brightness"] = clear_state;
char* buffer = new char[root.measureLength() + 1];
root.printTo(buffer, root.measureLength() + 1);
publish(buffer);
DebugMessage(DM_SEND, "Send State: " + String(buffer));
delete buffer;
}