-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8266-neopixel-wifi-final.ino
142 lines (122 loc) · 4.16 KB
/
8266-neopixel-wifi-final.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>
#ifndef LOCAL_SSID
#define LOCAL_SSID "nume-retea-wireless" /** numele rețelei, așa cum apare el în telefon sau pe calculator */
#define LOCAL_PASS "parola-de-la-wireless" /** parola de la wireless */
#endif
const char* ssid = LOCAL_SSID;
const char* password = LOCAL_PASS;
const uint16_t all_pixels = 150;
ESP8266WebServer server(80);
Adafruit_NeoPixel pixels(all_pixels, D5, NEO_GRB + NEO_KHZ800);
uint16_t _min = 1023;
uint16_t _max = 0;
uint16_t _length = 32;
uint16_t _hue = 40000;
uint8_t _mode = 1;
uint16_t pixel = 0;
uint16_t last_pixel = 0;
void handle_root() {
server.send(200, "text/html", "");
}
void handle_404() {
server.send(404, "application/json", "{\"error\":1,\"message\":\"not found\"}");
}
void handle_form () {
long convert; /** variabilă temporară în care convertesc șiruri în numere */
if (server.method() != HTTP_POST) { /** dacă cererea nu e HTTP POST */
server.send(405, "application/json", "{\"error\":1,\"message\":\"method not allowed\"}");
/** trimit către client codul de eroare asociat cu method not allowed, 405 */
return;
}
for (uint8_t c = 0; c < server.args(); c++) { /** scanez toți parametrii */
if (server.argName(c) == String("mode")) { /** dacă am găsit mode */
convert = server.arg(c).toInt(); /** în convert pun valoarea parametrului */
/** dar dacă cumva valoarea e mai mare de 1, pun în _mode 1 */
_mode = convert < 0 ? 0 : (convert > 255 ? 255 : convert);
}
if (server.argName(c) == String("hue")) { /** dacă am găsit hue */
convert = server.arg(c).toInt(); /** convertesc valoarea lui în număr */
/** iar dacă numărul e mai mare de 65535, pun în _hue 65535 */
_hue = convert < 0 ? 0 : (convert > 65535 ? 65535 : convert);
}
if (server.argName(c) == String("length")) { /** dacă am găsit length */
convert = server.arg(c).toInt(); /** convertesc valoarea lui în număr */
/** dacă numărul e mai mare decât all_pixels - 1, pun în _length all_pixels - 1 */
_length = convert < 0 ? 0 : (convert > all_pixels - 1 ? all_pixels - 1 : convert);
}
/** dacă găsesc reset cu valoarea on, reset valorile pentru _min și _max */
if (server.argName(c) == String("reset") && server.arg(c) == String("on")) {
_min = 1023;
_max = 0;
}
}
/** trimit ca răspuns valorile nou stabilite */
server.send (200, "application/json",
"{\"mode\":" + String(_mode) +
",\"length\":" + String(_length) +
",\"hue\":" + String(_hue) +
",\"min\":" + String(_min) +
",\"max\":" + String(_max) +
"}");
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("#");
}
Serial.println("");
Serial.println(WiFi.localIP());
server.on("/", handle_root);
server.on("/rpc/", handle_form); /** aici asociez handle_form cu serverul web */
server.onNotFound(handle_404);
server.begin();
Serial.println("S");
pixels.begin();
Serial.println("P");
pixels.clear();
Serial.println("C");
pinMode (D6, OUTPUT);
pinMode (D7, OUTPUT);
pinMode (D8, OUTPUT);
digitalWrite (D6, LOW);
digitalWrite (D7, LOW);
digitalWrite (D8, LOW);
}
void loop() {
uint8_t
b_map;
uint16_t
a_val,
a_map;
uint32_t
color;
server.handleClient();
a_val = analogRead(A0);
if (_min > a_val) {
_min = a_val;
}
if (_max < a_val) {
_max = a_val;
}
switch (_mode) {
case 0:
a_map = _max > _min ? (uint16_t) floor (65535.0 * (float)(a_val - _min) / (float) (_max - _min)) : 0;
color = pixels.gamma32(pixels.ColorHSV(a_map));
break;
case 1:
b_map = _max > _min ? (uint8_t) floor (255.0 * (float)(a_val - _min) / (float) (_max - _min)) : 0;
color = pixels.gamma32(pixels.ColorHSV(_hue, 255, b_map));
break;
}
pixels.setPixelColor(pixel, color);
last_pixel = (all_pixels + pixel - _length) % all_pixels;
pixel = (pixel + 1) % all_pixels;
pixels.setPixelColor(last_pixel, pixels.Color(0,0,0));
pixels.show();
}