Chat freely about anything...

User avatar
By rudy
#54483 Found at https://github.com/CytronTechnologies/ESP8266-WiFi-Module

Four outputs right now. Some inverted states due to some leds hooked up pull up and some pull down.

There also was a handy Windows program to convert html code to the text .h file formatting needed for the .ino included file. convertHtml.exe I tried it and it and it worked as expected.

Code: Select all/*
  ESP8266 mDNS responder sample

  This is an example of an HTTP server that is accessible
  via http://esp8266.local URL thanks to mDNS responder.

  Instructions:
  - Update WiFi SSID and password as necessary.
  - Flash the sketch to the ESP8266 board
  - Install host software:
    - For Linux, install Avahi (http://avahi.org/).
    - For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
    - For Mac OSX and iOS support is built in through Bonjour already.
  - Point your browser to http://esp8266.local, you should see a response.

 */

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include "ioControl.h"

const char* ssid = "esp";
const char* password = "12345678";

const int output12 = 12;
const int output13 = 13;
const int output14 = 14;
const int output16 = 16;

// multicast DNS responder
MDNSResponder mdns;

// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);

void setup(void)

  Serial.begin(115200);

  // Connect to WiFi network
  WiFi.begin(ssid, password);
  Serial.println(""); 
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Set up mDNS responder:
  // - first argument is the domain name, in this example
  //   the fully-qualified domain name is "esp8266.local"
  // - second argument is the IP address to advertise
  //   we send our IP address on the WiFi network
  if (!mdns.begin("esp8266", WiFi.localIP())) {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
 Serial.println("mDNS responder started");
 
  // Start TCP (HTTP) server
  server.begin();
  Serial.println("TCP server started");
  delay(2000);
  Serial.end();
 
  pinMode(12,OUTPUT);//active high
  pinMode(13,OUTPUT);digitalWrite(1,HIGH); //active low
  pinMode(14,OUTPUT);//active high
  pinMode(16,OUTPUT);//active high
}

void loop(void)
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait for data from client to become available
  while(client.connected() && !client.available()){
    delay(1);
  }
 
  // Read the first line of HTTP request
  String req = client.readStringUntil('\r');
 
  // First line of HTTP request looks like "GET /path HTTP/1.1"
  // Retrieve the "/path" part by finding the spaces
  int addr_start = req.indexOf(' ');
  int addr_end = req.indexOf(' ', addr_start + 1);
  if (addr_start == -1 || addr_end == -1) {
    return;
  }
  req = req.substring(addr_start + 1, addr_end);
  client.flush();
 
  String s;
  if (req == "/")
  {
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += file1;
  }
 
  else if (req.indexOf("gpio0")!=-1)
  {
    if(req.indexOf("on")!=-1)
    digitalWrite(14,LOW);  //HIGH);
    else
    digitalWrite(14,HIGH);  //LOW);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += file1;
  }

  else if (req.indexOf("gpio1")!=-1)
  {
    if(req.indexOf("on")!=-1)
    digitalWrite(12,LOW);
    else
    digitalWrite(12,HIGH);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += file1;
  }
 
 
  else if (req.indexOf("gpio2")!=-1)
  {
    if(req.indexOf("on")!=-1)
    digitalWrite(13,LOW);  //HIGH);
    else
    digitalWrite(13,HIGH);  //LOW);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += file1;
  }
 
  else if (req.indexOf("gpio3")!=-1)
  {
    if(req.indexOf("on")!=-1)
    digitalWrite(16,LOW);  //HIGH);
    else
    digitalWrite(16,HIGH);  //LOW);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += file1;
  }
 
  else
  {
    s = "HTTP/1.1 404 Not Found\r\n\r\n";
  }
 
  while(s.length()>2000)
  {
   String dummy = s.substring(0,2000);
   client.print(dummy);
   s.replace(dummy,"");
  }
  client.print(s);
}


Put the following in a file called ioControl.h

Code: Select all#ifndef header_h
#define header_h

String file1=
"<html>\r\n"
"<head>\r\n"
"   <title>ESP8266 GPIO Control</title>\r\n"
"</head>\r\n"
"<body>\r\n"
"<h2>ESP8266 GPIO Control</h2>\r\n"
"\r\n"
"<form action = \"/gpio0\" method=\"GET\">GPIO 0: G <input type=\"submit\" name=\"on\" value=\"ON\"/>&nbsp;<input type=\"submit\" name=\"off\" value=\"OFF\"/>&nbsp;</form>\r\n"
"<form action = \"/gpio1\" method=\"GET\">GPIO 1: R <input type=\"submit\" name=\"on\" value=\"ON\"/>&nbsp;<input type=\"submit\" name=\"off\" value=\"OFF\"/>&nbsp;</form>\r\n"
"<form action = \"/gpio2\" method=\"GET\">GPIO 2: Y <input type=\"submit\" name=\"on\" value=\"ON\"/>&nbsp;<input type=\"submit\" name=\"off\" value=\"OFF\"/>&nbsp;</form>\r\n"
"<form action = \"/gpio3\" method=\"GET\">GPIO 3: B <input type=\"submit\" name=\"on\" value=\"ON\"/>&nbsp;<input type=\"submit\" name=\"off\" value=\"OFF\"/>&nbsp;</form>\r\n"
"\r\n"
"</body>\r\n"
"</html>\r\n";

#endif