Skip to content

Instantly share code, notes, and snippets.

@r3wald
Created June 24, 2016 00:20
Show Gist options
  • Save r3wald/1628240e22ffa40ca107ecec10326b9b to your computer and use it in GitHub Desktop.
Save r3wald/1628240e22ffa40ca107ecec10326b9b to your computer and use it in GitHub Desktop.
Simple arduino sketch for YISON ESP-01/ESP-202 (ESP-8266)
#include <ESP8266WiFi.h>
const int LED_RED = 12; // multi color red
const int LED_BLUE = 13; // multi color blue
const int LED_GREEN = 15; // multi color green
const int LED_WLAN = 14; // on board 2
const int LED_SERVER = 16; // on board 1
const char* ssid = "";
const char* password = "";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void color(int r, int g, int b) {
digitalWrite(LED_RED, r);
digitalWrite(LED_GREEN, g);
digitalWrite(LED_BLUE, b);
}
void wlanStatusBlink() {
digitalWrite(LED_WLAN, 0);
delay(10);
digitalWrite(LED_WLAN, 1);
}
void wlanStatusOk() {
digitalWrite(LED_WLAN, 0);
}
void serverStatusBlink() {
digitalWrite(LED_SERVER, 1);
delay(10);
digitalWrite(LED_SERVER, 0);
}
void setup() {
pinMode(LED_RED, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_WLAN, OUTPUT);
pinMode(LED_SERVER, OUTPUT);
Serial.begin(115200);
wlanStatusBlink();
Serial.print("Connecting");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
wlanStatusBlink();
delay(500);
Serial.print(".");
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("Server started");
digitalWrite(LED_WLAN, 0);
}
int green = 0;
int blue = 0;
int red = 0;
void loop() {
serverStatusBlink();
// 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);
client.flush();
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nLED is now ";
if (req.indexOf("/red") != -1) {
s += "red";
color(1, 0, 0);
} else if (req.indexOf("/green") != -1) {
s += "green";
color(0, 1, 0);
} else if (req.indexOf("/blue") != -1) {
s += "blue";
color(0, 0, 1);
} else {
s += "off";
color(0, 0, 0);
}
s += "</html>\n";
// Send the response to the client
client.print(s);
client.flush();
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