1. 程式人生 > 其它 >ESP32讀取串列埠感測器資料

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)串列埠的使用(傳送和接收)