I am trying to get data that has been received through bluetooth, and parsed to be used to call a function in the void loop(). I can see that the variable that I used when the parse happened is valued when I do a Serial.println(*raceType); but when I put *raceType == "Autocross" it doesnt start the function being called like I expected.
When I was not trying to send a group of data across from bluetooth, and I just sent Autocross, and had that stored in raceType (without the *) it would work prefectly. Now that I want to send a bigger group of data through to save into variables, I am running into some issues.
Here is the code that I am using
when I Serial.print() the variables that I have being set this is what the serial monitor shows.
[V][BluetoothSerial.cpp:307] esp_spp_cb(): ESP_SPP_DATA_IND_EVT len=31 handle=129
User ID: email@address.com
Race Type: Autocross
Starting Loc:
#include <Wire.h>
#include <SparkFun_LPS28DFW_Arduino_Library.h>
#include "ICM_20948.h"
#include <SparkFun_u-blox_GNSS_v3.h>
#include <MicroNMEA.h>
#include <BluetoothSerial.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <string.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
LPS28DFW pressureSensor;
uint8_t i2cAddress = LPS28DFW_I2C_ADDRESS_DEFAULT;
float degreesf;
float preshg;
SFE_UBLOX_GNSS myGNSS;
char nmeaBuffer[100];
MicroNMEA nmea(nmeaBuffer, sizeof(nmeaBuffer));
ICM_20948_I2C myICM;
#define AD0_VAL 1
//testing for message with parsing
const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars];
char *userID[numChars] = {0};
char *raceType[numChars] = {0};
char *startingLoc[numChars] = {0};
boolean newData = false;
//timer for data
unsigned long previousMillis = 0;
const long interval = 500;
//sd card setup
File myFile;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
//BT initialize
SerialBT.begin("IRP_Tracker");
Serial.println("The device started, now you can pair it with bluetooth!");
//SD Card initialize
if (!SD.begin()) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC) {
Serial.println("MMC");
} else if (cardType == CARD_SD) {
Serial.println("SDSC");
} else if (cardType == CARD_SDHC) {
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
//accel initialize
bool initializedAccel = false;
while (!initializedAccel) {
myICM.begin(Wire, AD0_VAL);
Serial.print(F("Initialization of the sensor returned: "));
Serial.println(myICM.statusString());
if (myICM.status != ICM_20948_Stat_Ok) {
Serial.println("Trying again...");
delay(500);
} else {
initializedAccel = true;
}
}
// while (pressureSensor.begin(i2cAddress) != LPS28DFW_OK) {
// Serial.println("Error: LPS28DFW not connected, check wiring and I2C address!");
// delay(1000);
// }
// Serial.println("LPS28DFW connected!");
// if (myGNSS.begin() == false)
// {
// Serial.println(F("u-blox GNSS not detected at default I2C address. Please check wiring. Freezing."));
// while (1);
// }
}
void loop() {
unsigned long currentMillis = millis();
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
showParsedData();
newData = false;
}
if (*raceType == "Autocross" && (currentMillis - previousMillis >= interval)) {
{
previousMillis = currentMillis;
Serial.println();
Accel_Sensor();
}
} else if (*raceType == "DragRace" && (currentMillis - previousMillis >= interval)) {
{
previousMillis = currentMillis;
Serial.println();
Serial.print("Drag Racing Data");
}
} else if (*raceType == "Circuit" && (currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
Serial.println();
Serial.print("Circuit Racing Data");
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (SerialBT.available() > 0 && newData == false) {
rc = SerialBT.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars){
ndx = numChars -1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() {
*userID = strtok(tempChars, ",");
*raceType = strtok(NULL, ",");
*startingLoc = strtok(NULL, ",");
}
void showParsedData() {
Serial.print("User ID: ");
Serial.println(*userID);
Serial.print("Race Type: ");
Serial.println(*raceType);
Serial.print("Starting Loc: ");
Serial.println(*startingLoc);
}
void P_Sensor() {
pressureSensor.getSensorData();
degreesf = ((pressureSensor.data.heat.deg_c) * 1.8) + 32;
preshg = (pressureSensor.data.pressure.hpa) * 0.0295;
Serial.print("Temp(F),");
Serial.print(degreesf);
Serial.print(",");
Serial.print("Pres(hg),");
Serial.print(preshg);
Serial.print(",");
}
void Accel_Sensor() {
if (myICM.dataReady()) {
myICM.getAGMT(); // The values are only updated when you call 'getAGMT'
// printRawAGMT( myICM.agmt ); // Uncomment this to see the raw values, taken directly from the agmt structure
printScaledAGMT(&myICM); // This function takes into account the scale settings from when the measurement was made to calculate the values with units
//delay(500);
} else {
Serial.println("Waiting for data");
delay(500);
}
}