screamlt wrote:JanAage wrote:I did never try it with the provided software as intended by the documentation.
I programmed the ESP to my own needs, control it over web with Blynk http://www.blynk.cc.
You can have a look at the attached file and this site:
https://www.hackster.io/makerrelay/esp8266-wifi-5v-1-channel-relay-delay-module-iot-smart-home-e8a437
hello,
can you post a sketch, or PM me with sketch please, because i spend 2 days, but still can make it working with Blynk. Thank You.
Hi
how did you control it with blynk?
which network is running? AP?
which firmware on the esp?
thanks
hkusp wrote:Ienuzza wrote:I have a similar model but at 12 volts, even the relay is 12V, do I have to change the resistance values? Thank you
The only 12V ESP-01 relay module I see on Aliexpress doesn't look anything like the 5V modules. The modification I outlined above won't work.
I would suggest to diagram out where GPIO0 and GPIO2 go. I suspect they are not tied to VCC or float, which will cause a booting problem.
Like the 5V versions, I'm going to "guess" the Tx and Rx lines go to a microcontroller that then drives a NPN transistor or an N-channel mosfet.
I'm afraid you will have to reverse engineer and diagram out the board.
too complicated for a cheap module! I give up! Tnx
I spliced a couple codes together and did a bit of editing. It can take it a couple minutes for everything to link up after bootup or reboot (Could be code errors? Wifi related? 2 processors need time to sync up??).
So far been working nicely in my first few hours of use. Leaving them run all night , several reboots and reboots of the router and all is still working great.
I want to shout out a big Thanks to all of you for giving me a place to start. Also thanks to Github for the wonderful Websocket code that always keeps the state refreshed, although I don't know exactly how accurate it really is considering its not getting an actual reading from the onboard processor to the relay, but hey.....
My next step is to link these to a single ESP8266 and use that to put up a webpage to my router linking to the Relay boards on one webpage, that way I don't have to port a bunch of ESP's and open different webpages to activate them over the internet. One single connection to my router sounds nice .
Hopefully no code errors, but I am an amateur at best and this was 2 or 3 codes combined and a few modifications for adjustments. I honestly could not tell you how this code works line for line but after a few months I have at least gotten this relay to work with no physical adjustments to the board. Be sure to check the port settings around line 30, 50, and 200 as well and adjust as necessary for your Router/webpage. Look for comments
/*
* ESP8266 Web server with Web Socket to control an LC Technology Relay Board.
*
* References:
*
* https://github.com/Links2004/arduinoWebSockets
* http://www.esp8266.com/viewtopic.php?f=160&t=13164&start=16
* http://www.esp8266.com/viewtopic.php?f=160&t=13164&start=16
*/
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <Hash.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
static const char ssid[] = "********"; ////enter Network Router wifi broadcast SSID here, keep ""
static const char password[] = "*****"; ///Router Wifi Password here, keep ""
MDNSResponder mdns;
ESP8266WiFiMulti WiFiMulti;
///////If needed, Open Both of the ports below in your network wifi router port forwarding///////////////////
ESP8266WebServer server(80); //Usually 80///////////
WebSocketsServer webSocket = WebSocketsServer(81); //Usually 81///////////
byte relON[] = {0xA0, 0x01, 0x01, 0xA2}; //Hex command to send to onboard serial microprocessor for open relay
byte relOFF[] = {0xA0, 0x01, 0x00, 0xA1}; //Hex command to send to serial for close relay
void handleRoot(); // function prototypes for HTTP handlers
void handleLED();
void handleNotFound();
int ledState = false;
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0">
<title>LC Technology Relay Board</title>
<style>
</style>
<script>
var websock;
function start() {
websock = new WebSocket('ws://' + window.location.hostname + ':81/'); ////////////Usually 81///////////////
websock.onopen = function(evt) { console.log('websock open'); };
websock.onclose = function(evt) { console.log('websock close'); };
websock.onerror = function(evt) { console.log(evt); };
websock.onmessage = function(evt) {
console.log(evt);
var e = document.getElementById('ledstatus');
if (evt.data === 'ledon') {
e.style.color = 'black';
}
else if (evt.data === 'ledoff') {
e.style.color = 'red';
}
else {
console.log('unknown event');
}
};
}
function buttonclick(e) {
websock.send(e.id);
}
</script>
</head>
<body onload="javascript:start();">
<h1>ESP8266 LC Technology Relay Board</h1>
<div id="ledstatus" <b style="font-size: 35px;">Relay</b> </div>
<button id="ledon" type="button" onclick="buttonclick(this);" style="width:150px; height: 50px; font-size: 40px;">Off</button>
<button id="ledoff" type="button" onclick="buttonclick(this);" style="width:150px; height: 50px; font-size: 40px; color: red;">On</button>
</body>
</html>
)rawliteral";
// Current LED status
bool LEDStatus;
// Commands sent through Web Socket
const char LEDON[] = "ledoff";
const char LEDOFF[] = "ledon";
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
Serial.printf("webSocketEvent(%d, %d, ...)\r\n", num, type);
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\r\n", num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\r\n", num, ip[0], ip[1], ip[2], ip[3], payload);
// Send the current LED status
if (LEDStatus) {
webSocket.sendTXT(num, LEDON, strlen(LEDON));
}
else {
webSocket.sendTXT(num, LEDOFF, strlen(LEDOFF));
}
}
break;
case WStype_TEXT:
Serial.printf("[%u] get Text: %s\r\n", num, payload);
if (strcmp(LEDON, (const char *)payload) == 0) {
Serial.write(relON, sizeof(relON)); // turns the relay ON
}
else if (strcmp(LEDOFF, (const char *)payload) == 0) {
Serial.write(relOFF, sizeof(relOFF)); // turns the relay OFF
}
else {
Serial.println("Unknown command");
}
// send data to all connected clients
webSocket.broadcastTXT(payload, length);
break;
case WStype_BIN:
Serial.printf("[%u] get binary length: %u\r\n", num, length);
hexdump(payload, length);
// echo data back to browser
webSocket.sendBIN(num, payload, length);
break;
default:
Serial.printf("Invalid WStype [%d]\r\n", type);
break;
}
}
void handleRoot()
{
server.send_P(200, "text/html", INDEX_HTML);
}
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()
{
Serial.begin(9600);
//Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] BOOT WAIT %d...\r\n", t);
Serial.flush();
delay(1000);
}
WiFiMulti.addAP(ssid, password);
while(WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("espWebSock", WiFi.localIP())) {
Serial.println("MDNS responder started");
mdns.addService("http", "tcp", 80); ////////////Usually port 80////////////
mdns.addService("ws", "tcp", 81); ////////////Usually port 81////////////
}
else {
Serial.println("MDNS.begin failed");
}
Serial.print("Connect to http://espWebSock.local or http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop()
{
webSocket.loop();
server.handleClient();
}