Skip to content

Instantly share code, notes, and snippets.

@mikbuch
Last active July 30, 2022 11:30
Show Gist options
  • Save mikbuch/212cfb7365703caf517aa83642d10d91 to your computer and use it in GitHub Desktop.
Save mikbuch/212cfb7365703caf517aa83642d10d91 to your computer and use it in GitHub Desktop.
ESP8266 microchip is used to host a web server. This way we can control ESP's LED diode by turning it on or off with a web request, e.g., "http://192.168.0.16/switch".
// This script is avilable as gist:
// https://gist.github.com/mikbuch/212cfb7365703caf517aa83642d10d91
/* Requirements:
* * "esp8266" board installed.
*
* In order to install the board, go to:
* -- Tools => Board => Boards Manager => Install "esp8266"
* It also includes "Wemos" boards ("LOLIN").
*
* Then you set the board as:
* -- Tools => Board => ESP8266 => Boards LOLIN (Wemos) D1 lite
*
* Now you can complie and upload the code to your Wemos board.
*/
/* Connection status codes ("WiFi.status()"):
* 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
* 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached
* 3 : WL_CONNECTED after successful connection is established
* 4 : WL_CONNECT_FAILED if connection failed
* 6 : WL_CONNECT_WRONG_PASSWORD if password is incorrect
* 7 : WL_DISCONNECTED if module is not configured in station mode
*
* Source: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#diagnostics
*/
/* Instructon on how to use this example on the client side.
*
* The client side means that you will be sending requests
* to the server hosted at ESP8266, and you will be receiving
* requests.
*
* Use a root request (e.g., http://192.168.0.22/), or either of
* the two endpoints:
* (1) /switch (to switch diode), or
* (2) /status (to get the info about diode status: on or off).
* For example: http://192.168.0.22/status
* ----------------------------------------------------------------------
*
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
/* SSID is the name of your WiFi network, e.g.: "linksys", or "ab8402932".
* You have to set the variable STASSID below.
* --
* STAPSK is the password you your WiFi (WLAN) network, i.e., the password
* to your router. This variable has to be set as well.
*
* To sum up, the 4 lines below this block comment should look shomehow
* like that:
*
* #ifndef STASSID
* #define STASSID "linksys"
* #define STAPSK "@sdjj2325k^#asd"
* #endif
*/
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
int led_status = 0;
void handleRoot() {
Serial.println("Received a root (`/`) request.");
Serial.println("LED status is: " + String(led_status));
Serial.println("----------------------------");
// Server response
String message = "Use either of the two endpoints:\n";
message += " (1) /switch (to switch diode)\n";
message += " (2) /status (to get the info about diode status: on or off)\n";
message += "For example: 192.168.0.22/status\n";
message += "----------------------------\n";
message += "LED status is now: " + String(led_status) + "\n";
message += "----------------------------";
server.send(200, "text/plain", message);
}
void handleLedSwitch() {
Serial.println("Received a `switch` request.");
if (led_status == 0) {
digitalWrite(LED_BUILTIN, LOW);
led_status = 1;
} else {
digitalWrite(LED_BUILTIN, HIGH);
led_status = 0;
}
Serial.println("LED status is: " + String(led_status));
Serial.println("----------------------------");
// Server response
String message = "Switched LED!\n\n";
message += "Now status of the diode is: ";
message += String(led_status);
server.send(200, "text/plain", message);
}
void handleLedStatus() {
Serial.println("Received a `status` request.");
Serial.println("LED status is: " + String(led_status));
Serial.println("----------------------------");
// Server response
String message = "Status of the LED is: ";
message += String(led_status);
server.send(200, "text/plain", message);
}
void handleNotFound() {
digitalWrite(LED_BUILTIN, LOW);
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_BUILTIN, HIGH);
}
/* =======================================
*
* SETUP
*
* =======================================
*/
void setup(void) {
// Reset the serial monitor at the beginning.
Serial.flush();
// Initial variables and setup
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
/* Wait for the connection
*
* Connection status codes:
* 0 : WL_IDLE_STATUS when Wi-Fi is in process of changing between statuses
* 1 : WL_NO_SSID_AVAILin case configured SSID cannot be reached
* 3 : WL_CONNECTED after successful connection is established
* 4 : WL_CONNECT_FAILED if connection failed
* 6 : WL_CONNECT_WRONG_PASSWORD if password is incorrect
* 7 : WL_DISCONNECTED if module is not configured in station mode
* Source: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#diagnostics
*/
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.print(WiFi.status());
}
Serial.println("");
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("/switch", handleLedSwitch);
server.on("/status", handleLedStatus);
server.on("/gif", []() {
static const uint8_t gif[] PROGMEM = {
0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
};
char gif_colored[sizeof(gif)];
memcpy_P(gif_colored, gif, sizeof(gif));
// Set the background to a random set of colors
gif_colored[16] = millis() % 256;
gif_colored[17] = millis() % 256;
gif_colored[18] = millis() % 256;
server.send(200, "image/gif", gif_colored, sizeof(gif_colored));
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
Serial.println("----------------------------");
}
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