简体   繁体   中英

arduino esp32 bluetooth recieve a whole string

Is there a neat way of receiving a message string with the esp32 BluetoothSerial library just like Serial.readString() . The idea is to send a message from a smartphone, receive the message and update variables from that message which will affect what the Arduino does. Also can I save a byte as eg 255 instead of 0xFF?

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

uint8_t mode = 0;
int speedDelay = 50;
byte color1r, color1g, color1b, color2r, color2g, color2b = 0; // can I save this as a number from 0-255?


String readBTString() {
  return ???     // recieve a string or char from SerialBT ??
}

// this checks if a new message is available and then updates the variables accordingly
bool checkBT() {
  if(SerialBT.available()) {
    char data[35];

    // e.g. "1::0,255,67::255,43,87::30"
    String str = readBTString();
    str.toCharArray(data, 35);

    // update variables from message including updating mode which then effects the loop function
    sscanf(data, "%d::%d,%d,%d::%d,%d,%d::%d", &mode, &color1r, &color1g, &color1b, &color2r, &color2g, &color2b, &speedDelay);
    return true;
  }
  else return false;
}

doSomething(byte r, byte g, byte b, int speedDelay) {
  for (int i = 0; i<255; i++) {
     // do something
     delay(speedDelay);
     if (checkBT()) break; // check if a message is available
  }
}

doSomethingElse(byte r, byte g, byte b, int speedDelay) {
  for (int i = 0; i<255; i++) {
     // do something else
     delay(speedDelay);
     if (checkBT()) break;
  }
}

void setup() {
  SerialBT.begin("BTtest");
}

void loop() {
  switch (mode) // different mode values do different things
  {
  case 0:
    doSomething(color1r,color1g,color1b, speedDelay);
    break;
  case 1:
    doSomethingElse(color1r,color1g,color1b, speedDelay);
    break;
  default:
    doSomething(color1r,color1g,color1b, speedDelay);
    break;
  }
}

It actually seems that BluetoothSerial has a readString method... I'm on Platform.io with an esp32 + Arduino framework configuration and it correctly compiles...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM