Skip to content

Instantly share code, notes, and snippets.

@lsongdev
Created August 16, 2017 15:27
Show Gist options
  • Save lsongdev/92b50c41d3806ca8280a1aa8f3a8af4e to your computer and use it in GitHub Desktop.
Save lsongdev/92b50c41d3806ca8280a1aa8f3a8af4e to your computer and use it in GitHub Desktop.
#include <EEPROM.h>
#include <SSD1306.h>
#include <ESP8266mDNS.h>
#include <ESP8266SSDP.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define BTN_UP 12 //GPIO 12 = D6
#define BTN_DOWN 13 //GPIO 13 = D7
#define BTN_OK 14 //GPIO 14 = D5
#define BTN_FLASH 0 //GPIO 0 = FLASH BUTTON
#define BTN_RESET 4
#define LED_1 2
#define LED_2 16
const char *ssid = "[email protected]";
const char *password = "[email protected]";
SSD1306 display(0x3c, D1, D2);
ESP8266WebServer server(80);
void setup() {
pinMode(LED_1 , OUTPUT);
pinMode(LED_2 , OUTPUT);
pinMode(BTN_FLASH, INPUT);
pinMode(BTN_RESET, INPUT_PULLUP);
pinMode(BTN_DOWN , INPUT_PULLUP);
pinMode(BTN_OK , INPUT_PULLUP);
pinMode(BTN_UP , INPUT_PULLUP);
//
Serial.begin(115200);
Serial.println("\nStarting...\n");
//
EEPROM.begin(4096);
WiFi.mode(WIFI_STA);
WiFi.begin (ssid, password);
// Wait for connection
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
server.on ( "/", handleRoot );
server.on ( "/test.svg", drawGraph );
server.on ( "/inline", []() {
server.send ( 200, "text/plain", "this works as well" );
} );
server.on("/description.xml", HTTP_GET, [](){
SSDP.schema(server.client());
});
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "" );
Serial.print ( "Connected to " );
Serial.println ( ssid );
Serial.print ( "IP address: " );
Serial.println ( WiFi.localIP() );
Serial.println ( "HTTP server started" );
Serial.printf("Starting SSDP...\n");
SSDP.setSchemaURL("description.xml");
SSDP.setHTTPPort(80);
SSDP.setName("Philips hue clone");
SSDP.setSerialNumber("001788102201");
SSDP.setURL("index.html");
SSDP.setModelName("Philips hue bridge 2012");
SSDP.setModelNumber("929000226503");
SSDP.setModelURL("http://www.meethue.com");
SSDP.setManufacturer("Royal Philips Electronics");
SSDP.setManufacturerURL("http://www.philips.com");
SSDP.begin();
display.init();
display.flipScreenVertically();
display.setContrast(255);
// Align text vertical/horizontal center
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
display.setFont(ArialMT_Plain_10);
display.drawString(DISPLAY_WIDTH/2, DISPLAY_HEIGHT/2, "Hello world\n" + WiFi.localIP().toString());
display.display();
}
void loop() {
server.handleClient();
if(Serial.available()){
String input = Serial.readString();
}
if(LOW == digitalRead(BTN_OK)){
Serial.println ( "OK" );
}
digitalWrite(LED_2, digitalRead(BTN_OK));
}
void handleRoot() {
digitalWrite (LED_1, HIGH);
char temp[400];
int sec = millis() / 1000;
int min = sec / 60;
int hr = min / 60;
snprintf ( temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Demo</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Hello from ESP8266!</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", hr, min % 60, sec % 60
);
server.send ( 200, "text/html", temp );
digitalWrite (LED_1, LOW);
}
void drawGraph() {
String out = "";
char temp[100];
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"black\">\n";
int y = rand() % 130;
for (int x = 10; x < 390; x+= 10) {
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}
out += "</g>\n</svg>\n";
server.send ( 200, "image/svg+xml", out);
}
void handleNotFound() {
digitalWrite ( LED_1, HIGH );
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_1, LOW );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment