Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. ESP8266 + Radio + Relay Working Fine

ESP8266 + Radio + Relay Working Fine

Scheduled Pinned Locked Moved My Project
2 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Newzwaver
    wrote on last edited by
    #1

    HI Everyone,

    I noticed that I couldn't find a relay working with ESP8266MOD, I was messing with it over the last few days and figured out that the following sketch works. The big fix was in the sketch you use pin 0 for the relay and actually attach it to pin 3. This works great with WEMOS ESP8266.

    '''
    // Enable debug prints to serial monitor
    #define MY_DEBUG
    #define MY_NODE_ID 97 //esp8266relay
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69

    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE

    #include <MySensors.h>

    #define RELAY_1 0 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay

    void before()
    {
    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
    }
    }

    void setup()
    {

    }

    void presentation()
    {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relay", "1.0");

    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    	// Register all sensors to gw (they will be created as child devices)
    	present(sensor, S_BINARY);
    }
    

    }

    void loop()
    {

    }

    void receive(const MyMessage &message)
    {
    // We only expect one type of message from controller. But we better check anyway.
    if (message.type==V_STATUS) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
    // Store state in eeprom
    saveState(message.sensor, message.getBool());
    // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    }
    }
    ""

    mfalkviddM 1 Reply Last reply
    0
    • N Newzwaver

      HI Everyone,

      I noticed that I couldn't find a relay working with ESP8266MOD, I was messing with it over the last few days and figured out that the following sketch works. The big fix was in the sketch you use pin 0 for the relay and actually attach it to pin 3. This works great with WEMOS ESP8266.

      '''
      // Enable debug prints to serial monitor
      #define MY_DEBUG
      #define MY_NODE_ID 97 //esp8266relay
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69

      // Enable repeater functionality for this node
      #define MY_REPEATER_FEATURE

      #include <MySensors.h>

      #define RELAY_1 0 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 1 // Total number of attached relays
      #define RELAY_ON 1 // GPIO value to write to turn on attached relay
      #define RELAY_OFF 0 // GPIO value to write to turn off attached relay

      void before()
      {
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
      // Then set relay pins in output mode
      pinMode(pin, OUTPUT);
      // Set relay to last known state (using eeprom storage)
      digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
      }

      void setup()
      {

      }

      void presentation()
      {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay", "1.0");

      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
      	// Register all sensors to gw (they will be created as child devices)
      	present(sensor, S_BINARY);
      }
      

      }

      void loop()
      {

      }

      void receive(const MyMessage &message)
      {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
      // Change relay state
      digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
      // Store state in eeprom
      saveState(message.sensor, message.getBool());
      // Write some debug info
      Serial.print("Incoming change for sensor:");
      Serial.print(message.sensor);
      Serial.print(", New status: ");
      Serial.println(message.getBool());
      }
      }
      ""

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      @Newzwaver I think that makes sense. D3 on Wemos D1 Minis is GPIO0. I usually use the Dn notation, because I find it easier to understand.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      16

      Online

      11.7k

      Users

      11.2k

      Topics

      113.1k

      Posts


      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • MySensors
      • OpenHardware.io
      • Categories
      • Recent
      • Tags
      • Popular