Enable Javascript


Last Arduino/ESP8266 project (click to open)

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

Article Index


BOOST your Arduino with this Shield

(click thumbnail)
Wiring, information and demo output

 Amica NodeMCU V2 DEVKIT with ESP8266-12E


NodeNCU v2 Shield:
Boost up your Arduino with this cheap ESP8266-12E Shield.
Use the 32 bit RISC CPU with 4 Mb memory, WiFi STA and/or AP, 2-32 GB micro SD card, 7 free GPIO and 1 Analog ports, 2 LEDs and 1 button. Then you can use your Arduino for important things like reading sensors, controlling stepper motors, ... .

Hardware Serial/USB:
Total unhindered to use for firmware flash and the debug monitors. No fiddling with jumpers and buttons.

SoftwareSerial 3.3V ↔ 5V:
Using a 5V Arduino? Use a 3.3V↔5V (Bi-Directional) 'Level Converter' to save your ESP8266.

Pull-up resistor:
Use a 10 kΩ pull-up resistor on te SS (Chip Select) line.

SD Filesystem:

  • Arduio ESP8266 standard SD libray
    Simple and useful (see the sample pages below).
    You can use all the Stream/Serial functions
  • ChaN FatFs (Fat Filesystem)
    Stripped and modified for using with the SD adapter on this Shield.
    (dir functions are stripped, only ROOT is used)
    It's smaller than the Arduino SD library.
    FatFs Library with example: download SdFatFsESP
    With this Arduino NodeMCU v2 Shield the FatFs library it uses:
    • 8.946 bytes of program storage space (<1%).
    • 820 bytes of dynamic memory (~1%).
  • FatFs SD commands:
    • mount - Register/Unregister a work area.
    • open - Open/Create a file-
      READ, WRITE, OPEN_EXISTING, OPEN_ALWAYS
      CREATE_NEW, CREATE_ALWAYS
    • close - Close an open file
    • read - Read file
    • write - Write file
    • lseek - Move read/write pointer, Expand file size
    • sync - Flush cached data
    • gets - Read a string
    • putc - Write a character
    • puts - Write a string
    • tell - Get current read/write pointer
    • eof - Test for end-of-file on a file
    • size - Get size of a file
    • error - Test for an error on a file
    • f_printf - Write a formatted string to file

 Demo sketches with Serial and Arduino SD library on the next two pages.


ESP8266:

/*  DEMO Arduino + NodeMCU Shield
 *   With softwareSerial and micro SD
 *   Edited SD and SoftwareSerial examples
 *****************************************/

#include <SPI.h>             // https://github.com/esp8266/Arduino
#include <SD.h>              // https://github.com/esp8266/Arduino
#include <SoftwareSerial.h>  // https://github.com/esp8266/Arduino
#include <Streaming.h>       // http://arduiniana.org/libraries/streaming

File root;
SoftwareSerial serial(D1, D2, false, 128);  // RX-D1->UNO-TX, TX-D2->UNO-RX

void listFiles();
void printDirectory(File, int);
void show(String);

void setup() {
  Serial.begin(115200);  // to monitor (DEBUG)
  serial.begin(38400);   // to Arduino
  while (!Serial) {};    // wait for serial port to connect. Needed for native USB port only
  for (uint8_t t = 5; t > 0; t--) { 
    Serial.println();
    Serial.flush();
    delay(1000);
  }

  show("\n\nESP8266 Software serial test started\n");
  listFiles();
  show("\nType text to send...\n");
}

void loop() {
  while (serial.available()) Serial << serial.readString();  // from Arduino
  while (Serial.available()) serial << Serial.readString();  // from ESP8266 Monitor to Arduino
}

void listFiles() {
  show("_______________________\nInitializing SD card...\n");
  if (!SD.begin(D4)) {
    show("initialization failed!\n");
    return;
  }
  show("initialization done.\n");
  root = SD.open("/");
  printDirectory(root, 0);
  show("SD card done!\n_______________________\n\n");
}

void printDirectory(File dir, int numTabs) {  // get a list of SD files 
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       show("\t");
     }
     show(entry.name());
     if (entry.isDirectory()) {
       show("/\n");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       show("\t\t");
       show(String(entry.size(), DEC) + "\n");
     }
     entry.close();
   }
}

void show(String s) {  // show it on Arduino and ESP8266 monitors
  Serial << s;
  serial << s;
}


Arduino:

/*  DEMO Arduino + NodeMCU Shield
 *   With softwareSerial and micro SD
 *   Edited SoftwareSerial example
 ************************************/

#include <SoftwareSerial.h>
// using serial.read(): If lost characters then try AltSoftSerial
//              =====
//#include <AltSoftSerial.h>  // http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html 
#include <Streaming.h>  // http://arduiniana.org/libraries/streaming

SoftwareSerial serial(8, 9); //  RX-D8->ESP-TX, TX-D9->ESP-RX
//AltSoftSerial serial; //  RX-D8->ESP-TX, TX-D9->ESP-RX

String inBuf = "";

void setup() {
  Serial.begin(115200);
  serial.begin(38400);
  while (!Serial) {}  // wait for serial port to connect. Needed for native USB port only
  char s[] = "\n\nArduino Software serial test started\n";
  Serial << s;
  serial << s;
  Serial << "Wait for ESP8266 message...\n";
}

void loop() {
  while (serial.available()) Serial << (serial.readString());  // from ESP8266
  while (Serial.available()) serial << (Serial.readString());  // from Arduino Monitor to ESP8266

}