-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharduino_tally.ino
146 lines (123 loc) · 4.13 KB
/
arduino_tally.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
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <Adafruit_NeoPixel.h>
#include "index.h"
const char* ssid = "xxxxxx";
const char* pass = "xxxxxx";
const char* host_name = "esp-tally";
//LED config
const int led = 2; //D4 wemos mini also on board led
#define LED_PIN 15 //D8 wemos mini for NeoPixel
#define LED_COUNT 10
// group[x][2] x is number of groups
int group[2][2] = {{0,5},{5,5}}; //{start,count} for each group
// Web server port
const uint16_t port = 80;
// Web server object declaration
ESP8266WebServer server(port);
//NeoPixel onject declaration
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void neo_set(int grp, int r, int g, int b){
int start, end;
if(grp == 0){
start = 0;
end = strip.numPixels();
} else{
start = group[grp-1][0];
end = start + group[grp-1][1];
}
for(int i= start; i<end; i++) {
strip.setPixelColor(i,strip.Color(r,g,b)); //RED
strip.show();
}
}
/**GET NOT FULLY IMPLEMENTED
* USE FOR Testing of LEDs not actual tallying
*/
void handleGET()
{
/*String message = "get args: ";
for (int i = 0; i < server.args(); i++) {
message += "Arg no. " + (String)i + " –> "; //Include the current iteration value
message += server.argName(i) + ": "; //Get the name of the parameter
message += server.arg(i) + "\n"; //Get the value of the parameter
}
Serial.print(message);*/
// *** Turn on or off the LED ***
if (server.arg("led") == "ON"){
int r_val = server.arg("red").toInt();
int g_val = server.arg("green").toInt();
int b_val = server.arg("blue").toInt();
int grp_val = server.arg("group").toInt();
digitalWrite(led, LOW);
Serial.print("LED ON via GET\n");
neo_set(grp_val, r_val, g_val, b_val);; }
else if (server.arg("led") == "OFF") {
int grp_val = server.arg("group").toInt();
digitalWrite(led, HIGH);
Serial.print("LED OFF via GET\n");
neo_set(grp_val, 0, 0, 0 );}
if (server.arg("/")){
server.send_P(200, "text/html", index_html);
}
}
void handlePOST() {
/*String message = "post args: ";
for (int i = 0; i < server.args(); i++) {
message += "Arg no. " + (String)i + " –> "; //Include the current iteration value
message += server.argName(i) + ": "; //Get the name of the parameter
message += server.arg(i) + "\n"; //Get the value of the parameter
}
Serial.print(message);*/
if (server.arg("led") == "ON"){
int r_val = server.arg("red").toInt();
int g_val = server.arg("green").toInt();
int b_val = server.arg("blue").toInt();
int grp_val = server.arg("group").toInt();
digitalWrite(led, LOW);
Serial.print("LED ON via POST\n");
neo_set(grp_val, r_val, g_val, b_val); }
else if (server.arg("led") == "OFF") {
int grp_val = server.arg("group").toInt();
digitalWrite(led, HIGH);
Serial.print("LED OFF via POST\n");
neo_set(grp_val, 0, 0, 0 );}
server.sendHeader("Location","/");
server.send(303);
}
void setup()
{
//Set Neopixels
strip.begin(); //initialize neopixel
strip.show(); //set all off
strip.setBrightness(255); // max=255
// Set pin as output
pinMode(led, OUTPUT);
// Setup serial communication
Serial.begin(74880);
// *** Connect to a WiFi acces point ***
Serial.printf("Connecting to %s ", ssid);
WiFi.hostname(host_name);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
digitalWrite(led, !digitalRead(led));
}
MDNS.begin(host_name);
Serial.printf(" Connected\n");
Serial.printf("IP address: %s\n", WiFi.localIP().toString().c_str());
// *** Start web server ***
server.on("/", HTTP_GET, handleGET);
server.on("/", HTTP_POST, handlePOST);
server.begin();
Serial.printf("Web server started at port %d\n", port);
MDNS.addService("http", "tcp", 80);
}
void loop()
{
MDNS.update();
server.handleClient();
}