Need help with Wemos D1 Mini and Relay



  • I need some help to make an I/O switch lamp using Wemos D1 Mini and relay, does anyone can tell me how to built it with Arduino IDE?



  • /*This sketch is to turn ON/OFF relay shield which is connected to an etension
     * cord
     * for Christmas tree lights or whatever I want to turn on/off remotely
     */
    
    #include <ESP8266WiFi.h>
    #define relayPin 5//  Initialize D1 aka GPIO-5 as an output on the WeMOS board
    
    
    //  CONNECT TO WLAN
    const char* ssid = "YOURSSID";//*****************************
    const char* password = "YOURPASSWORD!";//****************
    
    //int ledPin = 2; // assign pin 2 for LED (GPIO2)
    
    //  CREATE WEBSERVER
    WiFiServer server(80);
    
    
    void setup() {
    //  pinMode(LED_BUILTIN, OUTPUT);  // Initialize the LED_BUILTIN pin as an output
    
      pinMode(relayPin, OUTPUT);     // Initialize the D1 aka GPIO5 as an output
    
    
    
      Serial.begin(115200);          //IN CASE I WANT TO USE ONBOARD LED
      delay(10);
    
    
    //  pinMode(ledPin, OUTPUT);
    //  digitalWrite(ledPin, LOW);
    
      // Connect to WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
    
      WiFi.begin(ssid, password);
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
    
      // Start the server
      server.begin();
      Serial.println("Server started");
    
      // Print the IP address
      Serial.print("Use this URL to connect: ");
      Serial.print("http://");
      Serial.print(WiFi.localIP());
      Serial.println("/");
    
    }
    
    void loop() {
      // Check if a client has connected
      WiFiClient client = server.available();
      if (!client) {
        return;
      }
    
      // Wait until the client sends some data
      Serial.println("new client");
      while (!client.available()) {
        delay(1);
      }
    
      // Read the first line of the request
      String request = client.readStringUntil('\r');
      Serial.println(request);
      client.flush();
    
      // Match the request
    
      int value = LOW;
      if (request.indexOf("/LED=ON") != -1) {
      //  digitalWrite(ledPin, LOW);
        digitalWrite(relayPin, HIGH);   // Turn the RELAY on (Note the Relay is active high
        value = LOW;
      }
      if (request.indexOf("/LED=OFF") != -1) {
     //   digitalWrite(ledPin, HIGH);
        digitalWrite(relayPin, LOW);   // Turn the RELAY on (Note the Relay is active high
        value = HIGH;
      }
    
      // Set ledPin according to the request
      //digitalWrite(ledPin, value);
    
    
      // Return the response
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println(""); //  do not forget this one
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
    
      client.println("Hello Hal.  Nice weather we're having!<br>");
      client.println("What can I do for you today?<br>");
      client.println("By the way, your tree lights are ");
    
    
      if (value == LOW) {
        client.print("<b>On.");
      } else {
        client.print("<b>Off.");
      }
      client.println("<center><HR><BR>");
      client.println("<a href=\"/LED=ON\"><button>ON<\button></a><br><br>");
    
      client.println("<a href=\"/LED=OFF\"><button>OFF<\button></a><br>");
      client.println("</html>");
    
      delay(1);
      Serial.println("Client disconnected");
      Serial.println("");
    
    }
    '''


  • @marine_hm thank for the sketch
    but i need problem, i got the ip address, then i try in a web browser, i click the off button but the lamp cannot switch to off, always on



  • do a simple switching first, then use the relay, then use the wifi
    also check your relay base resistance if you're not using a pre-made relay module.



  • how to made a simple switching? @quickmiester



  • i've been try the simple switching, but when i connect the D1 to relay, the ledpin cannot switching, always on



  • can you show your circuit?



  • @dimfreak
    Sounds like you wired it wrong... Give us a diagram.!
    Or something like this...

    RELAY (DIGITAL SIDE)

    • 5V TO 5V ON RELAY
    • GND TO GND ON RELAY
    • D1 FROM WEMOS TO D1 ON RELAY

    RELAY(110/220 SIDE)

    • POS TO CENTER SCREW
    • NO(NORMALLY OPEN) SCREW TO DEVICE

    DEVICE

    • NO FROM RELAY TO POS
    • NEG TO NEG

    0_1496062965408_Relay.jpg

    Upload a sketch too please.



  • @marine_hm
    can i get some blocks using app inventor to made an on/off app with controlling time? like timer to turn off the led



  • Sorry, don't have any experience with android apps.


Log in to reply
 

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