Last active
April 28, 2017 22:11
-
-
Save asteriskie/77610f0ba561efc51f68d6e1c3beb94a to your computer and use it in GitHub Desktop.
Sample git to change brightness of led with slider over http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <DNSServer.h> | |
#include <ESP8266WiFi.h> | |
////////////////////// | |
// WiFi Definitions // | |
////////////////////// | |
const char WiFiAPPSK[] = "55378008"; | |
///////////////////// | |
// Pin Definitions // | |
///////////////////// | |
const int LED_PIN = BUILTIN_LED; // Thing's onboard, green LED | |
//const int ANALOG_PIN = A0; // The only analog pin on the Thing | |
//const int DIGITAL_PIN = D0; // Digital pin to be read | |
const byte DNS_PORT = 53; // DNS Server Port | |
WiFiServer server(80); | |
DNSServer dnsServer; | |
void initHardware() | |
{ | |
Serial.begin(115200); | |
// pinMode(DIGITAL_PIN, INPUT_PULLUP); | |
pinMode(LED_PIN, OUTPUT); | |
// digitalWrite(LED_PIN, LOW); | |
// Don't need to set ANALOG_PIN as input, | |
// that's all it can be. | |
} | |
void setupWiFi() | |
{ | |
WiFi.mode(WIFI_AP); | |
// Do a little work to get a unique-ish name. Append the | |
// last two bytes of the MAC (HEX'd) to "Thing-": | |
uint8_t mac[WL_MAC_ADDR_LENGTH]; | |
WiFi.softAPmacAddress(mac); | |
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + | |
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX); | |
macID.toUpperCase(); | |
String AP_NameString = "D1Mini-Controller " + macID; | |
char AP_NameChar[AP_NameString.length() + 1]; | |
memset(AP_NameChar, 0, AP_NameString.length() + 1); | |
for (int i=0; i<AP_NameString.length(); i++) | |
AP_NameChar[i] = AP_NameString.charAt(i); | |
WiFi.softAP(AP_NameChar, WiFiAPPSK); | |
dnsServer.setTTL(300); | |
dnsServer.start(DNS_PORT, "*", WiFi.softAPIP()); | |
Serial.print(WiFi.softAPIP().toString()); | |
} | |
void setup() | |
{ | |
initHardware(); | |
setupWiFi(); | |
server.begin(); | |
} | |
void loop() | |
{ | |
dnsServer.processNextRequest(); | |
// Check if a client has connected | |
WiFiClient client = server.available(); | |
if (!client) { | |
return; | |
} | |
else | |
Serial.println("Client connected"); | |
// Read the first line of the request | |
String req = client.readStringUntil('\r'); | |
Serial.println(req); | |
client.flush(); | |
// Match the request | |
int val = 0; // We'll use 'val' to keep track of both the | |
// request type (read/set) and value if set. | |
if (req.indexOf("/builtin/")) { | |
req.replace("GET /builtin/",""); | |
val = req.toInt(); | |
Serial.print("Hello with " + String(val) + "\n"); | |
} | |
if (val >= 0) | |
analogWrite(LED_PIN, -val); | |
client.flush(); | |
// Prepare the response. Start with the common header: | |
String s = "HTTP/1.1 200 OK\r\n"; | |
s += "Content-Type: text/html\r\n\r\n"; | |
s += "<!DOCTYPE HTML>\r\n"; | |
s += "<title>Arduino LED Control</title>\r\n"; | |
s += "<html><body>\r\n"; | |
s += "<h1>Arduino LED Controller Page</h1>\r\n"; | |
s += "<input id=\"builtin\" type=\"range\" min=\"0\" max=\"1023\" step=\"5\" onchange=\"showValue(this.value)\"/><br>\r\n"; | |
s += "<span id=\"valueBuiltin\">0</span>\r\n"; | |
s += "<script type=\"text/javascript\">\r\n"; | |
s += "function showValue(newValue) {\r\n"; | |
//s += "document.getElementById(\"builtin\").value=newValue;\r\n"; | |
s += "document.getElementById(\"valueBuiltin\").innerHTML=newValue;\r\n"; | |
s += "request = new XMLHttpRequest();\r\n"; | |
s += "server = \"/builtin/\" + newValue;\r\n"; | |
s += "request.open(\"GET\", server, true);\r\n"; | |
s += "request.send(null);\r\n"; | |
s += "}</script>\r\n"; | |
s += "<a href=\"/led/dim\">Toggle => Dim</a><br><br>\r\n"; | |
s += "<a href=\"/led/bright\">Toggle => Bright</a><br><br>\r\n"; | |
s += "<a href=\"/led/off\">Toggle => Off</a><br><br>\r\n"; | |
if (val == -1) | |
{ | |
s += req + "\r\n"; | |
} | |
s += "</body></html>\n"; | |
// Send the response to the client | |
client.print(s); | |
delay(1); | |
Serial.println("Client disonnected"); | |
// The client will actually be disconnected | |
// when the function returns and 'client' object is detroyed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment