51单片机物联网智能小车系列文章目录
第一篇:最简单DIY的51蓝牙遥控小车设计方案
第二篇:最简单DIY串口蓝牙硬件实现方案
文章目录
前言
一、最简单DIY串口蓝牙硬件实现方案是什么?
ESP32充当电脑串口蓝牙遥控蓝牙设备
二、制作步骤
1.搭建ESP32开发环境
1.2搭建Arduino开发环境,不会搭建开发环境的买家可以到:https://www.cirmall.com/circuit/19141 自行按照说明搭建。
2.下载代码
//This example code is in the Public Domain (or CC0 licensed, at your option.) //By Victor Tchistiak - 2019 // //This example demostrates master mode bluetooth connection and pin //it creates a bridge between Serial and Classical Bluetooth (SPP) //this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018 // //author by:daodanjishui 2020.10.10 #include "BluetoothSerial.h" BluetoothSerial SerialBT; String MACadd = "AA:BB:CC:11:22:33"; uint8_t address[6] = {
0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; //uint8_t address[6] = {0x20, 0x18, 0x04, 0x15, 0x25, 0x13}; //String name = "OBDII"; String name = "HC-05"; char *pin = "1234"; //<- standard pin would be provided by default bool connected; void setup() {
Serial.begin(); //SerialBT.setPin(pin); SerialBT.begin("ESP32test", true); //SerialBT.setPin(pin); Serial.println("The device started in master mode, make sure remote BT device is on!"); // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs // to resolve name to address first, but it allows to connect to different devices with the same name. // Set CoreDebugLevel to Info to view devices bluetooth address and device names connected = SerialBT.connect(name); //connected = SerialBT.connect(address); if(connected) {
Serial.println("Connected Succesfully!"); } else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); } } // disconnect() may take upto 10 secs max if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!"); } // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address). SerialBT.connect(); } void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read()); } if (SerialBT.available()) {
Serial.write(SerialBT.read()); } delay(20); }
程序说明:用的是arduino写的工程,读者可以慢慢斟酌,如果为了成功,可以购买跟我一样型号的串口蓝牙模块。
3.根据软件和硬件完成硬件连接
三、仿真与调试
1. 准备好硬件,小车上电和打开arduino“串口监视器”,输入指令,点击发送。
2. 接收小车返回的响应
总结
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211759.html原文链接:https://javaforall.net
