-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpd.h
117 lines (99 loc) · 3.38 KB
/
httpd.h
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
#include "wifi_funcs.h"
#include <WiFiClient.h>
#ifdef ESP8266
#include <ESP8266WebServer.h>
ESP8266WebServer server(80);
#elif defined ESP32
#include <WebServer.h>
WebServer server(80);
#endif
void handleRoot() {
if(server.hasArg(F("ledindex"))){
Logger.println("has arg ledindex");
String pixel = server.arg(F("ledindex"));
// ledIndex = pixel;
// setHTTPValue(pixel.toInt());
// strip.setPixelColor(pixel.toInt(),strip.Color(255,0,0));
Logger.println("Setting pixel:" + (String)pixel);
// strip.show();
// delay(2000);
server.send(200, "text/plain", "hello from esp8266! ledindex SET");
}
if(server.hasArg(F("pwm"))){
Logger.println("has arg PWM");
String pwmvalue = server.arg(F("pwm"));
// ledIndex = pixel;
// setPWM(pwmvalue.toInt());
// strip.setPixelColor(pixel.toInt(),strip.Color(255,0,0));
Logger.println("Setting PWM:" + (String)pwmvalue);
// strip.show();
// delay(2000);
// server.send(200, "text/plain", "hello from esp8266! PWM set:" + (String)getPWM());
// analogWrite(16,pwmvalue.toInt());
setHTTPValue(pwmvalue);
}
if(server.hasArg(F("freq"))){
Logger.println("has arg FREQ");
String pwmvalue = server.arg(F("freq"));
// ledIndex = pixel;
// setPWM(pwmvalue.toInt());
// strip.setPixelColor(pixel.toInt(),strip.Color(255,0,0));
Logger.println("Setting FREQ:" + (String)pwmvalue);
// strip.show();
// delay(2000);
// server.send(200, "text/plain", "hello from esp8266! PWM set:" + (String)getPWM());
// analogWrite(16,pwmvalue.toInt());
setHTTPValueB(pwmvalue);
}
// digitalWrite(LED_BUILTIN, 1);
// server.send(200, "text/plain", "hello from esp8266!");
server.send(200, "text/plain", "hello from " + getHostname());
// digitalWrite(LED_BUILTIN, 0);
}
void handleNotFound() {
// digitalWrite(LED_BUILTIN, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
// digitalWrite(LED_BUILTIN, 0);
}
void httpd_init(){
if (MDNS.begin("esp8266")) {
Logger.println("[MDNS] responder started");
}
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.on("/", HTTP_OPTIONS, []() {
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
server.send(200, "text/plain", "" );
});
server.on("/", HTTP_GET, []() {
String response ;
// ... some code to prepare the response data...
server.sendHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
server.send(200, "text/plain", response.c_str() );
});
server.begin();
Logger.println("[HTTP] server started");
}
void httpd_process(void) {
server.handleClient();
#ifdef ESP8266
MDNS.update();
#endif
}