esp32太强大了,强大到以至于都没有和stm32交互的栗子。esp32的上一代esp8226与stm32交互的栗子很多,网上一搜到处都是,主要是stm32通过连接esp8226模块与物联网平台进行交互,但是esp32自己本身就有stm32的功能,就不需要再用这个stm32了。
但是很多小伙伴还是喜欢用stm32,毕竟资料多,源码删删改改就能用,esp32的编程门槛确实有点不敢恭维,都直接上linux编译环境了。
所以我就想了个办法,直接让stm32采集数据后,与esp32串口通信得了,然后esp32接收串口数据后,通过蓝牙发给手机或者通过wifi发给物联网平台,比如我之前写的那篇移动onenet平台。
stm32程序就不用提了,普通随便一个就行,只要是有串口发送就行,然后将stm32和esp32的串口接起来,就ok,这里我展示一下我写的esp32程序。
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
//这个例程为串口和传统蓝牙架设了一座桥梁
//同时也演示了SerialBT和普通的Serial具有一样的功能
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
String comdata = "";
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //蓝牙模块名称
Serial.println("蓝牙已启动,你可以配对了!");
}
void loop() {
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
}
if (comdata.length() > 0)
{
SerialBT.println(comdata);
comdata = "";
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
Serial.println("Serial");
}
delay(20);
}
打开我们的ide,Arduino,新建一个工程,直接复制进去下边的代码,编译下载,就可以了!
在手机上安装bluetooth_terminal蓝牙串口接收发送apk,找到我们的esp32设备,链接之后,就能收到esp32发来的数据了。网上找不到的可以使用我上传的这个-bluetooth_terminal蓝牙串口接收发送apk
转载自CSDN-专业IT技术社区
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/leva345/article/details/121612825