Created
October 16, 2018 15:55
-
-
Save badvision/a129a240084c65b6c2418f86bb9ed290 to your computer and use it in GitHub Desktop.
ESP32 Webserver Example for OLED
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 <string> | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <WebServer.h> | |
#include <ESPmDNS.h> | |
#include "SSD1306.h" | |
#define SDA 4 | |
#define SCL 15 | |
#define RST 16 //RST must be set by software | |
#define DISPLAY_HEIGHT 64 | |
#define DISPLAY_WIDTH 128 | |
const char *ssid = "YOUR-SSID-HERE"; | |
const char *password = "YOUR-PASSWORD-HERE"; | |
WebServer server(80); | |
SSD1306 display(0x3c, SDA, SCL, RST); | |
const int led = 2; // LED will blink when there is activity | |
void handleRoot() { | |
digitalWrite(led, 1); | |
char temp[500]; | |
int sec = millis() / 1000; | |
int min = sec / 60; | |
int hr = min / 60; | |
snprintf(temp, 500, "<html><head><title>ESP32</title><style>\ | |
body {background-color:#cccccc; font-family:Arial,Helvetica,Sans-Serif; Color:#000088;}\ | |
div {display:inline-block; width:71px;}\ | |
</style></head><body>\ | |
<p>Uptime: %02d:%02d:%02d</p>\ | |
<script>\ | |
for(var i=0;i<40;i++){document.write('<div>Pin '+i+'<input type=\"checkbox\" onclick=\"setPin('+i+',this);\"></div>');}\ | |
function setPin(n,v){var req=new XMLHttpRequest(),u='/pin?num='+n+'&val='+(v.checked?1:0);req.open('GET',u);req.send();}\ | |
</script>\ | |
</body>\ | |
</html>", | |
hr, min % 60, sec % 60); | |
server.send(200, "text/html", temp); | |
digitalWrite(led, 0); | |
} | |
void setPin() { | |
char temp[41]; | |
int pinNum = server.arg("num").toInt(); | |
int newState = server.arg("val").toInt(); | |
snprintf(temp, 41, "<html><body>Pin %02d state: %1d</body></html>", pinNum, newState); | |
server.send(200, "text/html", temp); | |
digitalWrite(led, 0); | |
pinMode(pinNum, OUTPUT); | |
digitalWrite(pinNum, newState); | |
} | |
void handleNotFound() { | |
digitalWrite(led, 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, 0); | |
} | |
void initScreen() { | |
display.init(); | |
display.flipScreenVertically(); | |
display.setContrast(255); | |
display.setLogBuffer(5, 30); | |
} | |
void setup(void) { | |
initScreen(); | |
pinMode(led, OUTPUT); | |
digitalWrite(led, 0); | |
Serial.begin(115200); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
display.clear(); | |
// Print to the screen | |
display.print("Trying: "); | |
display.println(ssid); | |
display.drawLogBuffer(0, 0); | |
// Display it on the screen | |
display.display(); | |
delay(500); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
display.clear(); | |
// Print to the screen | |
display.println("----------"); | |
display.print("Connected to "); | |
display.println(ssid); | |
display.print("IP: "); | |
display.println(WiFi.localIP()); | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
if (MDNS.begin("esp32")) { | |
Serial.println("MDNS responder started"); | |
display.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/pin", setPin); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
display.println("HTTP server started"); | |
// Draw it to the internal screen buffer | |
display.drawLogBuffer(0, 0); | |
// Display it on the screen | |
display.display(); | |
} | |
void loop(void) { | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment