Code from https://blog.haschek.at/post/fb08b
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
const int pin_all = D0; | |
const int pin_blue = D2; | |
#include <ESP8266WiFi.h> | |
const char* ssid = "your_wifi_name_here"; | |
const char* password = "your_wifi_password_here"; | |
int fade_delay = 2; //how many milliseconds between pulses (the lower, the faster the light changes) | |
//code starts here | |
int lastall = 0; | |
int lastblue = 0; | |
WiFiServer server(80); | |
void setup() { | |
Serial.begin(115200); | |
pinMode(pin_all, OUTPUT); | |
pinMode(pin_blue, OUTPUT); | |
setAll(0); | |
setBlue(0); | |
WifiConnect(); | |
} | |
void loop() { | |
WifiConnect(); | |
// Check if a client has connected | |
WiFiClient client = server.available(); | |
if (!client) { | |
return; | |
} | |
// Wait until the client sends some data | |
//Serial.println("new client"); | |
while(!client.available()){ | |
delay(1); | |
} | |
// Read the first line of the request | |
String req = client.readStringUntil('\r'); | |
//Serial.println(req); | |
//Serial.println(req.substring(5,8)); | |
client.flush(); | |
// Match the request | |
int val; | |
if(req.substring(5,8)=="all") | |
{ | |
String text = req.substring(9,15); | |
setAll(text.toInt()); | |
} | |
else if(req.substring(5,8)=="blu") | |
{ | |
String text = req.substring(9,15); | |
setBlue(text.toInt()); | |
} | |
else if(req.substring(5,8)=="fad") | |
{ | |
String text = req.substring(9,15); | |
fade_delay = text.toInt(); | |
} | |
else if (req.indexOf("getall") != -1) | |
{ | |
client.print(lastall); | |
client.stop(); | |
return; | |
} | |
else if (req.indexOf("getblue") != -1) | |
{ | |
client.print(lastblue); | |
client.stop(); | |
return; | |
} | |
else { | |
//Serial.println("invalid request"); | |
client.print("ERR"); | |
client.stop(); | |
return; | |
} | |
client.flush(); | |
// Send the response to the client | |
client.print("ok"); | |
delay(1); | |
//Serial.println("Client disonnected"); | |
return; | |
} | |
void WifiConnect() | |
{ | |
if(WiFi.status() == WL_CONNECTED) | |
return; | |
WiFi.begin(ssid, password); | |
Serial.println("(Re)connecting to wifi"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println(WiFi.localIP()); | |
// Start the server | |
server.begin(); | |
Serial.println("Server started"); | |
} | |
void setAll(int newval) | |
{ | |
if(newval<0) newval = 0; | |
if(newval>1023) newval = 1023; | |
if(newval>lastall) | |
{ | |
for(int i=lastall;i<newval;i++) | |
{ | |
analogWrite(pin_all,i); | |
delay(fade_delay); | |
} | |
lastall = newval; | |
return; | |
} | |
else if(newval<lastall) | |
{ | |
for(int i=lastall;i>=newval;i--) | |
{ | |
analogWrite(pin_all,i); | |
delay(fade_delay); | |
} | |
lastall = newval; | |
return; | |
} | |
} | |
void setBlue(int newval) | |
{ | |
if(newval<0) newval = 0; | |
if(newval>1023) newval = 1023; | |
if(newval>lastblue) | |
{ | |
for(int i=lastblue;i<newval;i++) | |
{ | |
analogWrite(pin_blue,i); | |
delay(fade_delay); | |
} | |
lastblue = newval; | |
return; | |
} | |
else if(newval<lastblue) | |
{ | |
for(int i=lastblue;i>=newval;i--) | |
{ | |
analogWrite(pin_blue,i); | |
delay(fade_delay); | |
} | |
lastblue = newval; | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment