Why is this failing after 3 iterations?
As Jaromanda X pointed out in the comments, sizeof(array[0]) (size of first element) will not return the size of the literal strings, but the size of a char pointer.



It doesn't work (whatever that means for you), because sizeof(devices) doesn't return the number of entries in the array, but the size of the array in bytes, e.g. something like (following is wrong: 24 (1 byte per character plus 1 byte with the concluding null character for each string) ) 6 (The size of a char pointer is 2 bytes on most microcontrollers; I'm not sure, if this is the same for Node MCU). So after 3 iterations (which can use the string literals in the array) currentDevice doesn't go back to zero, but counts further. With the access to the array with exceeding index you are accessing memory, that doesn't belong to the array anymore (e.g. random data).

There is no native function, that gives you the length of an array. But it can be calculated with sizeof(array)/sizeof(array[0]), which is the size of the array in bytes divided by the size of one element in bytes. In your case you would have an array size of 6 and an element size of 2, resulting in a calculated element count of 3, which is correct.

Another simple solution, which is easier to understand for beginners, but not as flexible, is to set a constant or define, that contains the size of your array. You can then use it in your code, whenever you need the size, including, when you define the array.