converting string to int array

Hey Guys,

i have a problem converting a string to an int array.

I send a String (“1,34,300,293,2”) from an html site via MQTT to my Arduino.

there I have a for loop to turn on leds int the int Array. but I couldtn figure out how to convert the string to my int array.

This is my code without the MQTT part

Could you help me pls?

Best Marvin :slight_smile:

#include <Adafruit_NeoPixel.h>
#define PIN        5
#define NUMPIXELS 22
#define DELAYVAL 50 // Time (in milliseconds) to pause between pixels

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int myLeds[] = {};

//exampleString i could recieve via MQTT
String testString = "21, 20, 19, 18";

void setup() {
  Serial.begin(115200);
  pixels.begin();
}

void loop() {

  //write testString to myLeds[]
  //should look like this if it worked: myLeds[] = {21, 20, 19, 18};

  for (int i = 0; i < (sizeof(myLeds) / sizeof(myLeds[0])); i++) { // For each pixel...
    pixels.setPixelColor(myLeds[i], pixels.Color(0, 10, 0));
    pixels.show();
    delay(DELAYVAL);
  }
}

If you want to stick to the String class, Look at the associated methods such as toInt()

A code snippit that might be of use.

      if (readString.length() >0) {
      Serial.println(readString);
      int n;
      char carray[6];
      readString.toCharArray(carray, sizeof(carray));
      n = atoi(carray); 
      myservo.writeMicroseconds(n);
      //myservo.write(n);
      readString="";
      }

Hey :slight_smile:

Thanks for your answers so far! :slight_smile:

@J-M-L

  1. 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...

  1. 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;
  
        }

Given this definition for your LED array:

int led[] = {};

How big is the sizeof() operator’s return value in the expression:

for (int i = 0; i < (sizeof(myLeds) / sizeof(myLeds[0])); i++)

Get rid of the String class…it’s a crutch you don’t need. See what this does:

int myLeds[4] = {};

char testString[] = "21, 20, 19, 18";
char delimiters[] = ",";

void setup() {
  Serial.begin(115200);

 char temp[10];
  char *ptr;
  int index = 0;

  ptr = strtok(testString, delimiters);

  while (ptr != NULL) {
    myLeds[index] = atoi(ptr);
    Serial.print("myLeds[");
    Serial.print(index);
    Serial.print("] = ");
    Serial.println(myLeds[index++]);
    ptr = strtok(NULL, delimiters);
  }
}

void loop() {
}

In case you haven't figured it out yet, String class is met with hostility here by experienced users.
Here's why.

MisterEsite:
@J-M-L

  1. I only have to stick to the String because I only can send strings via MQTT.

Which library do you use? The one I know have a publish and subscribe that can handle chat arrays (basically byte Arrays )

Hey :))

Thank you very very much for all your answers.

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 :slight_smile:

Thank you very much.

Best Marvin :slight_smile:

MisterEsite:
The MQTT library is PubSubClient from o’leary and I am using Cloudmqtt at the moment.

OK, yes then as the documentation describes, you can use cStrings (NULL terminated char arrays)

"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.