I only have to stick to the String because I only can send strings via MQTT.
An other option I think about is sending only "the numbers" and not an array of numbers. But it would be nicer to work with the array directly.
As well I didnt accomplished to append my numbers to the array. in JS or phyton its way easier...
I accomplished it to convert it to ints, to a char array,... but always it "kills" the numbers in it.
in the end I want to iterate through an array of numbers. like in this array {1, 22, 89, 90}, and turn on the LED[1], LED[22], LED[89],...
@Zoomkat
this converts it to a char array. but I need the whole numbers and not the chars...
Just to get my idea.
My TS Code is:
public h = false;
public array = [
];
public sendArray;
public lightEmUp(lightNumber){
this.h = !this.h;
if(this.h == true){
document.getElementById(lightNumber).style.color = "#ff0000";
this.array.push(lightNumber);
this.sendArray = this.array.toString();
this.mqttService.publish(this.message.topic, this.sendArray);
console.log(this.array);
console.log(this.sendArray);}
else { document.getElementById(lightNumber).style.color = "#000000";
var valueToRemove = lightNumber;
var filteredItems = this.array.filter(array => array !== valueToRemove)
this.array = filteredItems;
this.mqttService.publish(this.message.topic, this.sendArray);
console.log(this.array);
return this.array;
}
The MQTT library is PubSubClient from o'leary and I am using Cloudmqtt at the moment.
At the moment I went away from sending an array (without solving the problem why I started this Post).
At the moment I send only a number ("382") via MQTT to my Esp and I than add it to my array.
If the value is already in my array, I delete the value from the array.
With this way I got rid of sending the whole array.
I will check your recommendations for solving the problem when I have more time
"this converts it to a char array. but I need the whole numbers and not the chars..."
You seem to send the data as a comma delimited packet, so you can capture the packet into a String, then use the String functions to parse out the individual data pieces, assign the data pieces to String variables, then use readString.toInt() to convert the data piece to an integer.