Skip to content

Instantly share code, notes, and snippets.

@paltaio-admin
Created August 5, 2024 00:02
Show Gist options
  • Select an option

  • Save paltaio-admin/b9e52061a941b0d516c06865beaddafc to your computer and use it in GitHub Desktop.

Select an option

Save paltaio-admin/b9e52061a941b0d516c06865beaddafc to your computer and use it in GitHub Desktop.
Simple ESP01 Relay demo
<!-- data/index.html -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
user-select: none;
}
body {
font-family: sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100dvh;
background-color: #1f2937;
}
form {
display: flex;
justify-content: center;
align-items: center;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
user-select: none;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<form action="/relay" method="post">
<button type="submit">Trigger relay</button>
</form>
<script>
document.querySelector('form').addEventListener('submit', async (e) => {
e.preventDefault();
await fetch(e.target.action, {
method: e.target.method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
});
});
</script>
</body>
</html>
#include <Arduino.h>
#include <LittleFS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <AsyncTimer.h>
const char* ssid = "ESP01-Relay";
const char* password = "some really secure password";
AsyncTimer t;
AsyncWebServer server(80);
std::string hexToBytes(const std::string& hex) {
std::string bytes;
for (unsigned int i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2);
char byte = (char) strtol(byteString.c_str(), NULL, 16);
bytes += byte;
}
return bytes;
}
// Handle form submission
void handleRelay(AsyncWebServerRequest *request) {
// Trigger relay
std::string onCommand = hexToBytes("A00101A2"); // Relay on
Serial.write(onCommand.c_str(), onCommand.size());
t.setTimeout([]() {
std::string offCommand = hexToBytes("A00100A1"); // Relay off
Serial.write(offCommand.c_str(), offCommand.size());
}, 200);
// Send JSON response
JsonDocument doc;
doc["status"] = "ok";
String response;
serializeJsonPretty(doc, response);
request->send(200, "application/json", response);
}
void setup() {
// Initialize Serial
Serial.begin(9600);
Serial.write(hexToBytes("0").c_str());
// Initialize LittleFS
if (!LittleFS.begin()) {
Serial.println("An error has occurred while mounting LittleFS");
return;
}
// Start AP
WiFi.softAP(ssid, password);
WiFi.softAPConfig(IPAddress(10, 0, 0, 1), IPAddress(0, 0, 0, 0), IPAddress(255, 255, 255, 0));
Serial1.println("Access Point started");
Serial1.print("IP address: ");
Serial1.println(WiFi.softAPIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(LittleFS, "/index.html", "text/html");
});
server.on("/relay", HTTP_POST, handleRelay);
server.begin();
t.setTimeout([]() {
std::string offCommand = hexToBytes("A00100A1"); // Relay off
Serial.write(offCommand.c_str(), offCommand.size());
}, 100);
}
void loop() {
t.handle();
}
[env:esp01]
platform = espressif8266
board = esp01
board_build.filesystem = littlefs
upload_protocol = esptool
framework = arduino
monitor_speed = 115200
monitor_rts = 0
monitor_dtr = 0
monitor_echo = yes
monitor_eol = LF
monitor_filters =
send_on_enter
colorize
lib_deps =
ESPAsyncTCP-esphome
ESPAsyncWebServer-esphome
ArduinoJson
aasim-a/AsyncTimer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment