I would like to control a 5V relay with Wemos D1 R1 module through webserver. I'm using the following modules:
Tongling 5vdc Relay- Wemos D1 R1
with the following schematic:
Expand!
while using the following code:
#include <ESP8266WiFi.h>
const char* ssid = "Myssidname";
const char* password = "Myssidpass";
int ledPin = D3;
WiFiServer server(80);
void setup() {
Serial.begin(9600);
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 : ");
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, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(ledPin, LOW);
value = LOW;
}
// 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.print("Led pin is now: ");
if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("
");
client.println("Click <a href=\"/LED=ON\">here</a> Turn relay ON
");
client.println("Click <a href=\"/LED=OFF\">here</a> Turn relay OFF
");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}
After uploading the code successfully and watching the monitor, the light gets turned on and I get the following message:
16:02:20.400 -> WiFi connected
16:02:20.425 -> Server started
16:02:20.479 -> Use this URL : http://192.168.100.7/
16:02:41.680 -> new client
16:02:41.680 -> GET / HTTP/1.1
16:02:41.680 -> Client disconnected
However, when I turn on or off the relay using the browser, nothing happens .
19:38:56.010 -> new client
19:38:56.010 -> GET /LED=ON HTTP/1.1
19:38:56.010 -> Client disconnected
19:38:56.010 ->
19:38:57.016 -> new client
19:38:57.016 -> GET /LED=OFF HTTP/1.1
19:38:57.063 -> Client disconnected
19:38:57.063 ->
BTW, wemos wifi name appear when I search for networks and Wemos D1 doesn't respond to AT Commands
matrixall, you need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.
In fact, the IDE itself has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can easily be quite garbled - you appear to have fudged a fix to that - and is always more difficult to read due to the font.
It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.
Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.
dave-in-nj:
As for the relay it is proper to power it from a separate power supply. A relay need almost 100mA
As per his diagram, he appears to be using the WeMOS D1 in the "Arduino" format. This apparently has a properon-board switchmode regulator for the 5 V.
One more addition, I tried modifying the setup by defaulting relay to high which suppose to set the circuit open and turn off the bulb, however the light bulb is turned on :o
matrixall:
One more addition, I tried modifying the setup by defaulting relay to high which suppose to set the circuit open and turn off the bulb, however the light bulb is turned on :o
Did you verify that the relay signal is connected to ledPin ?
Might not be a good choice as the relay has an LED and the ledPin has an LED.
Ideally, you want the pin to pull to ground. the Vcc (5V) to the relay would have enough power to power the relay.
the grounds to the relay and the wemos board have to connected.
You should hear the click of the relay.
as for the comment about being a purist. if the relay pulls the voltage low and your board voltage drops, it can re-set the micro.
if your board voltage drops a little, other signals can get flakey.
if you use a nice 1 or 2 amp power supply, then there is less of a concern.
Many of the Relay modules have an opto-coupler. that is really an LED so you need to feed it about 10-20mA.
some have only and on-board transistor, so you have to provide the base correctly.
You may need to use a PN2222 like shown.
SteveMann:
I suspect that you are trying to control a 5V relay module with 3.3V logic levels. It probably won't work.
I tried replacing the Relay with led and it just worked which confirms the voltage is less than required. is there a work around with no additional modules?
SteveMann:
Also, I have absolutely no clue what you mean with: "... and Wemos D1 doesn't respond to AT Commands"
When I type in AT Commands in Serial Monitor no response is given,
OK, are your two comments isolated or connected ?
Did you use an LED and then also make it go off and on with AT commands ?
As for your relay board, the single relay board use and FET to switch the load for the coil of the relay.
100mA, 5V.
so, you were able to light an LED using the pin on your ESP8266
Were you able to provide the 5V to the FET it needs ?
Consider a relay module that uses optoisolators to interface the signal.
On the single relay modules, they often require you to provide the 5v. Easy to test with a wire and a resistor.
On the multiple relay boards, they use opto's and often expect you to bring the pin to ground. The relay board provides the power to the LED in the opto. Using the correct resistor is needed for the opto unless the board is fully featured.
Since the relay is not responding to your signal, there is some bit missing. Get a schematic for the relay module and figure out what is required to drive it. verify the chip on the board. all to often the assembler will swap chips.
I missed the post that said you were able to operate the relay board with 5v and with a resistor to the signal pin. Validating that the unit is operating.