Hello,
I'm currently receiving 20 character blocks from BLE using the nano 33 IoT with the code below. The chars returned are then stored into a global array of strings called myStrings, and this portion prints correctly (There is probably a better way than a global stringArray here but returning the strings seemed to complicate things more).
String myStrings[20];
int i = 0;
while (peripheral.connected()) {
if (myCharacteristic.valueUpdated()) {
int l = 20;
char values[l];
myCharacteristic.readValue(values, 20);
String temp = String(values);
myStrings[i] = temp;
Serial.println("myStrings[i] is");
Serial.print(myStrings[i]);
i++
}
I want to try and send this received message over MQTT using the following code
void publishMessage() {
Serial.println("Publishing message");
mqttClient.beginMessage("arduino/outgoing");
mqttClient.print(myStrings[0]);
mqttClient.endMessage();
}
I unfortunately cannot post more code than this due to potential IP issues, but I have verified that print is able to send string literals ie: "hello" over MQTT and those string literals are received on the AWS topic. There are catches for any potential errors in setting up BLE/MQTT and other functions, and the program just seems to break when I try to send a string from the string array. I've also tried things like saving a temp string such as:
String temp = stringArray[0];
mqttClient.beginMessage("arduino/outgoing");
mqttClient.print(temp);
mqttClient.endMessage();
but this also does not work. I am also unable to print the stringArray values outside of when they are initially created in the posted code block. So the print in that section works, but printing the strings anywhere else fails. Am I misunderstanding something about how globals or strings work? Does anyone have advice for a solution or better approach?
Thanks