So this is my firs time interfacing with NodeMCU ESP 32. im currently testing blinking led(NOT the led in the nodemcu, an actual red LED) as basic start up. the problem is it is not working for me? so here is the thing. my LED input is at pin 5 which i connected the LED to G5, i believe it is that. then i connected the other pin of the led to GND/ground. then i upload the code. according to my source i need to press "BOOT" when the "connecting...." appears which what i did then press RESET after ward. then go connect then go to website the press the the respective ON and OFF. the problem is id didnt light up my led. it just stay off even i pressed ON/HIGH. i wonder what is wrong? https://i1.wp.com/dronebotworkshop.com/wp-content/uploads/2020/04/esp32-buttons.jpg?w=768&ssl=1 Source(in my case im trying to lighten an external led, in his case, the led of the nodemcu itself)
LED Pin 1 = connected to G5 of the NodeMCU
LED Pin 2 = connected to GND of the NodeMCU
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define led 5 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "xxxx";
const char *password = "xxx";
WiFiServer server(80);
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(led, HIGH); // GET /H turns the LED on
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(led, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
i tried using both with(which is around 1k ohm) and without resistor but still no work. i even switch both connections thinking that the polarity is wrong but itis still not working.
the resistor is necessary to ensure the LED won't draw more current than your ESP can provide.
what type of LED is this ? does it work under 3.3V ?
also did you check exactly what currentLine holds when you test if it ends with "GET /H"? if (currentLine.endsWith("GET /H")) {
(you might have a trailing HTTP/1.1)
You need to know the specs of your leds to be able to drive them. I would say use a 220Ω resistor in series with the led. pin --- R220Ω -- Anode [LED] Cathode ---- GND
regarding the second part, add
// print what we got in the currentLine
Serial.print(F("currentLine is: ")); Serial.println(currentLine);
just before
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
and have a look at the Serial monitor when you click on the links
hi. regarding the 2nd part about serial.print. i checked and what happen with this "currentLine is: " is this. as you can see(the one i highlight), the currentline is still currentline with blank result. actually even put
Serial.println("currentline is: ") before the If statement of on and off. and i also put:
Serial.println("currentline is: ")
Serial.println ("HIGH") or LOW respeectively on each If statment when im going to turn on led on or off. i dont understand, does that mean the If statement didnt work? because the only print that appear is the first print which is outside the If statements. it should because this is actually an Example Code. i really wonder why...
ahh my bad. buy what happen is i put that code you want me to put in order to trace where my code is:
so i put it 3x. first is Before the IF statements on which i will turn on/off the led.
that is only:
Serial.print(F("currentLine is: "));
Serial.println(currentLine);
Serial.println("OUTSIDE")
then i put the other two inside the if statements of the LEDS
Serial.print(F("currentLine is: "));
Serial.println(currentLine);
Serial.println("HIGH") on high statement or Serial.println("LOW") on low statement respectively.
but what only appear in my Serial Monitor now is:
currentLine is:
OUTSIDE
currentLine is:
OUTSIDE
In other words the IF statement didnt work even if i clicked the ON or OFF in the webste
oh my bad. code is still pretty the same with additional base on what you said before:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define led 4 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED
// Set these to your desired credentials.
const char *ssid = "xxxxxx";
const char *password = "xxxxxx";
WiFiServer server(80);
void setup() {
pinMode(led, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
Serial.print(F("currentLine is: ")); //what i add NEW
Serial.println(currentLine);
Serial.println("OUTSIDE");
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(led, HIGH);
Serial.print(F("currentLine is: "));
Serial.println("HIGH");// GET /H turns the LED on //what i add NEW
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(led, LOW); // GET /L turns the LED off
Serial.print(F("currentLine is: ")); //what i add NEW
Serial.println("LOW");
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}