|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
Corvette T1与esp32通信实现安卓手机控制这次打算测试下corvette T1开发板(以下简称T1)的通讯能力,结合自己手头上的esp32 wroom,打算将两者进行以下通讯试试看,
5 ` L- @# M9 r m5 G/ d
9 e# c5 ]' y2 i) i最后可以通过手机发送指令,来控制灯的亮灭。
8 t8 c7 A! b' G3 z使用到esp32的蓝牙串口通讯以及T1板子的串口和两个led灯
8 y3 S" @! ~; d! j/ u实现效果如下: k" e4 u+ E+ u( e0 a {3 ^& T P6 g
通过手机输入a和b指令来分别控制2号引脚led灯闪烁13号引脚led灯常亮
0 g& l$ \4 P( G9 K/ ~" C![]() * _1 i* Q. T1 ]% N
可以看到T1开发板的背面有绿光,便是13引脚的led常亮的效果
% R: B+ r2 X0 K A8 n2 _+ h; C* {; _: }7 T
对esp32烧写以下代码7 d( i) Z, h; L: v0 Q2 P; i
//This example code is in the Public Domain (or CC0 licensed, at your option.)//By Evandro Copercini - 2018
# {+ I c$ B, b% x//
! r( H+ s5 X8 B$ M3 n( r+ b+ v//This example creates a bridge between Serial and Classical Bluetooth (SPP)
7 G6 e: S/ D S* b/ E- M1 c, p//and also demonstrate that SerialBT have the same functionalities of a normal Serial
5 {5 p" h+ Z$ m5 ~9 j' ~ I#include "BluetoothSerial.h"
- q# w" C$ Q# C7 f( \6 b#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
8 G& J% @$ h% x/ A+ ~#error Bluetooth is not enabled! Please run make menuconfig to and enable it
) P5 m( P9 Y7 i* G+ Q. ^' I#endif# ?, @% C; k# Y$ {) U
#if !defined(CONFIG_BT_SPP_ENABLED)
4 G% Q8 W! L' |; t2 W#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.+ m( a! B+ T$ p# `
#endif
0 o8 o9 {( u1 f5 w8 p/ I& w6 ?0 J0 BBluetoothSerial SerialBT;$ B' @, {4 ^8 Q- X( p- w
void setup() {7 d K9 q* Z, W6 b m0 k
Serial.begin(115200);
& N9 y, L% Q& r# A7 N2 ?SerialBT.begin("ESP32test"); //Bluetooth device name
0 s! v/ W: l8 O+ J, a9 ~5 w+ kSerial.println("The device started, now you can pair it with bluetooth!");
) { _0 k% @. T3 T- d- w}" j8 m# C/ l' p6 b, b
void loop() {
" H. m$ P5 x0 L' j. y" Wif (Serial.available()) {
5 H E' Y) Y& Z, }2 `4 d4 l3 G1 CSerialBT.write(Serial.read());! r1 n( O+ F* w3 i- b
}
2 S* g& ~' @. h) V/ n& rif (SerialBT.available()) {6 q Q6 k M6 E6 h( `5 A
Serial.write(SerialBT.read());/ _2 P# k7 T. Z6 x& W! ?
}
# Y a) ^3 }" V5 }$ w0 udelay(20);
' V6 K) D2 F) Q2 Q# f {* O+ K5 ]% A}
) A+ i8 l5 R* o4 j y" a6 j/ V5 c
对T1开发板烧写如下代码$ P& T; s) E3 K# }
int i ;void setup() {
8 A& T% K/ {; `, M4 x3 M& a4 ?/ {Serial.begin(115200); a7 U! y1 C+ d
pinMode(2, INPUT);3 H7 K: V# C& V) z
pinMode(3, OUTPUT);
$ Z8 P0 K# c: R, rpinMode(13, OUTPUT);( N, C6 _3 ?8 B( m7 L% I
}
r, e1 P+ M8 kvoid loop() {# ]# s! O& \' C: q
if (Serial.available())1 q& @) J) \+ k
{
6 u `* a8 a" H" G; Q! a5 g// Serial.write(Serial.read());//输出原数据格式( w( E* t Z$ ]5 c* e
i = Serial.read();
7 R& |, I' @) R# y( Z9 D// Serial.println(i);//输出可显示的ascll值! V, _) R: t3 c9 {+ s
Serial.write(i);0 t6 U9 E9 E9 Q- G ^
Serial.write('
- k5 w9 z+ }# ?5 z" t4 O! Z* O7 Z8 h');
. ?/ r+ m7 d+ B) z1 D# Z}7 F% f8 \2 v& V7 z s$ _
if (i == 'a')
+ i3 T% E% ?. j{; V$ s, W) @% j6 _
digitalWrite(2, HIGH);
& {9 Y g, i( X) {- \" tdelay(1000);
7 h# l& _/ [: @digitalWrite(2, LOW);1 L( \% p4 O: V' m
delay(1000);( r4 r( e* i( e- f5 ~
}2 |, |; ^6 _7 l0 Q1 j+ A
if (i == 'b')
+ Q5 _& u" P0 N3 x' V{( x) b0 r8 M8 o3 C9 L& j* N
digitalWrite(13, HIGH);
% ^4 R" c: r0 J1 \}# h* O; i+ m" Z' w" j
}
) a! Q0 w. L9 G7 c' H+ g$ h3 j5 I, ~1 w. S. y/ w- ?
其他设置esp32 wroom使用引脚RX0、TX0,连接到T1板子的IO1,IO0(RX0->IO1,TX0->IO0)
1 G0 A7 m V4 E% Q7 U9 [, @手机随便下个带有编码设置的蓝牙串口调试助手(ps:不是蓝牙调试助手)
2 O8 b, |4 s& m将编码调成utf-8,就可以输入命令并点灯了。! P0 f- S" o/ p9 ]% [9 m+ U
![]()
k& k- e) t$ h. z9 Z0 R注意事项手机蓝牙发送的编码一定要对可以是gbk、utf-8等编码,但是如果使用错的编码方式那么就会接收不到信号,串口打印数据就会乱码,如下图 # ^1 M. ^8 @; i5 D8 I! p. U; u
除此之外,esp32和T1开发板的串口波特率也要对齐。
: T; S( @% \: T5 y$ Z0 a( i, }% k以上便是本次设计的全部内容了。
. y2 k4 z2 f* O3 y; J8 t6 h3 @* o \+ n
|
|