Skip to content

Instantly share code, notes, and snippets.

@Sasszem
Created March 17, 2021 12:47
Show Gist options
  • Save Sasszem/092682d149ff1fc905a6e28c5cff0786 to your computer and use it in GitHub Desktop.
Save Sasszem/092682d149ff1fc905a6e28c5cff0786 to your computer and use it in GitHub Desktop.
Minimal example that shows tne basics of remote-controlled electornics
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "MY-SSID"
#define STAPSK "PASSWORD"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = LED_BUILTIN;
void handleRoot() {
server.send(200, "text/plain", "hello from esp8266!");
}
void handleNotFound() {
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);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 1);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/led/on", []() {
server.send(200, "text/plain", "OK");
Serial.println("/led/on");
digitalWrite(led, LOW);
// there's an inverter before the LED
});
server.on("/led/off", []() {
server.send(200, "text/plain", "OK");
Serial.println("/led/off");
digitalWrite(led, HIGH);
// there's an inverter before the LED
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment