Still searching on my side and good news, I found a way to make it running! Maybe not the start of the art, but it works!
So I'm able to connect to my PC in rtp-midi and I guess I will be able to send midi messages now without any problem.
But there is something strange: to make it running, I have to add some echo to the serial monitor (like "Serial.print(InitiatorToken[x] ,HEX);" in my code below). Without these lines, my arduino is reading boolshit from the esp and then I can't connect to my PC. I don't understand the link, except that adding these lines, it adds a delay I guess. Can somebody explain me this? An other way to do to not have this kind of problem?
my code:
Code: Select allconst uint8_t protocol[]={0,0,0,2};
const uint8_t mySSRC[]={0,0,0,0}; //SSRC for rtp, normally a random value, to modify later...
const uint8_t invitation[]={73,78}; // invitation message ff ff 49 4e (IN)
const uint8_t accepted[]={255,255,79,75}; // accept invitation ff ff 4f 4b (OK)
const uint8_t synchro[]={67,75}; // synchro message ff ff 43 4b (CK)
const uint8_t myempty[]={0,0,0,0}; // usefull to send 32 bits of 0
boolean resp;
unsigned long myTime;
uint8_t TimeStamp2[4];
uint8_t mybuf1[11];
uint8_t InitiatorToken[4]; // used when sending data
//-------------------------------------------------------------
void loop() {
/* data received from the ESP */
if (Serial1.available() > 0) {
int ch2 = Serial1.read();
if (ch2 == 43) { // message starting with +
String abc = Serial1.readStringUntil(':');
if (abc.startsWith("IPD")){ // that's data!
int id = abc.substring(4,5).toInt(); //
Serial.print("Data received on id: "+String(id));
int datalength = abc.substring(6,abc.length()).toInt();
Serial.println(" - Message length: "+String(datalength)+"bytes");
if (datalength>0) {
// reading to know the command
uint8_t command[4];
for (int x = 0; x < 4; x++){
command[x] = Serial1.read();
}
/*------------------------------------
* command interpreter
*-----------------------------------*/
if (command[2] == invitation[0] && command[3] == invitation[1]) { // Invitation
Serial.println("=> Invitation");
for (int x = 0; x < 4; x++){
Serial.print(Serial1.read()); // we don't care of this 4 bytes!
}
Serial.print("InitiatorToken:");
for (int x = 0; x < 4; x++){
InitiatorToken[x] = Serial1.read();
Serial.print(InitiatorToken[x] ,HEX);
}
Serial.println("");
/*-- my answer --*/
Serial1.println("AT+CIPSEND="+String(id)+",22");
Serial1.find(">");
Serial1.write(accepted, 4);
Serial1.write(protocol, 4);
Serial1.write(InitiatorToken, 4);
Serial1.write(mySSRC, 4);
Serial1.print("OM-A1");
Serial1.write(0);
Serial.print("myAnswerSent...");
resp = Serial1.find("OK\r\n");
Serial.println(resp);
}
else if (command[2] == synchro[0] && command[3] == synchro[1]) { // Synchronisation
for (int x = 0; x < 4; x++){
Serial.print(Serial1.read()); // we don't care of this 4 bytes!
}
Serial.print("=> Synchro: count=");
int SyncCount=Serial1.read();
Serial.println(SyncCount);
if (SyncCount == 0){ // this is the only one we need to answer
for (int x = 0; x < 11; x++){
mybuf1[x] = Serial1.read(); //we read all what we have to send back (including TimeStamp1)
Serial.print(mybuf1[x] ,HEX);
}
// my TimeStamp calculation
myTime= millis();
TimeStamp2[0] = (int)((myTime >> 24) & 0xFF) ;
TimeStamp2[1] = (int)((myTime >> 16) & 0xFF) ;
TimeStamp2[2] = (int)((myTime >> 8) & 0xFF) ;
TimeStamp2[3] = (int)(myTime & 0xFF) ;
//-- my answer --//
Serial1.println("AT+CIPSEND="+String(id)+",36");
Serial1.find(">");
Serial1.write(command, 4);
Serial1.write(mySSRC, 4);
Serial1.write(1);
Serial1.write(mybuf1, 11);
Serial1.write(myempty, 4);
Serial1.write(TimeStamp2, 4);
Serial1.write(myempty, 4);
Serial1.write(myempty, 4);
Serial.print("myAnswerSent...");
resp = Serial1.find("OK\r\n");
Serial.println(resp);
}
else {
Serial.println("No need to answer");
String def = Serial1.readStringUntil('\n'); // read any remaining characters in espSerial buffer that we don't care => flush
}
}
else {
Serial.println("Command not supported...");
String def = Serial1.readStringUntil('\n'); // read any remaining characters in espSerial buffer that we don't care => flush
}
}
}
}
else {
String abc = Serial1.readStringUntil('\n');
Serial.println(String(ch2)+abc);
}
}
}