Printing From ESP8266 NodeMCU

This project shows you an easy way to print data or files from an ESP8266 nodeMCU device configurated as a Wifi Web Server to a remote printer configurated by Google Cloud Print using Google Cloud Print Web Element to add GCP functionality to the web site. In that way you will be able to print those data or files from a desktop computer or from whatever mobile device.

As far as I know this solution works on whatever platform.

I hope this project be useful for somebody, but take into account that the Google Cloud Print technology will not be available by the end 2020

Supplies:

One ESP8266 nodeMCU

One SPI Micro SD card reader

Wires

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Getting Started

First we have to add printers to the cloud using Google Cloud Printer. For this project I have used my EPSON printer.

Go to the computer where your printer is connected and do the following steps:

  1. Enter Google Chrome Web Browser
  2. Sign in with a Google Account
  3. Go to Settings => Advanced => Printing => Google Cloud Print => Manage Cloud Print devices
  4. Press the button "Add printers"
  5. Select a printer device and press "Add printer(s)" (PICTURE 1)

To manage the cloud printer(s) or to print data or files, type https://www.google.com/cloudprint#printers in a web browser from a desktop computer or mobile phone (PICTURE 2).

Step 2: Configuring the Wifi Web Server

After you have downloaded the sketch you have to do the following:

  • Update the parameters of the "config.h" to connect to your wifi network.
  • Load the sketch to the ESP8266 board

After that when you type the IP address assigned to your ESP8266 board in your wifi network from a desktop computer, you will see the web page you see in the picture

Basically it shows several tests to print messages or files (data loggers) from FLASH memory or SD card.

Step 3: Printing "Hello From ESP8266 NodeMCU ..."

Typing in your web browser (ESP8266 IP address)/TestPrint1 will show you a print button to execute the test.

After pressed the button a printer dialog will ask you to select a cloud printer.

In the "HandleFileRead()" function in the "ESP8266_Utils_Server.hpp" file you can find the code to print the message as you can see in the PICTURE 1.

In the PICTURE 2 you can see the web page for the TestPrint1 and the Google Cloud Print dialog to select a cloud printer once you have pushed the default printer button.

The PICTURE 3 show you the same Google Cloud Print dialog from a mobile phone.

In this case you don´t need Chrome or Chromium web browser to execute the test.

Step 4: Printing File From FLASH Memory

Typing in your web browser (ESP8266 IP address)/TestPrint2 you will see the web page you can see in the PICTURE 1.

Every time the sketch begins, the code creates in the FLASH memory the file "FLASHtemplogger.txt" with the content you can see in the picture. It simulates a temperature logger.

The part of the code that executes this test in ESP8266_Utils_Srver.hpp is the following:

  // Printing from FLASH memory
  if (path.indexOf("TestPrint2") > 0) {
    if (SPIFFS.exists(FLASHtemplogger)) 
    {
      ServeFile(FLASHtemplogger);
      return true;
    }
  }

In this case you need Chrome or Chromium web browser to print the content of the file as you can see in the PICTURE 2. At the same time observe you can select local printers or cloud printers

Step 5: Printing File From FLASH Memory (size < 1024 Bytes)

Typing in your web browser (ESP8266 IP address)/TestPrint3 you will see a web with a default print button as you can see in the upper left corner of the picture.

I have fixed a 1024 bytes limit for the size of the file, but it could be greater. You have to review the maximum string size you can include as a parameter in a function to stablish the limit.

This test will print the content of the previous file "FLASHtemplogger.txt". Basically read the content of the file and send to the client as a string paramater of the "gadget" function (right side of the picture).

The part of the code that executes this test in ESP8266_Utils_Srver.hpp is the following:

  // Printing File From FLASH Memory (size < 1024 Bytes)<br>  if (path.indexOf("TestPrint3") > 0) {
    if (SPIFFS.exists(FLASHtemplogger)) 
    {
      File templogfile = SPIFFS.open(FLASHtemplogger,"r");
      if (templogfile.size() < 1024) {
        String stringtoprint = "";
        while (templogfile.available()) {
          if (not (stringtoprint == "")) stringtoprint = stringtoprint + "+\n";
          String s=templogfile.readStringUntil('\n');
          stringtoprint = stringtoprint + "'" + s.substring(0,s.length()-1) + "'";
        }
        templogfile.close();    
        String gadget = "gadget.setPrintDocument('text/html', 'TEST 3'," + stringtoprint + ");";
        server.setContentLength(sizeof(HTML_PART_G1) + gadget.length() + sizeof(HTML_PART_G2));
        server.sendContent(HTML_PART_G1);
        server.sendContent(gadget);
        server.sendContent(HTML_PART_G2);
        return true;
      }
    }    
  }

In this case you don´t need Chrome or Chromium web browser to execute the test.

Step 6: Printing File From SD Card

Typing in your web browser (ESP8266 IP address)/TestPrint4 you will see the web page you can see in the PICTURE 1.

Every time the sketch begins, the code creates in the SD card the file "SDtemplogger.txt" with the content you can see in the picture. It simulates a temperature logger.

The part of the code that executes this test in ESP8266_Utils_Srver.hpp is the following:

  // Printing File From SD Card
  if (path.indexOf("TestPrint4") > 0) {    
    if (SD.exists(SDtemplogger)) {
      File myFile = SD.open(SDtemplogger);
      if (myFile) {
        size_t sent = server.streamFile(myFile, "text/html");
        myFile.close();
        return true;
      }
    }
  }

In this case you need Chrome or Chromium web browser to print the content of the file as you can see in the PICTURE 2. At the same time observe you can select local printers or cloud printers

Step 7: Printing File From SD Card (size < 1024 Bytes)

Typing in your web browser (ESP8266 IP address)/TestPrint5 you will see a web with a default print button as you can see in the upper left corner of the picture.

As I have explained above I have fixed a 1024 bytes limit for the size of the file but it is possible to be greater.

This test will print the content of the previous file "SDtemplogger.txt".

Basically read the content of the file and send to the client as a string paramater of the "gadget" function The part of the code that executes this test in ESP8266_Utils_Srver.hpp is the following:

    // Printing File From SD Card (size < 1024 Bytes)  <br>  if (path.indexOf("TestPrint5") > 0) {
    if (SD.exists(SDtemplogger)) 
    {
      File templogfile = SD.open(SDtemplogger);
      if (templogfile.size() < 1024) {
        String stringtoprint = "";
        while (templogfile.available()) {
          if (not (stringtoprint == "")) stringtoprint = stringtoprint + "+\n";
          String s=templogfile.readStringUntil('\n');
          stringtoprint = stringtoprint + "'" + s.substring(0,s.length()-1) + "'";
        }
        templogfile.close();
      
        String gadget = "gadget.setPrintDocument('text/html', 'TEST 5'," + stringtoprint + ");";
        server.setContentLength(sizeof(HTML_PART_G1) + gadget.length() + sizeof(HTML_PART_G2));
        server.sendContent(HTML_PART_G1);
        server.sendContent(gadget);
        server.sendContent(HTML_PART_G2);
        return true;
      }
    }        
  }

In this case you don´t need Chrome or Chromium web browser to execute the test.

Step 8: The Code

One of the most interesting gadgets of this code is "setPrintDocument()" from de Google API GCP:

gadget.setPrintDocument("[document mimetype]",
                        "[document title]",
                        "[document content]",
                        "[encoding] (optional)")

Using this gadget you can print several type of information: images, url, Google Drawing Documents, Google Drive Files, Google Document, url content, .... in addition to most standard document/image formats.

You can find the description of this gadget here.

Be the First to Share

    Recommendations

    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    6 Discussions

    0
    None
    diy_blokeTecnoProfesor

    Reply 13 days ago

    yes ofcourse, I was just trying to temper the expectations of possible users diving into this

    1
    None
    TecnoProfesordiy_bloke

    Reply 13 days ago

    Thank you again
    A few days ago I included a comment regarding this issue in black and underline text at the beginning of the instructable

    0
    None
    TecnoProfesordiy_bloke

    Reply 19 days ago

    Yes, you are right.
    Google has announced that Cloud Print is being retired at the end of next year.
    Anyway they say in the same notice they has been improved the native printing experience only for Chrome OS, and will continue adding features to native printing. But for environments besides Chrome OS they recommend that over the next year execute a migration strategy.
    When I published the project I didn't know the notice