快捷搜索: 王者荣耀 脱发

ESP32cam蓝牙模块与arduino uno通信实验

网上关于esp32cam与arduino通信的资料几乎没有,有的还得付费,于是自己动手琢磨,实验成功。 实验有多个版本,成功实现蓝牙收到数据后发送单个字符给arduino板,匹配到对应字符亮对应颜色的灯,发送字符串可以此类推。 实验材料,接线方式在下图可以看到: 需要下载一个蓝牙串口助手,应用商店里很容易找到。

arduino uno上传代码(接收单字符):

int i ;
void setup() {
          
   

  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
          
   
  if (Serial.available())
  {
          
   
//    Serial.write(Serial.read());//输出原数据格式
    i = Serial.read();
//    Serial.println(i);//输出可显示的ascll值
      Serial.write(i);
      Serial.write(
);
  }

  if (i == a)
  {
          
   
    digitalWrite(2, HIGH);
  }
  if (i == b)
  {
          
   
    digitalWrite(3, HIGH);
  }
  if (i == c)
  {
          
   
    digitalWrite(4, HIGH);
  }
}

arduino上传代码(接收字符串):

String itext = "" ;
void setup() {
          
   

  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

String detectString();
String gettext();

void loop() {
          
   
  if (Serial.available())
  {
          
   
//    Serial.write(Serial.read());
      itext = gettext();
//    Serial.println(i);
      Serial.println(itext);
  }

  if (itext == "blue")
  {
          
   
    digitalWrite(2, HIGH);
  }
  if (itext == "green")
  {
          
   
    digitalWrite(3, HIGH);
  }
  if (itext == "red")
  {
          
   
    digitalWrite(4, HIGH);
  }
}

//======接收esp32cam数据===========================
String detectString()    //删除传输格式
{
          
   
  while(Serial.read() != {);
  return(Serial.readStringUntil(}));
}
String gettext()   //得到数据
{
          
   
  String s = detectString();
  return s;
}

esp32cam上传代码:

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal 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;

char i;

void setup() {
          
   
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
          
   
  // 蓝牙助手发送数据到arduino uno
  if (SerialBT.available()) {
          
   
    while (SerialBT.available()) {
          
   
      //      Serial.write(SerialBT.read());
      i = SerialBT.read();//一个字符一个字符读取,读一个就少一个
      Serial.print(i);//发送数据到arduino uno
    }
  }

}

两个实验接收数据显示图: (1)单字符 (2)字符串

经验分享 程序员 微信小程序 职场和发展