RemoteRelay icon indicating copy to clipboard operation
RemoteRelay copied to clipboard

4 channel relay requires AT command responses

Open crstrand opened this issue 4 years ago • 4 comments

The 4 channel relay available on AliExpress comes with firmware on the nuvoTon chip on the PCB that expects the ESP01 module to respond to AT commands, but this app does not. If you add some simple code to lie to the nuvoTon, then it thinks the module is operating as it expects and will then accept the binary commands to switch the relays. IF the nuvoTon chip does not get a repsonse to the AT commands, it will not do anything when it receives the switching command bytes. This solution was concocted after monitoring the communication between the ESP01 module and the nuvoTon chip on the relay module with a couple of USB-serial adapters.

  // pretend to be an AT device here
  if(Serial.available())
  {
    stringIn=Serial.readStringUntil('\r');
    Serial.flush();// flush what's left '\n'?
    if(stringIn!="")
    {
      if(stringIn.indexOf("AT+")>-1)
        Serial.println("OK");
      if(stringIn.indexOf("AT+RST")>-1)
      {
        // pretend we reset (wait a bit then send the WiFi connected message)
        delay(1);
        Serial.println("WIFI CONNECTED\nWIFI GOT IP");
      }
    }
  }

image image

On boot nuvoTon sends:

AT+CWMODE=1
AT+CWMODE=1
AT+RST

RemoteRelay+ responds with "OK" to every AT command and this to the AT+RST command

WIFI CONNECTED
WIFI GOT IP

Once the nuvoTon sees WiFi is connected, it thinks its going to start the webserver with:

AT+CIPMUX=1
AT+CIPSERVER=1,8080
AT+CIPSTO=360

Now it will accept the 4 byte commands to close and open all four relays. You'll have to add code for the extra two channels, but its a bit of easy copy and paste.

crstrand avatar Jun 20 '21 02:06 crstrand

Thanks for the patch ! I'll try to source one of these board to test it.

nagius avatar Dec 29 '21 17:12 nagius

I think I got that module, but it behaves slightly different: In CWMODE=2 (red led), nuvoTon doesn't wait for "WIFI CONNECTED". In CWMODE=1 (blue led), it does. From the manual LC-Relay-ESP01-4R-12.pdf page 2: "LED D5(blue):indicator for mode 2" (so CWMODE and described modes are swapped).

I'm quite sure about: CWMODE=2 means hotspot mode. CWMODE=1 means client mode.

The reaction to "WIFI GOT IP" happens even without sending a line terminator. "WIFI CONNECTED" can be omitted. Going to implement all of this in my fork.

pucgenie avatar Feb 27 '22 21:02 pucgenie

This code has been committed in bbbd766b365dca6efd31f158d4e7b1ed99b4d9dd Does that fixes all your issues ?

nagius avatar Apr 16 '22 17:04 nagius

It works since bbbd766, sorry for the confusion. I just wanted to document the slightly different behaviour in comparison to that described by crstrand. Seems there are multiple nuvoTon firmwares in the wild; some of them more tolerant to "junk" serial commands than others.

pucgenie avatar Apr 16 '22 20:04 pucgenie