WeMos D1 Mini Gateway + Relay



  • Hello all,

    I'm attempting to add a single relay to my WeMos D1 Mini gateway. I have my nrf24 wired according to the instructions on the connecting the radios page and I currently have the relay attached to D1/GPIO5.

    I'm certain the gateway is receiving messages for the switch, I can see them in the Arduino Serial Monitor with debug enabled.

    I'm certain the relay is functional, it clicks on and off while uploading firmware to the device.

    I suspect I have the relay_pin mapped incorrectly, but I've tried several different variations with no success. I also suspect that I may be using a protected or special pin, but I've seen others on this forum using D1, so I'm not sure.

    Here's the Sketch, superfluous code/comments removed.

    #define MY_DEBUG
    #define MY_BAUD_RATE 9600
    #define MY_RADIO_RF24
    #define MY_GATEWAY_ESP8266
    #define MY_WIFI_SSID "WiFiNetwork"
    #define MY_WIFI_PASSWORD "SuperSecretPassword"
    #define MY_HOSTNAME "MySensors_GW"
    #define MY_PORT 5003
    #define MY_GATEWAY_MAX_CLIENTS 255
    #define MY_NODE_ID 0
    #define CHILD_ID_RELAY 2
    
    #include <MyConfig.h>
    #include <MySensors.h>
    
    #define RELAY_PIN 1
    #define RELAY_ON 0
    #define RELAY_OFF 1
    
    MyMessage msgRelay(CHILD_ID_RELAY, V_STATUS);
    
    void setup() {
      pinMode(RELAY_PIN, OUTPUT);
      pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
      digitalWrite(RELAY_PIN, RELAY_ON);
    }
    
    void presentation() {
      sendSketchInfo("Attic GW", "0.2");
      present(CHILD_ID_RELAY, S_BINARY, "Switch");
      send(msgRelay.set(0));
    }
    
    void loop() {
      wait(3);
      digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    }
    
    void receive(const MyMessage &message) {
      digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
      if (message.type == V_STATUS) {
        if (message.getBool() == 1) {
          digitalWrite(RELAY_PIN, RELAY_OFF);
          wait(5000);
          digitalWrite(RELAY_PIN, RELAY_ON);
          wait(100);
        }
        send(msgRelay.set(0));
      }
    }
    

    As always, comments/suggestions appreciated.


  • Mod

    @anderBAKE are you sure the code to actually toggle the relay gets executed? You could add some debug statements or toggle the led instead.
    Btw. I would prefer

    if (message.getBool() == true)
    

    Or even

    if (message.getBool())
    


  • @Yveaux Thank you for the reply - at the least, it gave me the chance to reexamine the sketch I posted previously. In an effort to clean things up, I broke the functionality. Relay still doesn't work though. Here is the corrected sketch with output from the Serial Monitor.

    I realize how frustrating it is when the code posted doesn't accurately reflect what's being used - sorry about that.

    #define MY_DEBUG
    #define MY_BAUD_RATE 9600
    #define MY_RADIO_RF24
    #define MY_GATEWAY_ESP8266
    #define MY_WIFI_SSID "WiFiNetwork"
    #define MY_WIFI_PASSWORD "SuperSecretPassword"
    #define MY_HOSTNAME "MySensors_GW"
    #define MY_PORT 5003
    #define MY_GATEWAY_MAX_CLIENTS 255
    #define MY_NODE_ID 0
    #define CHILD_ID_RELAY 2
    
    #include <MyConfig.h>
    #include <MySensors.h>
    
    #define RELAY_PIN 1  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #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
    
    MyMessage msgRelay(CHILD_ID_RELAY, V_STATUS);
    
    void before() {
      pinMode(RELAY_PIN, OUTPUT);
      digitalWrite(RELAY_PIN, 1);
    }
    
    void presentation() {
      sendSketchInfo("Attic GW", "0.2");
      present(CHILD_ID_RELAY, S_BINARY, "Switch");
      send(msgRelay.set(0));
    }
    
    void setup() {
      //pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
    }
    
    void loop() {
      //digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    }
    
    void receive(const MyMessage &message) {
      //digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
      if (message.type == V_STATUS) {
        Serial.print("message.type = ");
        Serial.println(message.type);
        if (message.getBool() == 1) {
          Serial.print("message.getBool() = ");
          Serial.println(message.getBool());
          Serial.println("This is a V_STATUS message with a value of 1");
          //digitalWrite(RELAY_PIN, 0);
          //wait(5000);
          //digitalWrite(RELAY_PIN, 1);
          //wait(100);
        }
        send(msgRelay.set(0));
      }
    }
    

    Serial Monitor output below

    74064 GWT:RFC:C=0,MSG=0;2;1;1;2;1
    message.type = 2
    message.getBool() = 1
    This is a V_STATUS message with a value of 1
    

    I commented out the LED - I'm only using that as a simple network activity indicator - it's not related to the relay at all.

    I read in another forum post by @mfalkvidd that he used the Dn notation in his script, but I can't find the post now. I'm going to try that next.


  • Mod

    @anderBAKE said in WeMos D1 Mini Gateway + Relay:

    #define RELAY_PIN 1

    That should be

    #define RELAY_PIN D1
    

    When something doesn't work it's best to strip a sketch to the bare minimum and get that to work first, eg setup the pin, then switch the relay on and off from the main loop(), without including mysensors or anything else.



  • @Yveaux Thanks again for the suggestions. I tried the

    #define RELAY_PIN D1
    

    in the previous sketch to no avail. I'll try something simple from the main loop() next without anything else mysensors.

    Maybe, I'm selecting the wrong board from the board manager? I had been using the NodeMCU 1.0 (ESP-12E Module) based on the instructions.

    I know that in my effort to make this work last night, I was using ESP8266 >3.0 in the board manager which had the LOLIN(WeMos) D1 Mini (clone). Nothing using >3.0 worked though, so I rolled back to ESP8266 2.7.4 which does not have the same LOLIN(WeMos) D1 Mini (clone); however, maybe I should be using one of the LOLIN models from the board manager?



  • No success after stripping the sketch down to a bare relay example and using multiple different Boards from the board manager. I'm going to look into picking up a new D1 Mini + relay shield to see if it magically solves whatever issue I'm facing. @Yveaux once again, I appreciate the suggestions.


  • Contest Winner

    @anderBAKE Be careful to which pin you hook up the relay. Most Mini D1 pins are shortly being pulled up during boot up. That short spike can damage the device you've connected to the relay. It's why I avoid using the Wemos D1 mini with relays. I believe d0 is one of the pins that's not being pulled high during boot up - but not sure.



  • @TheoL Thanks. I kind of suspect that might be the case. It honestly shouldn't be this complicated. The device does everything else correctly and I'm certain the relay is wired correctly. I checked voltage, 5.4 at the relay (external power) and I know signal is getting through based on the spikes you mentioned (rapid open/close) when flashing the device.

    I'm not going to look it up right now, but I believe I remember reading somewhere that D0 was a reserved or special pin. Having looked at the WeMos relay shield, D1 is what is used, so I see no reason why it shouldn't work except that maybe I shorted it accidentally. Wouldn't be the first time.


  • Contest Winner

    What type of relay are you using? You can only use one with opto couplers. You can regonise it, because it has a vcc and a jd-vcc pin. Power the jd-vcc with 5v and vcc with 3v. The vcc is for TTL lvl control and the jd-vcc powers the 5v relay. Without this version you can not hook up a relay to a 3.3v microcontroller. Because the high state is barely high enough to drive a normal 5v relay


  • Contest Winner

    Also posting the wiring helps others to help you. Fitzing is easy for this task


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.6k
Posts