I've got a device, (battery charger) which has a rs232 port and a printer , which prints summary after each charge cycle. I 'm trying to replace this printer with BT one, so I connected it like this: charger->rs232 ttl adapter-> esp32 _> bluetooth serial printer.
I know serial parameters of old printer, printer is connected via Bluetooth corectly, however it prints some chinese symbols (looks like a pattern) . I use :
// Forward all data from the physical serial to the Bluetooth serial
while (Serial2.available()) {
SerialBT.write(Serial2.read());
From Esp to printer i can print correctly
When i change it to SerialBT.print, it prints a string of digits. I can't determine if old printer was in other printing mode, or code page or something
I couldn't decipher how a logo was send to a printer, so instead of that, i decided to set start sequence of redirect data to serialBT. now it is printed without a logo.
//btprinter1.8a
#include "BluetoothSerial.h"
const char startSequence[] = "Device";
const int sequenceLength = sizeof(startSequence) - 1; // Subtract 1 to exclude the null terminator
bool sequenceMatched = false;
BluetoothSerial SerialBT;
const int LED_PIN = LED_BUILTIN;
const int BUTTON_PIN = 26;
bool buttonState = false;
unsigned long buttonPressTime = 0;
unsigned long lastButtonPressTime = 0;
const unsigned long DEBOUNCE = 50;
bool restartRequested = false;
uint8_t address1[6] = {0x66, 0x22, 0x45, 0xEB, 0x8B, 0x75};
bool connected = false;
#define RXD2 16
#define TXD2 17
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
SerialBT.begin("ESP32test", true);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), restartButtonPressed, FALLING);
SerialBT.setPin("0000");
Serial.println("The device successfully started!");
connected = SerialBT.connect(address1);
if (connected) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Connected successfully!");
SerialBT.print("\n");
SerialBT.print("Drukarka podlaczona \n");
SerialBT.print("przez Bluetooth!\n");
SerialBT.print("\n");
SerialBT.write(0x1B);
SerialBT.print('@');
Serial.write(0x1B);
Serial.write(0x1C);
Serial.write(0x2E);
} else {
while (!SerialBT.connected(5000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
for (int i = 0; i <= 2; i++) {
digitalWrite(LED_PIN, HIGH);
delay(400);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
}
}
void loop() {
if (Serial2.available()) {
char c = Serial2.read();
if (!sequenceMatched && c == startSequence[0]) {
//SerialBT.print('D');
sequenceMatched = true;
}
if (sequenceMatched) {
SerialBT.print(c);
}
// Check if restart button has been pressed
if (restartRequested) {
restartRequested = false;
delay(500);
Serial.println("Restarting...");
ESP.restart();
}
}
}
void restartButtonPressed() {
buttonPressTime = millis();
if (buttonPressTime - lastButtonPressTime > DEBOUNCE) {
Serial.println("Restarting...");
delay(500);
ESP.restart();
}
}
Next goal is to find out how to print own bitmap to raw bluetooth printer