ESP8266 Webserver Project

Moderator: Sprite_tm

User avatar
By Piman
#53578 I know this is most probably simple and straightforward but I've been working on a project for weeks now and not being able to achieve my goal, I'm trying to fade in a GPIO pin when high and fade out when Low. I have tried some of the Arduino examples to no avail, so please if you can help it would be much appreciated, , this is my code:-
Code: Select all

#include <ESP8266WiFi.h>
#define SSID     "xxxx"   

#define PASSWORD     "xxxx" 

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO3
  pinMode(3, OUTPUT);
  digitalWrite(3, 0);
 
  // 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.println(WiFi.localIP());
}

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 req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
 
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO3 according to the request
  digitalWrite(3, val);
 
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}


Thanks in advance kind regards Andy.
User avatar
By Barnabybear
#53614 Hi, your going to need something like this:
Code: Select all  switch (val) {
    case 0:
      for(int led_brightness = 255; led_brightness = 0; led_brightness--) {
        analogWrite(3, led_brightness);
        delay(10);
        //yield;
      }
      break;

    case 1:
      for(int led_brightness = 0; led_brightness = 255; led_brightness++) {
        analogWrite(3, led_brightness);
        delay(10);
        //yield;
      }
      break;
  }

Instead of:
Code: Select all  // Set GPIO3 according to the request
  digitalWrite(3, val);

If that contains bits of code you haven't used before you find explinations in the following link.
https://www.arduino.cc/en/Reference/HomePage

Why did you pick GPIO 3? By default it is Rx and will not work as a GPIO unless you:
Code: Select allpinMode(3, FUNCTION3)

first. But note it will no longer function as Rx unless you:
Code: Select allpinMode(3, FUNCTION0)

or untill you enter UART mode to flash new code.
User avatar
By Piman
#53731 @ Barnabybear, Thank you very much for your reply, the sample code and references you sent were very helpful, looking at the code and using the references I've managed to work it out if it wasn't for your kind help. who knows how long I would've spent on this once again thank you very much. :D