Question with powering and triggering relay shield



  • @Roo-ster
    I agree with you make the changes to the code.



  • Hmmm maybe I'm just being cheap. Rather than having two wemos and 2 relays in a project box. I thought I would try to run 2 relays from 1 wemos.
    Basically; a wemos d-1 mini in the middle and a relay on either side. The sketch above was just to show it does work. So the relays work on digital 0 or 1 and not just 3V-5V or GND. I keep forgetting that.
    This is the Sketch I'm working on.

    /*
     * NEW PROJECT BOX WITH TWO LEDS
     *RED
     *GREEN
     */
    
    #include <EEPROM.h>
    #include <ESP8266WiFi.h>
    #include <PubSubClient.h>
    #include <Wire.h>
    //#define relay1Pin 5//  Initialize D1 aka GPIO-5 as an output on the WeMOS board
    
    // Connect to the WiFi
    const char* ssid = "MYSSID";
    const char* password = "MYPASSWORD";
    const char* mqtt_server = "192.168.0.62";
    
    WiFiClient espClient;
    PubSubClient client(espClient);
    
    const int relay1Pin = D1; // FOR RED LED  CHANGED TO D1 FOR TESTING
    // DONT FORGET TO CHANGE IT BACK TO D5
    const int relay2Pin = D8; // FOR GREEN LED
    
    
    // ************************************************************
    /* WIRING SCHEMATIC
     *    
     *  LEDS:
     *  12V TO RED WIRES OF RED AND GREEN LEDS
     *  BLACK WIRES OF LED TO "NORMAL OFF" SIDE OF RELAY
     *  CENTER OUTPUT (+) ON RELAY TO GROUND
     *  TIE GROUNDS TOGETHER
     *  
     *  "VERTER":
     *  12V TO INPUT SIDE OF "BUCK VERTER"
     *  5V OUT TO WEMOS
     *  
     *  WEMOS:
     *  5V FROM BUCK VERTER TO 5V PIN ON WEMOS
     *  D5 TO D1 ON RELAY 1
     *  D8 TO D1 ON RELAY 2
     *  
     *  RELAYS:
     *  ?????????????????????
     */
    // ************************************************************
    
    
    void callback(char* topic, byte* payload, unsigned int length) {
    
      // ******************* DF CHARTS *****************************
      if (strcmp(topic, "Charts/DF/F") == 0) {
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '0') {
            Serial.println (F("RED LED IS ON"));
            digitalWrite(relay1Pin, HIGH);
          }
          if (receivedChar == '1') {
            Serial.println (F("RED LED IS OFF"));
            digitalWrite(relay1Pin, LOW);
          }
        }
      }
      // ******************* VEIN CHECKS *****************************
      if (strcmp(topic, "Charts/DF/V") == 0) {
        for (int i = 0; i < length; i++) {
          char receivedChar = (char)payload[i];
          if (receivedChar == '0') {
            Serial.println (F("GREEN LED IS ON"));
            digitalWrite(relay2Pin, HIGH);
          }
          if (receivedChar == '1') {
            Serial.println (F("GREEN LED IS OFF"));
            digitalWrite(relay2Pin, LOW);
          }
        }
      }
    }
    
    boolean reconnect() {  // **********************************************************
      // Loop until we're reconnected
      while (!client.connected()) {
        Serial.print (F("Contacting MQTT server..."));
        // Attempt to connect
        if (client.connect("DonorFloor")) {//assign a "client name".  Each wemos must have a unique name
          Serial.println (F("connected"));
          
          // ... SUBSCRIBE TO TOPICS
          client.subscribe("Charts/DF/F");
          client.subscribe("Charts/DF/V");
          return client.connected();
    
          Serial.print (F("Failed to connect. "));
          Serial.println (F(" Attempting connection again in 3 seconds"));
          // Wait 3 seconds before retrying
          delay(3000);
          return 0;
        }
      }
    }
    
    
    void setup()
    {
      {
        Serial.begin(9600);
        client.setServer(mqtt_server, 1883);
        client.setCallback(callback);
        pinMode(relay1Pin, OUTPUT);
        pinMode(relay2Pin, OUTPUT);
      }
    
    
      // Connect to WiFinetwork
      Serial.println();
      Serial.println();
      Serial.print (F("Connecting to "));
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
      WiFi.mode(WIFI_STA);
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        //Serial.begin(9600);
        Serial.print (F("."));
      }
      Serial.println (F(""));
      Serial.println (F("WiFi connected"));
      // Print the IP address
      Serial.print (F("Local IP: "));
      Serial.println(WiFi.localIP());
    }
    
    
    void loop() {
      if (!client.connected()) {
        reconnect();
      }
    
      client.loop();
    }
    digitalWrite(relay1Pin, HIGH);    works  but again, I want to control 2 relays.  So what I was thinking was to digitalWrite to D5 and D8 and just connect those wire to D1 pin on the relays


  • It will need two pins if you are wanting different actions from each relay. But if you are wanting them to act at the same time everytime then this is fine.



  • This post is deleted!


  • @thejester88
    Yes I realize that. When I remove the Relay from the Wemos(UNSTACK) and both on a bread board.
    5V to 5V
    GND to GND
    D1 to D1
    Nothing hapens



  • Do you have a schematic of your wiring?



  • If I can get it to work without the "piggy-backing", there will be two relays.
    Pin 5 on the wemos to pin 1 on relay 1
    Pin 8 on the wemos to pin 1 on relay 2
    For now, I'm just trying to get this to trigger the relay. Here's what i'm trying to accomplish, second relay has recently been purchased and not here yet.
    0_1494548146649_WemosRelay.jpg



  • This should work. How are you actually making the connections? Are they reliable?



  • @deshipu
    I thought it should work too! Time to go check my jumper wires. Wouldn't be the 1st time they were the culprit.



  • Sorry for the delay. Works has kept me busy. I did apply 3 and 5V to pin 1 on the Wemos trying to trigger the relay. In my quest, I even tried applying 3 and 5 volts to pin 1 on the relay.

    How can I test to see if the individual pins on the Wemos & relay have been fried? I should still be able to use the other pins? I still have a hand full or D1 Minis.

    I have 2 more relays on the way via AliExepress.

    Next time, I will just use solid copper wire instead of jumper wires and NOT apply voltages directly to the digital pins.


Log in to reply
 

Looks like your connection to WEMOS Forum was lost, please wait while we try to reconnect.