用ESP32连接蓝牙打印机
做项目看用途,用ESP32,打印试验用因为蓝牙连接不能完成的工作。
- 没有说单片机选择与实物实现的例子,快递就是一个难题。
- 选择蓝牙打印机,使用机器和实现的介绍的文章很少,我好像找不到片子。
- 是打印机用串口但还是其他的通讯方式,打印出看到用的那种方式不是很好用的。
- 使用加密货币不支持问,第一次销售,基本没有技术。
1.使用硬件
打印机从网上购买,有一个产品,产品介绍中很少涉及单片机的使用。所有硬件硬件都在使用Windows下。 ascii代码的设置打印方式,直接接收字符串的打印。我选择的是58mm的热敏打印机,打印形式小票。
2.单片机
单片机选择了ESP32,因为软件集成了蓝牙,实现方便。
3.
软件选择用了,比较丰富的库的例子可以快速使用。我就是用ESP32中的程序来改变试验实现的。在软件中实现,BLE Utility帮助很大。3.1
BLE
通讯是低速蓝牙通讯,新的蓝牙,比如手环等,支持蓝牙等蓝牙就好像不支持了。
- Central,中心设备,其实是BLE中的客户端。
- 外设,外设,BLE中的服务器。
Central作为主设备一般是需要数据的一方,比如主机需要外部的温度信号等。外围是提供数据的外部设备,比如LED指示灯等。场景一般是设备内容开始做广告,报告的包括:
- 通用访问
- 通用属性
- 设备信息
- 未知服务
-UUID:000018f0-0000-1000-8000-00805f9b34fb
- UUID:00002af1-0000-1000-8000-00805f9b34fb
- 未知特征
还有一些是可以读取的UD。不需要的实例程序。或者不用。每个UUID现在还有数字比较大的,在BLE Utility中可以使用,但在中可以使用
。3.2软件编写
ESP32的没有用,例子程序里面有BLE_。打开项目,改成打印机的UUID,将CharacteristicUUID改成写操作的UUID。定时发送客户端的输入信息,然后改成当有输入的命令,就可以完成打印测试工作了。下面是程序。
3.3 例子程序
#include "BLEDevice.h"
//static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
static BLEUUID serviceUUID("000018f0-0000-1000-8000-00805f9b34fb");
//static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
static BLEUUID charUUID("00002af1-0000-1000-8000-00805f9b34fb");
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify)
{
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println((char*)pData);
}
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient* pclient)
{
}
void onDisconnect(BLEClient* pclient)
{
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer()
{
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient * pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server
pClient->connect(myDevice);
// if you pass BLEAdvertisedDevice instead of address,
//it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->>getService(serviceUUID);
if (pRemoteService == nullptr)
{
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
// 读取characteristic的值
if(pRemoteCharacteristic->canRead())
{
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if ( pRemoteCharacteristc->canNotify())
pRemoteCharacteristc->registerForNotify(notifyCallback);
connected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises
* the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// We have found a device, let us now see
// if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() &&
advertisedDevice.isAdvertisingService(serviceUUID))
{
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup()
{
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds. 这里我增加了时间,成功找到了打印机
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(10, false);
}
// This is the Arduino main loop function.
void loop()
{
// If the flag "doConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true)
{
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
}
// If we are connected to a peer BLE Server, update the characteristic each time we are reached
// with the current time since boot.
if (connected)
{
String newValue = "Time since boot: " + String(millis()/1000);
Serial.println("Setting new characteristic value to "" + newValue + """);
// Set the characteristic's value to be the array of bytes that is actually a string.
// pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
// 在这里进行了更改。当有外部的串口输入时,打印数据到打印机
if ( Serial.available())
{
String xStr = Serial.readString();
//int x = length(xStr);
pRemoteCharacteristic->writeValue(xStr.c_str(), xStr.length());
}
}else if(doScan)
{
BLEDevice::getScan()->start(0);
// this is just eample to start scan after disconnect,
// most likely there is better way to do it in arduino
}
delay(1000); // delay a second between loop
}
这是整个程序,注意2个地方:
- 更改了serviceUUID和CharacteristicUUID
- 更改了每次为有串口输入时才打印。
用ESP32连接蓝牙打印机
做项目看用途,用ESP32,打印试验用因为蓝牙连接不能完成的工作。
- 没有说单片机选择与实物实现的例子,快递就是一个难题。
- 选择蓝牙打印机,使用机器和实现的介绍的文章很少,我好像找不到片子。
- 是打印机用串口但还是其他的通讯方式,打印出看到用的那种方式不是很好用的。
- 使用加密货币不支持问,第一次销售,基本没有技术。
1.使用硬件
打印机从网上购买,有一个产品,产品介绍中很少涉及单片机的使用。所有硬件硬件都在使用Windows下。 ascii代码的设置打印方式,直接接收字符串的打印。我选择的是58mm的热敏打印机,打印形式小票。
2.单片机
单片机选择了ESP32,因为软件集成了蓝牙,实现方便。
3.
软件选择用了,比较丰富的库的例子可以快速使用。我就是用ESP32中的程序来改变试验实现的。在软件中实现,BLE Utility帮助很大。3.1
BLE
通讯是低速蓝牙通讯,新的蓝牙,比如手环等,支持蓝牙等蓝牙就好像不支持了。
- Central,中心设备,其实是BLE中的客户端。
- 外设,外设,BLE中的服务器。
Central作为主设备一般是需要数据的一方,比如主机需要外部的温度信号等。外围是提供数据的外部设备,比如LED指示灯等。场景一般是设备内容开始做广告,报告的包括:
- 通用访问
- 通用属性
- 设备信息
- 未知服务
-UUID:000018f0-0000-1000-8000-00805f9b34fb
- UUID:00002af1-0000-1000-8000-00805f9b34fb
- 未知特征
还有一些是可以读取的UD。不需要的实例程序。或者不用。每个UUID现在还有数字比较大的,在BLE Utility中可以使用,但在中可以使用
。3.2软件编写
ESP32的没有用,例子程序里面有BLE_。打开项目,改成打印机的UUID,将CharacteristicUUID改成写操作的UUID。定时发送客户端的输入信息,然后改成当有输入的命令,就可以完成打印测试工作了。下面是程序。
3.3 例子程序
#include "BLEDevice.h"
//static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
static BLEUUID serviceUUID("000018f0-0000-1000-8000-00805f9b34fb");
//static BLEUUID charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");
static BLEUUID charUUID("00002af1-0000-1000-8000-00805f9b34fb");
static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
uint8_t* pData,
size_t length,
bool isNotify)
{
Serial.print("Notify callback for characteristic ");
Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
Serial.print(" of data length ");
Serial.println(length);
Serial.print("data: ");
Serial.println((char*)pData);
}
class MyClientCallback : public BLEClientCallbacks
{
void onConnect(BLEClient* pclient)
{
}
void onDisconnect(BLEClient* pclient)
{
connected = false;
Serial.println("onDisconnect");
}
};
bool connectToServer()
{
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient * pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remove BLE Server
pClient->connect(myDevice);
// if you pass BLEAdvertisedDevice instead of address,
//it will be recognized type of peer device address (public or private)
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->>getService(serviceUUID);
if (pRemoteService == nullptr)
{
Serial.print("Failed to find our service UUID: ");
Serial.println(serviceUUID.toString().c_str());
pClient->disconnect();
return false;
}
Serial.println(" - Found our service");
// 读取characteristic的值
if(pRemoteCharacteristic->canRead())
{
std::string value = pRemoteCharacteristic->readValue();
Serial.print("The characteristic value was: ");
Serial.println(value.c_str());
}
if ( pRemoteCharacteristc->canNotify())
pRemoteCharacteristc->registerForNotify(notifyCallback);
connected = true;
return true;
}
/**
* Scan for BLE servers and find the first one that advertises
* the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
// We have found a device, let us now see
// if it contains the service we are looking for.
if (advertisedDevice.haveServiceUUID() &&
advertisedDevice.isAdvertisingService(serviceUUID))
{
BLEDevice::getScan()->stop();
myDevice = new BLEAdvertisedDevice(advertisedDevice);
doConnect = true;
doScan = true;
} // Found our server
} // onResult
}; // MyAdvertisedDeviceCallbacks
void setup()
{
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
BLEDevice::init("");
// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 5 seconds. 这里我增加了时间,成功找到了打印机
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setInterval(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(10, false);
}
// This is the Arduino main loop function.
void loop()
{
// If the flag "doConnect" is true then we have scanned for and found the desired
// BLE Server with which we wish to connect. Now we connect to it. Once we are
// connected we set the connected flag to be true.
if (doConnect == true)
{
if (connectToServer()) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothin more we will do.");
}
doConnect = false;
}
// If we are connected to a peer BLE Server, update the characteristic each time we are reached
// with the current time since boot.
if (connected)
{
String newValue = "Time since boot: " + String(millis()/1000);
Serial.println("Setting new characteristic value to "" + newValue + """);
// Set the characteristic's value to be the array of bytes that is actually a string.
// pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
// 在这里进行了更改。当有外部的串口输入时,打印数据到打印机
if ( Serial.available())
{
String xStr = Serial.readString();
//int x = length(xStr);
pRemoteCharacteristic->writeValue(xStr.c_str(), xStr.length());
}
}else if(doScan)
{
BLEDevice::getScan()->start(0);
// this is just eample to start scan after disconnect,
// most likely there is better way to do it in arduino
}
delay(1000); // delay a second between loop
}
这是整个程序,注意2个地方:
- 更改了serviceUUID和CharacteristicUUID
- 更改了每次为有串口输入时才打印。
举报