Go Down

Topic: Using ESP32 with a thermal printer Adafruit (Read 460 times) previous topic - next topic

Daedevils

Hi,
my girlfriend and I would like to create a smart object.
If you read a QR code on the object via the phone, you will be redirected to a web page created with 3 buttons (menu1, menu2, menu3).
By clicking these buttons you should go and have the different menus printed by the thermal printer.

initially, through the suggestion of a university professor we have been advised against using the code inside the loop, but in this way the printer goes to print just one of the menus giving problems if you request printing the second time of any of the menu.

this is the code

>>#include <FS.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>


#include "WiFi.h"
#include "Adafruit_Thermal.h"


const char* ssid = "dlink";
const char* password =  "mikcazzutociao";

#define TX_PIN 19 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 23 // Arduino receive   GREEN WIRE   labeled TX on printer

//HardwareSerial Serial1(1);
HardwareSerial Serial2(2);


//SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first

//Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor
Adafruit_Thermal printer(&Serial2);      // Or Serial2, Serial3, etc


AsyncWebServer server(80);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  //Serial1.begin(9600, RX_PIN, TX_PIN);
  Serial2.begin(9600, SERIAL_8N1, RX_PIN, TX_PIN);



  // setup wifi:
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connected to the WiFi network");
  Serial.println(WiFi.localIP());

  //setup stampante

  Serial.println("Setup Completed");


  // setup web server:
  server.on("/menu1", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(200, "text/html", "<p>This is Menu1!</p>");
    Serial.println("inizia stampa");

    printer.begin();        // Init printer (same regardless of serial type)
    printer.setSize('L');        // Set type size, accepts 'S', 'M', 'L'
    printer.println("Menu per Roberto");
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Antipasti");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Mini tartare di filetto \ndi Angus argentino\nCicoria e fave\nFiori di zucca in tempura");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Primi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Risotto alla milanese \n(senza lattosio)\nTortelli all'entrana su crema \ndi formaggio (senza lattosio)\nPaccheri al ragu");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Secondi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Filetto di Angus ai ferri \ncon patate al forno\nFiletto di salmone servito \nsu asparagi gratinati\nRombo chiodato servito \ncon fagiolini e salsa guacamole\nAnatra all'arancia");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Dessert");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Gelato al latte di mandorla\nSorbetto al limone (senza latte)");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
   
    printer.feed(2);

    printer.sleep();      // Tell printer to sleep
    delay(3000L);         // Sleep for 3 seconds
    printer.wake();       // MUST wake() before printing again, even if reset
   
    /// stampa menu 1
    Serial.println("fine stampa");
  });

  server.on("/menu2", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(200, "text/html", "<p>This is Menu2!</p>");
    Serial.println("inizia stampa");

    printer.begin();        // Init printer (same regardless of serial type
    printer.setSize('L');        // Set type size, accepts 'S', 'M', 'L'
    printer.println(F("Menu per Marina"));
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Antipasti");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Mini tartare di filetto \ndi Angus argentino\nCicoria e fave\nBurratina fresca");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Primi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Risotto alla milanese\nSpaghetti cacio e pepe\n(senza glutine)");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Secondi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Filetto di Angus ai ferri \ncon patate al forno\nFiletto di salmone servito \nsu asparagi lessi\nRombo chiodato servito \ncon fagiolini e salsa guacamole\nAnatra all'arancia");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Dessert");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Gelato\nSorbetto al limone\nCreme brulee");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
   
    printer.feed(2);

    printer.sleep();      // Tell printer to sleep
    delay(3000L);         // Sleep for 3 seconds
    printer.wake();       // MUST wake() before printing again, even if reset
   
    /// stampa menu 2
    Serial.println("fine stampa");
  });

  server.on("/menu3", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(200, "text/html", "<p>This is Menu3!</p>");
    Serial.println("inizia stampa");

    printer.begin();        // Init printer (same regardless of serial type
    printer.setSize('L');        // Set type size, accepts 'S', 'M', 'L'
    printer.println(F("Menu per Lidia"));
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Antipasti");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Mini tartare di filetto \ndi Angus argentino\nCicoria e fave\nFiori di zucca in tempura\nBurratina fresca");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Primi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Risotto alla milanese\nTortelli all'entrana su crema \ndi formaggio\nPaccheri al ragu\nSpaghetti cacio e pepe");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Secondi");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Filetto di Angus ai ferri \ncon patate al forno\nAnatra all'arancia");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
    printer.feed(1);

    printer.setSize('M');       
    printer.println("Dessert");
    printer.feed(1);

    printer.setSize('S');
    printer.println("Gelato\nSorbetto al limone\nCreme brulee");
    printer.setLineHeight(); // Reset to default
    printer.justify('L');
   
    printer.feed(2);

    printer.sleep();      // Tell printer to sleep
    delay(3000L);         // Sleep for 3 seconds
    printer.wake();       // MUST wake() before printing again, even if reset
   
    /// stampa menu 3
    Serial.println("fine stampa");

  });

  server.begin();

}

void loop() {
  // put your main code here, to run repeatedly:

}

<<

trying to insert in the loop the part dedicated to the printing (//set up web server) of the menus continues to connect, wait for the click of one of the buttons and disconnect within a few seconds by not allowing me to click the buttons.



Help me please and sorry for my english.

Andrea

rw950431

#1
Jun 17, 2018, 09:23 am Last Edit: Jun 17, 2018, 09:25 am by rw950431
Welcome to the forum!   If you put the code tags (</> ) around your code its much easier for everyone to read.


If you comment out all the actual thermal printing code and just leave the serial print statements in each server.on() section does it work OK? 

Code: [Select]
server.on("/menu1", HTTP_GET, [](AsyncWebServerRequest * request) {
 request->send(200, "text/html", "<p>This is Menu1!</p>");
 // stampa menu 1
 Serial.println("fine stampa");
 });


It may be that the thermal print is taking too long and causing a problem in the Web server.

Go Up