WeMos D1 ESP8266 WiFi Board

This weekend (while the mercury hid in its bulb with a -42° C windchill), was an excellent opportunity to stay indoors and play with electronics! So, I took my recently received WeMos D1 R2 WiFi ESP8266 Development Board for a test drive. While I have used the ESP8266 in other WiFi-enabled projects, a Wi-Fi enabled Arduino-compatible board based on this chip is an interesting move.

Here’s the D1 R2 board which is the most up-to-date revision.

WeMosD1: Top View

WeMosD1: Top View

WeMosD1: Bottom View

WeMosD1: Bottom View

The Arduino-UNO look-alike board is built on the ESP8266EX 32 bit RISC micro-controller powered at 3.3V (so all the I/O is also 3.3V!) and running at 80MHz. It has a full WiFi transceiver and contains 64KB of instruction RAM,  96KB of data RAM and 4MB flash memory!  (Reread that again!) As someone who continually runs up against the memory limits of the smaller Arduino’s, the huge additional RAM and flash capacity could be a game-changer. And all for the price of a couple of Starbuck’s specials!

Got your attention yet?  (If not, check your pulse!)

The layout and pin-out of the board supports some (if not all) of the Arduino-based shields and the datasheet on the board claims:

  • 11 digital input/output pins (with support for I2C, SPI protocols)
  • 1 ADC (3.3V max input)
  • a Micro USB connection
  • a power jack, 9-24V power input.

The best reference for the board I have found is the WeMos Wiki so if you are planning on getting one, this is an excellent resource. If, like me, you are more familiar with the Arduino IDE, you can follow the instructions on the wiki to download the CH340g USB drivers and use the Boards Manager to download support for this and other ESP8266 products.

I checked around and found an interesting tutorial and website get started with the board. This tutorial scans and connects to your local WiFi network, displaying the results on a small OLED display.

Rather than concentrating on getting my own 0.96″ OLED display working (I’ve had problems with it in the past), I concentrated on getting WiFi connectivity and demonstrating interaction with a web page on my webserver.

In the software below, the WeMos D1 board connects to my local WiFi network, sends a “GET” command with a variable to a simple php page on my website and displays the response from the page. The php file returns the square of the variable so this demonstrates sending data to and receiving data from a web page. It is a very simple piece of software that exercises some interesting properties of the board and is a great way to start playing with it.

Here’s my code written using the Arduino IDE:

//*****************************************************************************************//
//                                WeMos D1 Board WIFI TESTING
//     based on work from Nick "https://www.youtube.com/watch?v=IQVKGAU8jcA"
//                                     Adrian Jones
//                                    February 2016
//*****************************************************************************************//
//
// Build 1
//   r1   160213 - Reading from php file stored on net
//
//********************************************************************************************
#define build 1
#define revision 1
//********************************************************************************************

#include <ESP8266WiFi.h>

const char* ssid     = "Your_WiFi_SSID";      // SSID of local network
const char* password = "Your_WiFi_Password";  // Password on network
char servername[]="woodsgood.ca";             // my remote server
WiFiClient client;
String result;

int x=0;

// Serial control
#define sb   Serial.begin(57600)
#define sp   Serial.print
#define spf  Serial.printf
#define spln Serial.println

extern "C" {
#include "user_interface.h"
}

//*****************************************************************************************//
//                                      SUBROUTINES
//*****************************************************************************************//

// deleteHttpHeader(): remove all of the html header
void deleteHttpHeader() {
  if(result.endsWith("Content-Type: text/html")) result="";
}

// doSendGET(num): client function to send/receive GET request data
void doSendGET(int inx) {                      
  if (client.connect(servername, 80)) {         // starts client connection, checks for connection
    sp(F("Connected. "));                          
    client.print("GET /text.php?in=");
    client.print(inx);
    client.println(" HTTP/1.1");                // download text
    client.println("Host: woodsgood.ca");
    client.println("Connection: close");       // close 1.1 persistent connection  
    client.println(); //end of get request
  } else {
    spln(F("Connection failed")); //error message if no client connect
    spln();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    result = result+c;
    deleteHttpHeader();
  }
  result.trim();                // remove extra beginning and ending whitespace
  sp(F("Response: "));
  spln(result);                 // print response from website page
  client.stop(); //stop client
}


//*****************************************************************************************//
//                                      Initial Setup
//*****************************************************************************************//

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);  // initialize onboard LED as output
  sb;
  Serial.setDebugOutput(true);
  delay(1000);
  spln(F("********************************************"));
  spln(F("WiFi Tests on WeMos D1 Board by Adrian Jones"));
  sp(F("Build ")); sp(build); sp(F(".")); spln(revision);
  sp(F("Heap Size: \t"));    sp(system_get_free_heap_size()/1024); spln(F("KB"));
  sp(F("Boot Version: \t")); spln(system_get_boot_version());
  sp(F("CPU Speed: \t"));    sp(system_get_cpu_freq()); spln(F("MHz"));
  spln(F("********************************************"));
  
  spf( "Connecting to %s ", ssid );
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { delay(500); sp("."); }
  sp(" with IP address of "); 
  spln(WiFi.localIP());
  spln();
}

//*****************************************************************************************//
//                                      MAIN LOOP
//*****************************************************************************************//

void loop() {
  digitalWrite(BUILTIN_LED, !digitalRead(BUILTIN_LED));
  result="";
  doSendGET(x++);
  delay(5000);
}

The website page that returns the square of the incoming variable can be found here and the simple code is below:

<?php 
if (isset($_GET['in'])) {  
    echo 'The square of ' . $_GET['in'] . ' is ' . pow($_GET['in'],2);
} else {
    echo 'hello';
}
?

Now, while there appears to be only a handful of Arduino libraries available, this is a very interesting board with great potential. And at such a low price, I’m sure that this is going to garner a lot more attention and see much more development…

Stay tuned!

4 thoughts on “WeMos D1 ESP8266 WiFi Board

  1. Sol

    Hey Adrian, kudos on the great tutorial!
    Tried it the first time and worked like magic.

    Do you happen to have a working code to send a get request using Arduino (preferably the Mega 2560) with ESP8266? Haven’t had any luck with this. Tried using your wemos code on my Arduino but some libraries are not found.

    Reply
    1. Adrian Post author

      Sol.. Pleased to hear that all’s working.

      I tend to port all of my software onto the ESP8266 so that I do not have both the Arduino and ESP8266 connected together.
      As almost all of the libraries I tend to use are available for both platforms, I prefer the additional RAM and code space that the ESP gives me. The downsides are that the number of I/O is more limited and I/O is 3.3v. The latter is easy to handle using I2C expanders and when connecting to 5v sensors etc., it is necessary to add one or more level shifters, However, these are simple to make with a FET and couple of resistors and generally I only need a couple so the additional hardware is minimum.
      Hope that this helps,

      … Adrian

      Reply
  2. Mark Friesen

    How does one use the external RAM within a sketch? I’ve found examples of various WEB Servers that use images from an SD shield, but I can’t find any guidance as to reading the data from the RAM.

    Reply

Come on... leave a comment...