ESP32读取串口传感器数据

最近拿到一个九轴陀螺仪传感器,如图:

该设备通过串口方式输出九轴数据,可用官方自带得上位机或者是sscom看到数据,官网提供了很多例程,但唯独没有ESP32的

既然提供了Arduino Mega2560的例程,那么ESP32应该也可以使用。

先说一下ESP32的串口通信方式。

ESP32基于硬件的串行通信接口总共有三个,分别定义在了如下的GPIO口:

 针对三个硬件串行通信接口,ESP32已预先对应的建立了三个对象Serial、Serial1、Serial2。

因此我们才可以在开发时直接上来就写Serial.begin(9600) ; Serial.print(“Hello World!”);这样使用串口。

当然了,如果想使用UART2串口,我们就可以直接写Serial2.begin(9600) ; Serial2.print(“Hello World!”)。

我在ESP32板子上找到了rx0,tx0和rx2,tx2,原来rx0,tx0是拿来下载程序用的,只有板子上的RX2和TX2才能拿来用。所以这个设备和ESP32要这么接线:

陀螺仪VCC  <> ESP32的VIN(5v供电)(这里有个大坑,这个设备必须5V供电,3.3V收不到数据)

陀螺仪GND <> ESP32的GND

陀螺仪TX    <> ESP32 RX(D16)

陀螺仪RX    <> ESP32 TX(D17)

进入官网:https://dl.wit-motion.com:2103/index.html#/wit-service/productLiterature/details?productId=41903864ec49452495992a0e663bbe71

下载:“7.Arduino Mega2560读取维特智能标准协议”。

然后将文件夹复制到arduino的libraries文件夹下。上代码:

#include <Wire.h>
#include <JY901.h>

void setup() 
{
  Serial.begin(9600);  
  Serial2.begin(9600);
}

void loop() 
{
  //print received data. Data was received in serialEvent;
  Serial.print("Time:20");Serial.print(JY901.stcTime.ucYear);Serial.print("-");Serial.print(JY901.stcTime.ucMonth);Serial.print("-");Serial.print(JY901.stcTime.ucDay);
  Serial.print(" ");Serial.print(JY901.stcTime.ucHour);Serial.print(":");Serial.print(JY901.stcTime.ucMinute);Serial.print(":");Serial.println((float)JY901.stcTime.ucSecond+(float)JY901.stcTime.usMiliSecond/1000);
               
  Serial.print("Acc:");Serial.print((float)JY901.stcAcc.a[0]/32768*16);Serial.print(" ");Serial.print((float)JY901.stcAcc.a[1]/32768*16);Serial.print(" ");Serial.println((float)JY901.stcAcc.a[2]/32768*16);
  
  Serial.print("Gyro:");Serial.print((float)JY901.stcGyro.w[0]/32768*2000);Serial.print(" ");Serial.print((float)JY901.stcGyro.w[1]/32768*2000);Serial.print(" ");Serial.println((float)JY901.stcGyro.w[2]/32768*2000);
  
  Serial.print("Angle:");Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);Serial.print(" ");Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);Serial.print(" ");Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
  
  Serial.print("Mag:");Serial.print(JY901.stcMag.h[0]);Serial.print(" ");Serial.print(JY901.stcMag.h[1]);Serial.print(" ");Serial.println(JY901.stcMag.h[2]);
  
  Serial.print("Pressure:");Serial.print(JY901.stcPress.lPressure);Serial.print(" ");Serial.println((float)JY901.stcPress.lAltitude/100);
  
  Serial.print("DStatus:");Serial.print(JY901.stcDStatus.sDStatus[0]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[1]);Serial.print(" ");Serial.print(JY901.stcDStatus.sDStatus[2]);Serial.print(" ");Serial.println(JY901.stcDStatus.sDStatus[3]);
  
  Serial.print("Longitude:");Serial.print(JY901.stcLonLat.lLon/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLon % 10000000)/1e5);Serial.print("m Lattitude:");
  Serial.print(JY901.stcLonLat.lLat/10000000);Serial.print("Deg");Serial.print((double)(JY901.stcLonLat.lLat % 10000000)/1e5);Serial.println("m");
  
  Serial.print("GPSHeight:");Serial.print((float)JY901.stcGPSV.sGPSHeight/10);Serial.print("m GPSYaw:");Serial.print((float)JY901.stcGPSV.sGPSYaw/10);Serial.print("Deg GPSV:");Serial.print((float)JY901.stcGPSV.lGPSVelocity/1000);Serial.println("km/h");
  
  Serial.print("SN:");Serial.print(JY901.stcSN.sSVNum);Serial.print(" PDOP:");Serial.print((float)JY901.stcSN.sPDOP/100);Serial.print(" HDOP:");Serial.print((float)JY901.stcSN.sHDOP/100);Serial.print(" VDOP:");Serial.println((float)JY901.stcSN.sVDOP/100);
  
  Serial.println("");
  delay(500);

  while (Serial2.available()) 
  {
    JY901.CopeSerialData(Serial2.read()); //Call JY901 data cope function
  }
}

参考:

ESP32 for arduino 的3个hardware serial

(ESP32学习11)串口的使用(发送和接收)

posted @ 2021-12-08 22:27  盛世芳华  阅读(4484)  评论(0编辑  收藏  举报