|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
Corvette T1与esp32通信实现安卓手机控制这次打算测试下corvette T1开发板(以下简称T1)的通讯能力,结合自己手头上的esp32 wroom,打算将两者进行以下通讯试试看,; c1 j4 g5 q: G: Z8 y
% ~+ a# `4 b0 ?3 Q* ]
最后可以通过手机发送指令,来控制灯的亮灭。
4 i t( U6 A c7 O# a3 V+ z5 P使用到esp32的蓝牙串口通讯以及T1板子的串口和两个led灯$ {0 {2 L/ V+ H; o% f' u: O/ Q
实现效果如下:
; L+ k- L& p" Z. F1 ?1 M4 i# z0 W通过手机输入a和b指令来分别控制2号引脚led灯闪烁13号引脚led灯常亮
* R% I5 ?: h* }( x# {![]() ![]()
3 W# W1 B }! g) A: Q( P3 N5 H可以看到T1开发板的背面有绿光,便是13引脚的led常亮的效果
# x! l1 s P5 Q
. }8 {/ A! V, T2 ^7 s: Y! O对esp32烧写以下代码
# Y# n, e/ e# ^3 U//This example code is in the Public Domain (or CC0 licensed, at your option.)//By Evandro Copercini - 2018 J* w# {+ g8 M
// t! R6 o( m3 e4 s0 ^$ a% X3 W' B
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
' J; f! Y) M6 c1 e7 x7 B6 _0 O//and also demonstrate that SerialBT have the same functionalities of a normal Serial
1 e3 q; Y' _ q#include "BluetoothSerial.h"7 T0 h5 S# t! x4 b- Q
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)# `2 }$ w! @, t" F* e
#error Bluetooth is not enabled! Please run make menuconfig to and enable it
* l; d8 H8 g+ U7 |" _ w#endif* s) i' A- K: D* x( ]
#if !defined(CONFIG_BT_SPP_ENABLED) ^. W! `$ V" h: u8 t [4 U; _
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.! P+ d+ b/ a( ?
#endif5 r% A2 u; W5 a' t# |& u
BluetoothSerial SerialBT;; s* V5 t" w) ~1 p% P7 N" N
void setup() {
$ {) j0 X& E2 O. B1 k) uSerial.begin(115200);' C" x; D1 Z& i5 C! T, n" E
SerialBT.begin("ESP32test"); //Bluetooth device name5 {& H$ w% B% ^/ G, M! u
Serial.println("The device started, now you can pair it with bluetooth!");
+ S P5 G+ }5 l7 w4 o7 h* O4 Q* \}+ P; Y6 T9 V8 A& c9 K% R; b
void loop() {
9 [' I1 q2 V8 _/ B8 _; eif (Serial.available()) { X! [0 b# L3 }8 S- Y8 p
SerialBT.write(Serial.read());
+ X, b2 _, t7 `) E}3 g) T. i$ Q# u3 y- D
if (SerialBT.available()) {
8 L; e; |2 h5 r* r$ @ vSerial.write(SerialBT.read());' C- o+ [" }& j9 f. I; s, d: e
}, }% y: Y! i9 m" T) U5 o- {
delay(20);
6 e* O2 n4 ^/ [5 P8 K, _" G$ v}
- Z; U* I0 S: U U. Z
0 S5 M$ S2 u6 F对T1开发板烧写如下代码
) b, U* J4 A' K( N) |3 hint i ;void setup() {* x* G# L% H5 g& P9 \" o
Serial.begin(115200);
8 r8 |4 y" ^3 t7 w3 jpinMode(2, INPUT);, V! w$ H5 ]- J4 c+ `4 w7 i2 n; T; s
pinMode(3, OUTPUT);6 ~; X3 U& c2 {9 B; O6 v: m
pinMode(13, OUTPUT);6 u) e* [6 P7 \: ?2 @" P/ e
}
( t5 V5 J3 _# e. t$ m9 V4 L0 q! _void loop() {% R9 M a- F) i, @1 k+ C; j. x% x
if (Serial.available())
7 A3 ]: [$ B9 ^; c# d{
1 `" x" E! ]" T- W2 ^! f) _& H' _// Serial.write(Serial.read());//输出原数据格式& @7 z; r+ z0 u5 |! ^: J$ A) a7 M
i = Serial.read();
. e9 n! @ K( W4 a// Serial.println(i);//输出可显示的ascll值2 t! _6 S5 P* Q- }! _
Serial.write(i);- A* G. S: m. ~5 E: a( {
Serial.write('% }; K6 p; Y+ B7 ^* a) I9 d
');# R0 |1 g h$ R
}( e" _! A6 l; A Q& y9 j
if (i == 'a')
& y! w( F5 t/ r{ @7 d; x. N M3 _. I0 j. g* {+ _
digitalWrite(2, HIGH);, J+ a# u5 ]' Z0 d
delay(1000);: y; Q3 {1 H/ n1 e/ Z( @
digitalWrite(2, LOW);, L% w2 ]. g& U0 c7 k% j; R: s
delay(1000);" u2 O. Y: `4 X# T
}% r1 c+ a: |6 V; P
if (i == 'b')' |0 C& l2 B {4 V3 @2 X; n; ^
{
% T# o% i, q' @3 g% C) g+ E: adigitalWrite(13, HIGH);6 p: r* } W) z
}; L+ r$ w7 x' I7 ~& e" G5 J" {
}: A& Y. m9 Z- \
% }% i3 o+ {) }1 ]' f: q其他设置esp32 wroom使用引脚RX0、TX0,连接到T1板子的IO1,IO0(RX0->IO1,TX0->IO0)
- x* u$ L L7 T$ r; N k0 C2 m# B手机随便下个带有编码设置的蓝牙串口调试助手(ps:不是蓝牙调试助手)+ a( x, W0 h4 V
将编码调成utf-8,就可以输入命令并点灯了。' h$ h6 S4 g# Y. f1 \0 l* ?
3 O/ h9 C S! ~+ d
注意事项手机蓝牙发送的编码一定要对可以是gbk、utf-8等编码,但是如果使用错的编码方式那么就会接收不到信号,串口打印数据就会乱码,如下图![]()
* q( ^- v' @$ I8 l除此之外,esp32和T1开发板的串口波特率也要对齐。
5 v( i7 R, f. ]5 j& }; {$ w2 L以上便是本次设计的全部内容了。
) q f( V Q* |' g
7 f. w/ k# {4 F; Y |
|