Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By TheQuestor
#39983 Ok so I finally after many hours got both my ESP-01 to have a 3rd GPIO [4] and it works great. I can use GPIO2 and GPIO4 but now my lack of programming skills are coming into play.

I am using the following sketch that I modified [or at least tried] to work with 2 LEDS instead of just 1 on either GPIO2 or GPIO4

Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid = "MySSID";
const char* password = "MyPasswd";
 
int ledPinR = 2; // GPIO2
int ledPinB = 4; // GPIO4
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
 
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  digitalWrite(ledPinR, LOW);
  digitalWrite(ledPinB, 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 valueR = LOW;
  if (request.indexOf('/LEDR=ON') != -1) {
    digitalWrite(ledPinR, HIGH);
    valueR = HIGH;
  }
  if (request.indexOf('/LEDR=OFF') != -1){
    digitalWrite(ledPinR, LOW);
    valueR = LOW;
  }

  int valueB = LOW;
  if (request.indexOf('/LEDB=ON') != -1) {
    digitalWrite(ledPinB, HIGH);
    valueB = HIGH;
  }
  if (request.indexOf('/LEDB=OFF') != -1){
    digitalWrite(ledPinB, LOW);
    valueB = LOW;
  }


 
// 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.print("LedR pin is now: ");
   
  if(valueR == HIGH) {
    client.print("On"); 
  } else {
    client.print("Off");
  }

client.print("<hr>");

  client.print("LedB pin is now: ");
  if(valueB == HIGH) {
    client.print("On"); 
  } else {
    client.print("Off");
  }
 
  client.println("<br><br>");
  client.println("Click <a href=\"/LEDR=ON\">here</a> turn the LED on pin 2 ON<br>");
  client.println("Click <a href=\"/LEDR=OFF\">here</a> turn the LED on pin 2 OFF<br>");
  client.println("<hr>");
  client.println("Click <a href=\"/LEDB=ON\">here</a> turn the LED on pin 4 ON<br>");
  client.println("Click <a href=\"/LEDB=OFF\">here</a> turn the LED on pin 4 OFF<br>");
  client.println("</html>");
 
  delay(1);
  Serial.println("Client disonnected");
  Serial.println("");
 
}
 


Browser output is as follows
In attachments

When I click LEDR or LEDB on they both come on and when I click either LEDR or LEDB they both go off :(

I am sure I am missing something simple but I can't for the life of me figure out what I am missing so they work independent of each other :(
You do not have the required permissions to view the files attached to this post.