-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi_rgb.ino
164 lines (122 loc) · 4.33 KB
/
wifi_rgb.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// Wifi creds:
const char* ssid = "ssid";
const char* password = "password";
// RGB Light Initialize:
// Pins:
int redPin = 13; // GPIO 13 | D7 On board.
int greenPin = 12; // GPIO 14 | D5 On board.
int bluePin = 14; // GPIO 12 | D6 On board.
// RGB Values:
int redValue = 0; // Initial Red Value.
int greenValue = 0; // Initial Green Value.
int blueValue = 0; // Initial Blue Value.
// Flicker:
int flickerDelay = 0; // Delay value.
int flickerRange = 0; // Color range value.
int flickerDelayRemote = 0; // Delay value from the server.
int flickerRangeRemote = 0; // Color range value from the server.
int flickerRed; // Altered red value to produce flicker effect.
int flickerGreen; // Altered green value to produce flicker effect.
int flickerBlue; // Altered blue value to produce flicker effect.
// If your LED is NOT a common anode; comment the next line out.
#define COMMON_ANODE
int cnt = 0; // Initial loop count.
void setup() {
Serial.begin(115200); // Start the serial monitor.
// RGB Light Setup:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.println("Hello Digital Craft");
Serial.println("Connecting ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("WiFi Connected");
Serial.println(WiFi.localIP());
}
void loop() {
// After it loops 10 times; check the server for an update:
if (cnt == 10){
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://dockit.org/candles/data.txt");
int httpCode = http.GET();
if (httpCode > 0){
Serial.println("Reading data: ");
String payload = http.getString();
// Parse the data:
redValue = getValue(payload, ':', 0).toInt();
greenValue = getValue(payload, ':', 1).toInt();
blueValue = getValue(payload, ':', 2).toInt();
flickerRangeRemote = getValue(payload, ':', 3).toInt();
flickerDelayRemote = getValue(payload, ':', 4).toInt();
// Print the values to the serial monitor:
Serial.println("Red:" + String(redValue));
Serial.println("Green:" + String(greenValue));
Serial.println("Blue:" + String(blueValue));
Serial.println("Decay:" + String(flickerRangeRemote));
Serial.println("Speed:" + String(flickerDelayRemote));
}else{
Serial.println("Something baaaaaaad happened!");
}
http.end();
}
cnt = 0; // Reset loop counter.
}
// Get flicker value for Red:
flickerRange = random(0,flickerRangeRemote); // Random number for color value.
if ((redValue - flickerRange) < 0) {
flickerRed = redValue + flickerRange;
}else{
flickerRed = redValue - flickerRange;
}
// Get flicker value for Green:
flickerRange = random(0,flickerRangeRemote); // Random number for color value.
if ((greenValue - flickerRange) < 0) {
flickerGreen = greenValue + flickerRange;
}else{
flickerGreen = greenValue - flickerRange;
}
// Get flicker value for Blue:
flickerRange = random(0,flickerRangeRemote); // Random number for color value.
if ((blueValue - flickerRange) < 0) {
flickerBlue = blueValue + flickerRange;
}else{
flickerBlue = blueValue - flickerRange;
}
// Get flicker delay value:
flickerDelay = random(20,20+flickerDelayRemote); // Randomize the color flicker/
setColor(flickerRed, flickerGreen, flickerBlue); // Send color values to the light.
delay(flickerDelay); // Apply the delay.
++cnt; // Increment the loop count by 1.
}
// Parsing function:
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
// Sends color values to LED:
void setColor(int red, int green, int blue) {
#ifdef COMMON_ANODE
red = 1024 - red;
green = 1024 - green;
blue = 1024 - blue;
#endif
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}