You are reading a single comment by @Ruprect99 and its replies. Click here to read the full conversation.
  • 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

About

Avatar for Ruprect99 @Ruprect99 started