Advertisement
FlyFar

Hack an Epson TM-T88 thermal printer and remotely control it through a WiFi-enabled ESP32

Aug 12th, 2023
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.65 KB | Cybersecurity | 0 0
  1. /**
  2.  * Copyright 2017 Dan Oprescu
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *    
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. /*
  18. Hack an Epson TM-T88 thermal printer (the ones that print receipts in a supermarket till) and remotely control it through a WiFi-enabled ESP32.
  19.  
  20. This code :
  21. - connects to a given WiFi network
  22. - starts an HTTP server on port 80
  23. - serves a basic page that allows one to type some text in a text area
  24. - once a button is pressed it sends that text through a serial link to the thermal printer and tells it to cut the paper at the end
  25. - also, the setup steps are printed on the integrated OLED display
  26. */
  27.  
  28. HardwareSerial serialPrinter(1);
  29.  
  30. #include <WiFi.h>
  31. #include <ESP32WebServer.h>
  32. #include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
  33.  
  34. ESP32WebServer server(80);
  35.  
  36. // Initialize the OLED display using Wire library
  37. SSD1306  display(0x3c, 5, 4);
  38.  
  39.  
  40. void setup() {
  41.   Serial.begin(500000);
  42.  
  43.   display.init();
  44.   display.drawString(0, 0, "Starting...");
  45.   display.display();
  46.  
  47.  
  48.   serialPrinter.begin(19200, SERIAL_8N1, 25, 26);
  49.  
  50.  
  51.  
  52.   display.drawString(0, 10, "Connecting to WiFi...");
  53.   display.display();
  54.   WiFi.begin("SSID", "password");
  55.   /*wait until ESP32 connect to WiFi*/
  56.   while (WiFi.status() != WL_CONNECTED) {
  57.       delay(500);
  58.       Serial.print(".");
  59.   }
  60.   display.drawString(0, 20, "\nWiFi connected, IP address: ");
  61.   display.drawString(0, 30, WiFi.localIP().toString());
  62.   display.display();
  63.  
  64.  
  65.   server.onNotFound(handleNotFound);
  66.   server.on("/", menu);
  67.   server.on("/print", sendToPrinter);
  68.  
  69.  
  70.   server.begin();
  71.   display.drawString(0, 40, "HTTP started on port 80");
  72.   display.display();
  73. }
  74.  
  75.  
  76.  
  77. void loop() {
  78.   // deals with the Wifi clients and responds, calls the callbacks, etc. ...
  79.   server.handleClient();
  80.   delay(1);
  81. }
  82.  
  83.  
  84.  
  85. void menu() {
  86.   Serial.println("Menu...");
  87.   String message = "<h1>Thermal Printer</h1>";
  88.   message += "<form action=\"/print\" id=\"frm\"><input type=\"submit\" value=\"Send to printer\"  style=\"height:200px;width:200px\"> </form>";
  89.   message += "<textarea rows=\"10\" cols=\"50\" name=\"text\" form=\"frm\"> Enter text here...</textarea>";
  90.   server.send(200, "text/html", message);
  91. }
  92.  
  93. void sendToPrinter() {
  94.   // we expect one and only 1 var which is the text to print
  95.   String var = server.argName(0);
  96.   String value = server.arg(0);
  97.  
  98.  
  99.   if (var == "text") {
  100.     Serial.print("Received to print: "); Serial.println(value);
  101.     serialPrinter.println(value);
  102.     cutPaper();
  103.    
  104.   }
  105.   else Serial.println("UNKNOWN var " + var );
  106.  
  107.   menu();
  108. }
  109.  
  110.  
  111. void cutPaper() {
  112.   //extra empty rows to cut below the latest text
  113.   serialPrinter.println("\n\n\n");
  114.   byte cutCmd[] = {0x1B, 0x69, 0x0A};
  115.   serialPrinter.write(cutCmd, sizeof(cutCmd));
  116. }
  117.  
  118. void handleNotFound() {
  119.   String message = "File Not Found\n\n";
  120.   message += "URI: " + server.uri();
  121.   message += "\nMethod: " + (server.method() == HTTP_GET)?" GET":" POST";
  122.   message += "\nArguments: " + server.args();
  123.   message += "\n";
  124.   for (uint8_t i=0; i<server.args(); i++){
  125.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  126.   }
  127.   server.send(404, "text/plain", message);
  128. }
  129.  
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement