ESP 8266 Relay Box - WiFi and Local on / off
Obliczymy ostateczną cenę na podstawie wybranego pliku. Sprawdzisz ją i zaakceptujesz przed płatnością.
Zapisane na tym urządzeniu.
ESP 8266 Relay Box - WiFi and Local on / off of mains through IEC male / female sockets.
- Ocena
- 0 / 5
- Polubienia
- 2
- Pobrania
- 26
- Wydruki
- 0
- Wyświetlenia
- 426
- Oceny
- 0
Aktualizacja u źródła:
Informacje o modelu
- Źródło
- Printables
- Kategoria
- Other Gadgets
- Licencja
- Creative Commons — Attribution — Noncommercial — Share Alike
I enjoy looking at things that others have designed and wanted a way to switch my QIDI Q1 Pro off or on, it being in a different part of the home.
Having played with computers and embedded electronics for the better part of the last 50 years, (yes FIFTY), looked like just the thing to use one of the ESP8266 modules I bought to muck about with during covid and my confinement due to underlying health issues!
All the parts can be purchased on Amazon.
The code for the ESP8266 was created in Arduino environment.
Relay module: https://www.amazon.co.uk/dp/B07TTVYGC8
ESP8266 Module: https://www.amazon.co.uk/Development-NodeMcu-ESP8266-Internet-Compatible/dp/B08QZ2887K
IEC Female Socket: https://www.amazon.co.uk/dp/B0BY3LXW1Y
IEC Male Socket: https://www.amazon.co.uk/dp/B07PY56LY7
For the Arduino code, lots of examples out there. You might want to use this one as a basis:
https://www.instructables.com/Easy-IOT-Remotely-Controlling-ESP32-Using-an-Andro-1/
https://www.amazon.co.uk/dp/B07ZCJYK6M, will be needed to connect from female socket to male of Q1 or other product using IEC style, “kettle plugs”.
Button switch: https://www.amazon.co.uk/dp/B0CNCPZSPF
Fastners used, all M3, 3mm.
Button switch will require a 10k ohm resistor pulling pin used to connect to button to 3.3 volts output on the board, the other side of the button switch connected to the board ground marked “G” on the board.
I used PETG for the build.
my Arduino sketch: (oops, I put code for the ESP01S module by mistake, corrected for D1 mini)
#include <ESP8266WiFi.h>
const char* ssid = "put your wifi id here"; // fill in here your router or wifi SSID, keep quotation marks
const char* password = "your wifi password"; // fill in here your router or wifi password
#define RELAY 16 // relay connected to GPIO0
const int touchPin = 0; // GPIO-2 for button switch
int touchState = LOW;
int lastTouchState = LOW;
int relayState = LOW;
boolean haveClient = false;
int value = LOW;
WiFiServer server(80);
void setup() {
Serial.begin(115200); // must be same baudrate with the Serial Monitor
pinMode(RELAY, OUTPUT);
pinMode(touchPin, INPUT);
digitalWrite(RELAY, LOW);
ESP.wdtEnable(10000);
delay(500);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(2000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
WiFi.setSleepMode(WIFI_NONE_SLEEP); //this seems to stop the hang
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print(WiFi.localIP());
Serial.println(" and all f'cked up by Michael.");
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
}
void loop() {
// Read the state of the touch switch
value = digitalRead(RELAY);
touchState = digitalRead(touchPin);
// Check if the touch switch is pressed
if (touchState == LOW && lastTouchState == HIGH) {
// if (touchState == LOW){
// Toggle the relay state
relayState = !relayState;
// Update the relay
digitalWrite(RELAY, relayState);
}
// Save the current state as the last state
lastTouchState = touchState;
// Small delay to debounce the switch
delay(50);
// Check if a client has connected
// WiFiClient client = server.available();
WiFiClient client = server.available();
if (client) {
haveClient = true;
} else {
haveClient = false;
}
if (haveClient && client.available()) {
// Wait until the client sends some data
Serial.println("new client");
delay(1);
// touchState = digitalRead(RELAY);
//* Serial.print("current relay state: ");
// Serial.println(touchState);*//
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/RELAY=ON") != -1) {
Serial.println("RELAY=ON");
digitalWrite(RELAY, HIGH);
value = HIGH; //should not need to set as set by read of realy pin
lastTouchState = HIGH;
}
if (request.indexOf("/RELAY=OFF") != -1) {
Serial.println("RELAY=OFF");
digitalWrite(RELAY, LOW);
value = LOW; //should not need to set value again
lastTouchState = LOW;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // this is a must
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>Q1 On/Off</title></head>");
client.print("Q1 is powered: ");
if (value == LOW) {
client.print("OFF");
} else {
client.print("ON");
}
client.println("<br><br>");
client.println("Turn <a href=\"/RELAY=OFF\">OFF</a> Q1<br>");
client.println("Turn <a href=\"/RELAY=ON\">ON</a> Q1<br>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
}