kegel/osc32_espnow_sprejemnik/osc32_espnow_sprejemnik.ino

150 lines
3.6 KiB
Arduino
Raw Permalink Normal View History

2023-02-28 13:31:26 +01:00
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
#include <OSCBundle.h>
#include <OSCBoards.h>
2023-06-14 13:51:18 +02:00
//#define DEBUG
2023-02-28 13:31:26 +01:00
#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
#else
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial); // Change to Serial1 or Serial2 etc. for boards with multiple serial ports that dont have Serial
#endif
// Set your new MAC Address
// MAC naslov sprejemnika: 08:3A:F2:50:EF:6C
uint8_t newMACAddress[] = {0x08, 0x3A, 0xF2, 0x50, 0xEF, 0x6C};
typedef struct sensor_msg {
2023-06-14 13:51:18 +02:00
uint8_t id;
2023-06-28 17:54:15 +02:00
float aX;
float aY;
float aZ;
2023-06-14 13:51:18 +02:00
float qX;
float qY;
float qZ;
float qW;
} sensor_msg;
//sensor_msg odcitek;
// Maksimalno stevilo
#define ST_KEGLOV 10
int odcitekId;
sensor_msg odcitki[ST_KEGLOV];
bool poslji[ST_KEGLOV];
void prejemPodatkov(const uint8_t * mac_addr, const uint8_t * noviPodatki, int len) {
2023-02-28 13:31:26 +01:00
char macNaslov[18];
snprintf(macNaslov, sizeof(macNaslov), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
2023-06-14 13:51:18 +02:00
#ifdef DEBUG
Serial.print("Prejel podatke od ");
Serial.println(macNaslov);
#endif
2023-02-28 13:31:26 +01:00
2023-06-14 13:51:18 +02:00
// ID odcitka je na prvem mestu!
memcpy(&odcitekId, noviPodatki, sizeof(uint8_t));
2023-02-28 13:31:26 +01:00
2023-06-14 13:51:18 +02:00
#ifdef DEBUG
Serial.print("RAZBIRAM ID ");
Serial.println(odcitekId);
#endif
memcpy(&odcitki[odcitekId], noviPodatki, sizeof(sensor_msg));
#ifdef DEBUG
Serial.printf("aX: %i \n", odcitki[odcitekId].aX);
Serial.printf("aY: %i \n", odcitki[odcitekId].aY);
Serial.printf("aZ: %i \n", odcitki[odcitekId].aZ);
Serial.printf("qX: %f \n", odcitki[odcitekId].qX);
Serial.printf("qY: %f \n", odcitki[odcitekId].qY);
Serial.printf("qZ: %f \n", odcitki[odcitekId].qZ);
Serial.printf("qW: %f \n", odcitki[odcitekId].qW);
2023-02-28 13:31:26 +01:00
Serial.println();
2023-06-14 13:51:18 +02:00
#endif
poslji[odcitekId] = true;
2023-02-28 13:31:26 +01:00
}
2023-06-14 13:51:18 +02:00
2023-02-28 13:31:26 +01:00
void setup() {
SLIPSerial.begin(115200);
2023-06-14 13:51:18 +02:00
// Ne posiljaj preden se podatki napolnijo
for (int i = 0; i < ST_KEGLOV; i++) {
poslji[0] = false;
}
2023-02-28 13:31:26 +01:00
Serial.println("Inicializiram WIFI...");
WiFi.mode(WIFI_STA);
esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
2023-06-14 13:51:18 +02:00
esp_err_t result = esp_now_init();
if (result != ESP_OK) {
2023-02-28 13:31:26 +01:00
Serial.println("Error initializing ESP-NOW");
2023-06-14 13:51:18 +02:00
Serial.println(result);
2023-02-28 13:31:26 +01:00
return;
}
Serial.print("MAC naslov: ");
Serial.println(WiFi.macAddress());
esp_now_register_recv_cb(prejemPodatkov);
}
void loop() {
2023-06-14 13:51:18 +02:00
/* OSC MSG channels */
OSCBundle bundle;
char glava[32];
2023-02-28 13:31:26 +01:00
for (int i = 0; i < ST_KEGLOV; i++) {
if (poslji[i]) {
sprintf(glava, "/ww/%d/accel", i);
2023-06-14 13:51:18 +02:00
/*
Serial.print("Posiljam ");
Serial.println(glava);
*/
2023-02-28 13:31:26 +01:00
bundle.add(glava)
.add(odcitki[i].aX)
.add(odcitki[i].aY)
.add(odcitki[i].aZ);
sprintf(glava, "/ww/%d/quaternion", i);
2023-06-14 13:51:18 +02:00
/*
Serial.print("Posiljam ");
Serial.println(glava);
*/
2023-02-28 13:31:26 +01:00
bundle.add(glava)
.add(odcitki[i].qW)
.add(odcitki[i].qX)
.add(odcitki[i].qY)
.add(odcitki[i].qZ);
2023-06-14 13:51:18 +02:00
/*
Serial.printf("XaX: %i \n", odcitki[i].aX);
Serial.printf("XaY: %i \n", odcitki[i].aY);
Serial.printf("XaZ: %i \n", odcitki[i].aZ);
Serial.printf("XqX: %f \n", odcitki[i].qX);
Serial.printf("XqY: %f \n", odcitki[i].qY);
Serial.printf("XqZ: %f \n", odcitki[i].qZ);
Serial.printf("XqW: %f \n", odcitki[i].qW);
Serial.println();
*/
2023-02-28 13:31:26 +01:00
SLIPSerial.beginPacket();
bundle.send(SLIPSerial);
SLIPSerial.endPacket();
2023-06-14 13:51:18 +02:00
bundle.empty();
2023-02-28 13:31:26 +01:00
poslji[i] = false;
}
}
//delay(10);
}