LCTech relay control via Espurino

Posted on
  • Hi,

    I'm trying to write some simple code to toggle a esp8266 based relay.

    According to all the docs I've seen (and according to this post https://www.esp8266.com/viewtopic.php?p=66327) all one needs to do is send a series of hex codes out over the serial port to toggle the switch.

    So I've written a very simple bit of code that I think should work, but I'm not overly familar with sending serial data.

    function onInit() {
    
      Serial1.setup(9600);
    
      var relayOn = [0xA0, 0x01, 0x01, 0xA2];
      var relayOff = [0xA0, 0x01, 0x00, 0xA1];
    
      var toggle = true;
      setInterval(function() {
        if (toggle) {
          console.log('on');
          Serial1.write(relayOn);
          toggle = true;
        } else {
          console.log('off');
          Serial1.write(relayOff);
          toggle = false;
        }
      }, 5000);
    }
    

    Is there any obvious flaw in this code as its not working. I can see the blue led flash every 5 seconds as it sends the data, but is this the correct way to send a set of hex instructions?

    Thanks

  • I've since tried sending the hex directly to the board using my PC and still getting nothing, so I think its something up with the board rather than the espurino code.

  • So you have this module?

    https://www.hackster.io/makerrelay/esp8266-wifi-5v-1-channel-relay-delay-module-iot-smart-home-e8a437

    Are you running Espruino on the module itself, or do you have Espruino running elsewhere and you're trying to send data to it?

    If you've flashed Espruino onto the module itself then yeah, what you're doing sounds ok - you could try sending just the String "A00101A2" just in case that was it?

    However, the problem is that when the ESP8266 module is working as it does normally as seems to be described in that link above, it won't just output that hex code - there's some other stuff too. Something along the lines of +IPD0,4:\xA0\x01\x01\xA2

    To be sure, you'd probably have to flash the normal AT command firmware, get it working, then connect your PC and figure out exactly what got sent.

  • I have the 2 channel version of it.

    Here is some code that is supposed to work on it.

    https://github.com/mischkasmith/lctech-2ch-relay/blob/master/lctech2ch.ino

    I'm running the code on the ESP8266 that is on the device, so in theory I should just power the device, and it should load the code, and start switching the relay. I just wanted to do a simple test before doing anything else to ensure I could control it.

    I did connect an FTDI to is while it was running and it did seem to be sending more that just those commands to the out. basically anything outputted to the console was getting sent too. Is there a way to redirect the console outputs to another channel that I can observe, whilst keeping the serial1 channel just for the comms to the module?

    I did also try just sending the commands directly from my pc using some node js code, but I still couldn't get it to work at all.

    I have a second relay module so might try that incase there is a fault with that one, but it just seemed very unresponsive.

    Next thing will be to get it working as per the original spec as you suggest to make sure the board actually works!

  • Is there a way to redirect the console outputs to another channel that I can observe, whilst keeping the serial1 channel just for the comms to the module?

    Yes, just use LoopbackA.setConsole() and then add whatever listener you want with LoopbackB.on('data', ...) - or just replace LoopbackA with whatever output device you want the JS console to appear on.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

LCTech relay control via Espurino

Posted by Avatar for Ruprect99 @Ruprect99

Actions