Printing From ESP8266 NodeMCU

by TecnoProfesor in Circuits > Microcontrollers

14824 Views, 16 Favorites, 0 Comments

Printing From ESP8266 NodeMCU

20191110_132432.jpg

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

Getting Started

Imagen JPEG 2.jpeg
cloud print manage.jpg

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).

Configuring the Wifi Web Server

Captura de pantalla de 2019-11-19 10-35-23.png

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.

Printing "Hello From ESP8266 NodeMCU ..."

Captura de pantalla de 2019-11-19 10-37-58.png
Captura de pantalla de 2019-11-12 10-53-08.jpg
20191112_110546.jpg

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.

Printing File From FLASH Memory

Captura de pantalla de 2019-11-19 10-48-49.png
Captura de pantalla de 2019-11-13 11-48-57.jpg

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

Printing File From FLASH Memory (size < 1024 Bytes)

Captura de pantalla de 2019-11-19 10-45-00.png

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.

Printing File From SD Card

Captura de pantalla de 2019-11-19 10-48-32.png
Captura de pantalla de 2019-11-13 11-16-03.png

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

Printing File From SD Card (size < 1024 Bytes)

Captura de pantalla de 2019-11-19 10-45-24.png

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.

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.