Chat freely about anything...

User avatar
By Janosik
#95547 Hi all,

I recently bought a ESP8266 WIFI 4 Channel Relay Module (this one).
I am using the ARDUINO IDE and a USB to TTL board to write and upload programs to the ESP.
So far, so good, but...
I can't find how to turn on or off the relays...
The technical information says this:
Relay control command(Hexadecimal):
Turn OFF the first relay : A0 01 00 A1
Turn ON the first relay : A0 01 01 A2
Turn OFF the second relay : A0 02 00 A2
Turn ON the second relay : A0 02 01 A3
Turn OFF the third relay : A0 03 00 A3
Turn ON the third relay : A0 03 01 A4
Turn OFF the fourth relay : A0 04 00 A4
Turn ON the fourth relay : A0 04 01 A5

but I have no idea what this means or how to do this.

The code below will create a WiFiServer where you can access an html-page.
It contains a button to turn ON the BUILDIN_LED. A second button will turn it OFF again.
This all works fine...
Now I need to change line 23 and 27 to controll the relay.
Does anybody know how?

Thanks ;-)
Janosik.

Code: Select all#include <ESP8266WiFi.h>
const char* ssid="******";
const char* password="******";
const int PIN=1;
WiFiServer server(8080);

void setup(){
  pinMode(PIN,OUTPUT);
  digitalWrite(PIN,HIGH);
  WiFi.begin(ssid,password);
  while(WiFi.status()!=WL_CONNECTED){delay(500);}
  server.begin();
}

void loop(){
  String request,receivedCommand;
  WiFiClient client=server.available();
  if(!client){return;}
  while(!client.available()){delay(1);}
  request=client.readStringUntil('\r');
  client.flush();
  if(request.indexOf("?RELAY1ON")!=-1){
    receivedCommand="1 on";     //turn ON relay1
    digitalWrite(PIN,LOW);
  }
  if(request.indexOf("?RELAY1OFF")!=-1){
    receivedCommand="1 off";    //turn OFF relay1
    digitalWrite(PIN,HIGH);
  }
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.println("<html>");
  client.println("<body>");
  client.print("PIN = ");
  client.println(PIN);
  client.print("<br>Received command = ");
  client.println(receivedCommand);
  client.println("<br><a href='/?RELAY1ON'><button class='button'>RELAY1 ON</button></a>");
  client.println("<br><a href='/?RELAY1OFF'><button class='button'>RELAY1 OFF</button></a>");
  client.println("</body>");
  client.println("</html>");
  delay(1);
}
User avatar
By Kyle McCahon
#95619 try this.
remove all your Serial.println commands as this will override the write command

Code: Select allbyte relON1[] = { 0xA0, 0x01, 0x01, 0xA2 };   //Hex command to send to serial for open relay
byte relOFF1[] = { 0xA0, 0x01, 0x00, 0xA1 };  //Hex command to send to serial for close relay

byte relON2[] = { 0xA0, 0x02, 0x01, 0xA3 };   //Hex command to send to serial for open relay
byte relOFF2[] = { 0xA0, 0x02, 0x00, 0xA2 };  //Hex command to send to serial for close relay


void openRelay1()
  {
    Serial.write(relON1, sizeof(relON1));     // turns the relay ON
  }

void closeRelay1()
  {
    Serial.write(relOFF1, sizeof(relOFF1));     // turns the relay ON
  }