ESP8266 Arduino Webserver

Merry christmas everyone. I have a present for you.

Untitled

A sketch with a simple modified webserver for the ESP8266 AT firmware using an arduino. Most credits go to this guy, where i got the basics : http://rayshobby.net/?p=9734 .

What my sketch basically does you might ask?

Well, it first boots up as an AP (access point), you connect to it (just like you connect to your wifi, it should be named something like ESP-???) and access in your browser 192.168.4.1. Here you will see a very simple webpage where you enter the SSID and password of your wifi network, and then it connects to it. After this you can do all sort of things. In this sketch it just shows the same webpage again.

I did this to learn how to use forms with GET method to send and receive data (to fiddle with sensors, use it to turn LEDs on and off, etc).

I won’t post here the full code because i will upload the file and you can also find the simple webserv on rayshobby.

The function “get_data” is very simple and reads what comes out after the /get request. Ignore if it request the favicon.ico (very annoying, chrome sends a request for every refresh of the page – arrrhh), simple display the website if it’s a simple “/get” as if you are accessing the webserver, and if there is a question mark character store the data using the sort_data function. (if you input data and press connect, i will have to add to check if the strings are empty, or if it connects successfuly, this is just a basic sketch)

Please modify the port if u want (but you will have to access the ESP like 192.168.4.1:PORT) and the baud rate. You need to update firmware to be able to change baud rate. I use this one.

I will post a simple tutorial about how to simply turn a led ON or OFF soon but if you understand the code, it should be simple enough. Enjoy!

Download the arduino sketch.

Advertisements

9 thoughts on “ESP8266 Arduino Webserver

  1. Hello i wonder if it is possible to put a password on the page before entering it that way no one can change the ssid or password?

  2. Hello i was wondering if there was a way to put a password on the page so that no one can change the ssid or password?

    • Yes. You can definitly do this. Just insert this javascripthttp://www.javascriptkit.com/script/cut10.shtml
      Also check out esp8266.com .
      My code is a bit oudated there are better that don t use at+ commands.

  3. zee says:

    This code is showing this error
    “exit status 1
    ‘setupWiFi’ was not declared in this scope”

    Please upload a esp8266 username and password code

    • Are you using my code from google drive? I just checked and I can compile it :
      “Done compiling. Global variables use 1,411 bytes of dynamic memory.”
      What version of Arduino are you using? I use 1.6.5.

  4. zee says:

    #include

    const char* ssid = “Bin Nadeem Associates”;
    const char* password = “35577F48”;

    int ledPin4 = 4; // GPIO4
    int ledPin5 = 5;
    WiFiServer server(80);
    WiFiClient client = server.available();

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

    pinMode(ledPin4, OUTPUT);
    digitalWrite(ledPin4, LOW);
    pinMode(ledPin5, OUTPUT);
    digitalWrite(ledPin5, 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 value4 = LOW;
    if (request.indexOf(“/LED4=ON”) != -1) {
    digitalWrite(ledPin4, HIGH);
    value4 = HIGH;
    }
    if (request.indexOf(“/LED4=OFF”) != -1) {
    digitalWrite(ledPin4, LOW);
    value4 = 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(“”);
    client.println(“”);

    client.print(“Led4 pin is now: “);

    if(value4 == HIGH) {
    client.print(“On”);
    } else {
    client.print(“Off”);
    }
    client.print(“Led5 pin is now: “);

    if(value5 == HIGH) {
    client.print(“On”);
    } else {

    client.print(“Off”);
    client.println(“”);
    client.println(“SSID”);
    client.println(“”);
    client.println(“Password”);
    client.println(“”);
    client.println(“”);
    client.println(“”);

    delay(1);
    Serial.println(“Client disonnected”);
    Serial.println(“”);

    }

    I’m using the following code. But I dont know how to store the password and username in a variable that will come from the web page. Plz help.

  5. Hello, I am using arduino nano with esp8266-01.
    The compiling and uploading goes well but in the serial monitor I get this after entering ssid and password in the browser –

    AT+CIPSEND=2,311
    link is not
    AT+CIPCLOSE=2
    link is not

    complete log : http://codepaste.net/x77giw

    please help me out.

    • Sorry for the slow response.
      Here is the problem :
      “if(wait_for_esp_response(2000, “> “))
      esp.print(header);
      esp.print(content);
      }
      else {
      esp.print(“AT+CIPCLOSE=”);

      So you aren’t getting a response from the ESP. First of all, is the firmware the one posted on the page?
      I have to admit that my code is old and the ESP8266 evolved greatly, you should try the newest AT firmware, but you will have to change some code because the commands are different. But if you want to use my code, first please check that the firmware is the one above. Second, you can connect to the ESP8266 via browser and the page appears?

      PS. The codepaste.net link isn t valid anymore.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: