# String(int) causing SOS safe mode

**URL:** <https://community.particle.io/t/string-int-causing-sos-safe-mode/51857>\
**Category:** Troubleshooting\
**Tags:** electron\
**Created:** [August 25, 2019, 8:10pm UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857 "2019-08-25T20:10:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MikeP171](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@MikeP171](https://community.particle.io/u/MikeP171)\
**Post date:** [August 25, 2019, 8:10pm UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/1 "2019-08-25T20:10:47Z")

</div>

Hi Community,  
For one of my projects I am currently working on requires an integer to be converted to a string. I am working with a Particle electron on the Particle Web IDE.  
My lines of code  
int wireVal = 0;  
Serial.print("wireVal is: “+ String(wireVal)+ " gbye”); // this crashes firmware

crashes firmware, giving a red SOS led, entering safe mode. I have also tried converting wireVal to a string outside the print statement but was unsucessful.  
I have used this same syntax on an Arduino but am unsure why this will not work on an Electron. Any help is appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [August 25, 2019, 9:17pm UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/2 "2019-08-25T21:17:55Z")

</div>

Have you considered not constructing a String from wireVal? You could use the following to just print out wireVal:

```auto
Serial.printlnf("wireVal is: %d gbye", wireVal);

```

Could you possibly share more of your code to help provide some more context? If constructing a String is crashing the firmware there may be a memory-related issue somewhere else in your code.

Also, which device OS version are you targeting?

---

<div class="post-metadata">

**Author:** ![MikeP171](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@MikeP171](https://community.particle.io/u/MikeP171)\
**Post date:** [August 26, 2019, 2:00am UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/3 "2019-08-26T02:00:02Z")

</div>

Hi NRobinson,  
my code:

```cpp
#include <Wire.h>
int wireVal = 0;
int led = D7;
String incomingString= "";

void setup() {
    pinMode(led, OUTPUT);
    Serial.begin(9600);
    Wire.begin(1);
    Wire.onReceive(receiveData);
}

void loop() {
    digitalWrite(led, wireVal);
    //Serial.println(wireVal); //added august
}

void receiveData (int byteCount) {
    while (0 < Wire.available()) {
    wireVal = Wire.read();
    //Serial.println("wireVal is: "+ String(wireVal));
    Serial.printlnf("wireVal is: %d gbye", wireVal);
    //Serial.println(wireVal);
    }
}

```

your print statement also doesnt yied results.... my output to putty is completely blank.... I am running win8.1 with version on electron device os 1.2.1 Does my error have to do with an object in c? from this post?

> [@Serial.printlnf - can't handle Strings?](https://community.particle.io/t/serial-printlnf-cant-handle-strings/16641):
>
> Maybe I need the sleep, but can anyone verify this with a 0.4.6 Photon? Test 1: (works fine) Serial.printlnf("The %s jumped over the moon", "cow!"); Test 2: (compile throws a ‘non-trivially-copyable type’ error if a String variable is used) String s = "cow"; Serial.printlnf("The %s jumped over the moon", s); All other printf specifiers seem to work. Except for Strings. Any ideas?

Thx.  
Mike

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [August 26, 2019, 2:42am UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/4 "2019-08-26T02:42:19Z")

</div>

I haven’t tested your code yet, but I think I have an idea why creating a String was crashing the firmware. Because the Wire.onReceive() handler operates atomically, I would guess that constructing objects like Strings isn’t allowed in that scope.

I would suggest creating a global byte array that you would use in receiveData() to iteratively store the bytes received from the I2C master, keeping the receiveData() handler as tight as possible.

In loop(), I would suggest doing any additional necessary processing of the array and printing it out to the serial console if the contents of the array changed since the last call of loop().

Could you possibly elaborate on your hardware setup? I assume the Electron running this sketch is connected to an I2C master that is sending data to it? If not, I wouldn’t be surprised if you’re not seeing any reports to your serial monitor.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 26, 2019, 6:35am UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/5 "2019-08-26T06:35:51Z")

</div>

I’d rather put the `Serial.print()` statement out of the `onReceive()` callback, just as I wouldn’t put a `Serial.print()` statement into an interrupt handler.

---

<div class="post-metadata">

**Author:** ![MikeP171](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@MikeP171](https://community.particle.io/u/MikeP171)\
**Post date:** [August 29, 2019, 1:12am UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/6 "2019-08-29T01:12:50Z")

</div>

> [@nrobinson2000](#):
>
> Could you possibly elaborate on your hardware setup?

My hardware setup is in this post, with picture from June 8.

> [@Casting Variables for I2C](https://community.particle.io/t/casting-variables-for-i2c/48813/35):
>
> Hi Guys, I was able to get my project to work finally, albeit the inverse setup I originally intended, with the Arduino as the master. This current setup is with the Electron as the Master. Here is my setup with code and output to follow. Arduino slave: #include \<Wire.h\> void setup() { Wire.begin(); Serial.begin(9600); Serial.print("setup has completed.");//mp } void loop() { Wire.beginTransmission(1); Wire.write(234); //modify this! //numerical 256 is the limit on this -…

> [@ScruffR](#):
>
> I’d rather put the `Serial.print()` statement out of the `onReceive()` callback, just as I wouldn’t put a `Serial.print()` statement into an interrupt handler.

Are you saying move the Serial.print into the setup right after onReceive()? Not quite sure what you are suggesting....

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 29, 2019, 6:33am UTC](https://community.particle.io/t/string-int-causing-sos-safe-mode/51857/7 "2019-08-29T06:33:46Z")

</div>

> [@MikeP171](#):
>
> Are you saying move the Serial.print into the setup right after onReceive()?

No, that wouldn't do anything. `onReceive()` hooks up a callback functino which will be called each time the interface detects incoming data, putting a single `Serial.print()` statement in setup - where it only gets called once - would only print once, and probably nothing since there wasn't any data yet.

What I'd rather do is creating a global `char` array (or other suitable type depending on your expected data) and feed the incoming data into that and have `loop()` check whether new data has arrived there and if so, print that.
