ESP8266 WiFi Module Design Notes

Some informational links

ESP8266 Community Wiki
NURDs ESP8266 Information Page
ESP8266 WIFI Dropout-proof Connectivity
ESP8266 Discussion Forums
Arduino core for ESP8266 WiFi chip
ESP8266 Arduino IDE WebConfig
Building A Low Cost WiFi Camera using an ESP8266
ESP8266 Troubleshooting Guide
ESP8266 Mains Energy Monitor
ESP8266 Remote Controlled Sockets
How to Make Two ESP8266s Talk
IoT Datalogger with ESP8266 WiFi Module and FRDM-KL25Z
A list of tutorials on sending data to Thingspeak. Includes some ESP8266 examples.
Minimal MQTT: Networked Nodes
ESP8266 Video Series by Andreas Spiess
ESP8266 Mobile Rick Roll Captive Portal
MicroPython on the ESP8266: Kicking the Tires
Building a Low Cost WiFi Camera
ESP8266-Based DIY WiFi Baby Monitor
ESP8266 Mailbox Notifier using DeepSleep and Blynk
ESP8266 GMail Sender
Email from ESP8266 for $3
Web Matrix Control Proves Power of ESP8266
ESP8266 01 Send Email via Gmail with Google Docs
The ESP: A New 1KB Contender Appears
Avoid Hard-Coding WiFi Credentials on Your ESP8266 Using the WiFiManager Library
ESP8266 Servo Motor Control
Reducing WiFi power consumption on ESP8266, part 3

LED Control Demonstration Code

This is a heavily modified version of the AdvancedWebServer example sketch to demonstrate the control of an LED from a web interface without the use of a bunch of extra libraries.

The LED control was implemented using a form that performs a get with the argument passed to the index page. The index page is checked for arguments, and if found, these are parsed, the LED value is decoded, and the pin is updated. There are probably much cleaner ways of handling this, but my knowledge of making nice html is limited.

It would be pretty easy to implement a toggle LED button, or to generate the text on a single button dynamically, based on the current pin state. At some point, I may implement these as well, and I'll post an update.

There is also support for OTA updates, but this hasn't been tested, so it may not work properly.

/*
 * Copyright (c) 2015, Majenko Technologies
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * * Neither the name of Majenko Technologies nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * Some additions are from http://www.whatimade.today/esp8266-on-websockets-mdns-ota-and-leds/.
 * 
 * Heavily modified by Michael Anton to provide AP mode support, and to be able to turn an LED on/off
 * with a button, without using a bunch of fancy code.
 */
 
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// Includes for OTA updates.  From http://www.whatimade.today/esp8266-on-websockets-mdns-ota-and-leds/
#include <ESP8266HTTPUpdateServer.h>
 
#define APMODE    // Define to enable AP mode, otherwise a connection to the local network will be established.
 
#ifdef APMODE
  // AP mode password should be at least 8 characters, or some clients complain
  const char *APssid = "ESP8266";
  const char *APpass = "password";
#else
  const char *ssid = "yourssid";
  const char *password = "yourpassword";
#endif
 
// From http://www.whatimade.today/esp8266-on-websockets-mdns-ota-and-leds/
ESP8266HTTPUpdateServer httpUpdater;
 
ESP8266WebServer server(80);
 
// LEDs are connected between pin and +3.3V through 220 ohm resistors
#define REDLED 12   
#define YELLED 13
 
void handleRoot()
  {
  digitalWrite(REDLED, 0);
 
  char temp[1000];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;
 
  snprintf(temp, sizeof(temp),
    "<html>\
      <head>\
        <meta http-equiv='refresh' content='5'/>\
        <title>ESP8266 Demo</title>\
        <style>\
          body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
        </style>\
      </head>\
      <body>\
        <h1>Hello from ESP8266!</h1>\
        <p>Uptime: %02d:%02d:%02d</p>\
        <p><form action=\"/\" method=\"get\">\
        <button type=\"submit\" name=\"LED\" value=\"0\">LED Off</button>\
        <button type=\"submit\" name=\"LED\" value=\"1\">LED On</button>\
        </form></p>\
        <img src=\"/test.svg\" />\
      </body>\
    </html>",
    hr, min % 60, sec % 60);
  server.send(200, "text/html", temp);
 
  if (server.args())
    {
    for (int8_t i = 0; i < server.args(); i++)
      {
      if (server.argName(i).equals("LED"))
        digitalWrite(YELLED, 1 - server.arg(i).toInt());
      }
    }
  digitalWrite(REDLED, 1);
  }
 
void handleNotFound()
  {
  digitalWrite(REDLED, 0);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
 
  for (uint8_t i = 0; i < server.args(); i++)
     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
 
  server.send(404, "text/plain", message);
  digitalWrite(REDLED, 1);
  }
 
void setup(void)
  {
  pinMode(REDLED, OUTPUT);
  pinMode(YELLED, OUTPUT);
  digitalWrite(REDLED, 1);
  digitalWrite(YELLED, 1);
 
  Serial.begin(115200);
 
  Serial.println("");
 
 // WiFi.setMode(WIFI_STA_AP);    // Setup as an access point, and a station
  #ifdef APMODE
    /* You can remove the password parameter if you want the AP to be open. */
    WiFi.softAP(APssid, APpass);
    WiFi.begin();
 
    IPAddress myIP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(myIP);
  #else
//    WiFi.mode(WIFI_AP);    // Setup as an access point
    WiFi.begin(ssid, password);
  #endif
 
  Serial.println("");
 
  Serial.print("ESP8266 chip ID: 0x");
  Serial.println(ESP.getChipId(), HEX);
 
  Serial.print("ESP8266 flash chip ID: 0x");
  Serial.println(ESP.getFlashChipId(), HEX);
 
  Serial.print("ESP8266 flash chip speed: ");
  Serial.println(ESP.getFlashChipSpeed());
 
  Serial.println("");
 
  #ifndef APMODE
   // 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());
  #endif
 
  if (MDNS.begin("esp8266"))
    Serial.println("MDNS responder started");
 
  // From http://www.whatimade.today/esp8266-on-websockets-mdns-ota-and-leds/
  httpUpdater.setup(&server);
  server.on( "/", handleRoot);
  server.on( "/test.svg", drawGraph);
  server.on( "/inline", []()
    {
    server.send ( 200, "text/plain", "this works as well" );
    });
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
  // WiFi.printDiag(Serial);
  }
 
void loop(void)
  {
  server.handleClient();
  }
 
void drawGraph()
  {
  String out = "";
  char temp[100];
  out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  out += "<g stroke=\"black\">\n";
  int y = rand() % 130;
  for (int x = 10; x < 390; x+= 10)
    {
    int y2 = rand() % 130;
    sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
    out += temp;
    y = y2;
    }
  out += "</g>\n</svg>\n";
 
  server.send(200, "image/svg+xml", out);
  }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial 3.0 License