完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
用ESP32连接蓝牙打印机
做项目看用途,用ESP32,打印试验用因为蓝牙连接不能完成的工作。
打印机从网上购买,有一个产品,产品介绍中很少涉及单片机的使用。所有硬件硬件都在使用Windows下。 ascii代码的设置打印方式,直接接收字符串的打印。我选择的是58mm的热敏打印机,打印形式小票。 2.单片机 单片机选择了ESP32,因为软件集成了蓝牙,实现方便。 3. 软件选择用了,比较丰富的库的例子可以快速使用。我就是用ESP32中的程序来改变试验实现的。在软件中实现,BLE Utility帮助很大。3.1 BLE 通讯是低速蓝牙通讯,新的蓝牙,比如手环等,支持蓝牙等蓝牙就好像不支持了。
。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个地方:
|
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2951 浏览 16 评论
3455 浏览 1 评论
8984 浏览 16 评论
4045 浏览 18 评论
1097浏览 3评论
567浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
564浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2299浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1855浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-20 21:19 , Processed in 1.073166 second(s), Total 77, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号