Hello. I have question to ask. currently my project is to measure the angle of IMU BNO055 which connected to M5 stack atom (ESP32). I used three IMU sensors and with using the ESP-NOW to transmit the data to M5 stack monitor. I used mac address of each M5 stack atom to get the data. My question is how can I read the data from mac address to Unity script
#include <M5Stack.h>
#include <WiFi.h>
#include <esp_now.h>
/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (100)
uint8_t ad5;
float incomingqw;
float incomingqx;
float incomingqy;
float incomingqz;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
float w,x,y,z;
} struct_message;
float data[15];
File file;
const char* fname = "/data.csv";
// Create a struct_message called myData
struct_message BNO055Readings;
struct_message myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
memcpy(&myData, incomingData, sizeof(myData));
if (mac[5]==148)// change the mac from hexadecimal to decimal (from the mac of M5atom took the first 2 digit)
{
data[0] = mac[5];
data[1] = myData.w;
data[2] = myData.x;
data[3] = myData.y;
data[4] = myData.z;
}
/* else if (mac[5]==208)
{
data[5] = mac[5];
data[6] = myData.w;
data[7] = myData.x;
data[8] = myData.y;
data[9] = myData.z;
}
else if (mac[5]==232)
{
data[10] = mac[5];
data[11] = myData.w;
data[12] = myData.x;
data[13] = myData.y;
data[14] = myData.z;
}
*/
// Write Mac adress, Quaternion to SD in M5Stack
File datafile = SD.open(fname, FILE_APPEND);
if (datafile) {
datafile.print(mac[5]);
datafile.print(",");
datafile.print(myData.w, 6);
datafile.print(",");
datafile.print(myData.x, 6);
datafile.print(",");
datafile.print(myData.y, 6);
datafile.print(",");
datafile.print(myData.z, 6);
datafile.println("");
datafile.close();
}
}
byte mac[5]; // create client ID from mac address
WiFi.macAddress(mac); // get mac address
String clientID = String(mac[0]) + String(mac[4]) ; // use mac address to create clientID
thank you for your reply. Is that for script in unity or for arduino?How can I read the data from mac address of esp32 to the script in Unity. I will shared the script code from unity.
using System;
using System.IO.Ports;
using System.Threading;
using UnityEngine;
public class IMU : MonoBehaviour
{
private const string SERIAL_PORT = "COM6";
private const int SERIAL_BAUD_RATE = 115200;
private const int SERIAL_TIMEOUT = 100;
private Thread _readThread;
private static SerialPort _serialPort;
private static bool _continue;
private static Quaternion _handQuaternion = new Quaternion();
void Start()
{
_readThread = new Thread(Read);
_serialPort = new SerialPort(SERIAL_PORT, SERIAL_BAUD_RATE);
_serialPort.ReadTimeout = SERIAL_TIMEOUT;
_serialPort.Open();
_continue = true;
_readThread.Start();
}
void Update()
{
transform.rotation = _handQuaternion;
}
void OnApplicationQuit()
{
_continue = false;
_readThread.Join();
_serialPort.Close();
}
private static void Read()
{
string[] values;
float x, y, z, w;
while (_continue)
{
if (_serialPort.IsOpen)
{
try
{
values = _serialPort.ReadLine().Split('\t');
if (values[0] == "quat")
{
x = float.Parse(values[2]);
y = -float.Parse(values[4]);
z = float.Parse(values[3]);
w = float.Parse(values[1]);
_handQuaternion.Set(x, y, z, w);
}
}
catch (TimeoutException)
{
}
}
Thread.Sleep(1);
}
}
}
I do not know about the unity thingy.
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.