Char zError[24] = ""; works, but char zError[24] = "INIT"; will lock up Core

Hi,

I have been playing with DHT22 sensors and was able to integrate both DHT22 and One-Wire temp/humid working on the same Core.

But, I had trouble and locked up my Core when I initialized a string with “INIT”:

...

char zError[24] = "INIT";

void setup()
{
    Spark.variable("errorX", zError, STRING);
}

void loop()
{
  int result = DHT.acquireAndWait();
  switch (result) {
      case DHTLIB_OK:
          sprintf(zError , "OK");
          break;
      case DHTLIB_ERROR_CHECKSUM:
          sprintf(zError , "Checksum");          
          break;
	...
	...
    }      
    delay(2500);
}

If I simply change to:

char zError[24] = "";

Everything works as it should…

I found several topics on STRING and memory issues, some which were solved with hardware updates. I don’t know if my Core has been automatically updated, but I reset everything and just changed the “INIT” part, which solved my problem.

I get the impression that STRING is a difficult variable to handle, but I have not seen good examples of what one can (or should) do to master this important variable type.

Any thoughts ?

Marcus

As you are not using the String class, some of the issues you found on the forum won’t apply to you, since you use the good ol’ C char-array.
I would expect your original code to work as is. Just by looking at the code, I can’t see a reason why you are seeing this behaviour, but I’m curious to find out :wink:
I’ll try it on my Cores. It might well be a preprocessor glitch of some kind, just as well as some other obscure misbehaviour.

Just some more questions.
What dev environment are you using (WebIDE, Spark Dev, CLI, local toolchain; …)?
Can you try some other preset strings (e.g. “Init”, “1234”, “SomethingLong”, “or” short, …)?
What is the exact problem? Does the Core hang in setup(), in loop() or when you retrieve errorX?
Can you put in some Serial.print() or LED blinking, to narrow down where it happens?

Hi ScruffR,

I’m using Spark Dev v0.20 :slight_smile: (they fixed the MacOS version that was heating up my CPU)

The crash comes just after the upload, when the WIFI OK blue light is pulsing. At this point the Core doesn’t respond to anything, so I cannot re-load a new compiled code. I have to reset the device and start with my iPhone to give it a new WIFI connection.

I’ll play with different length strings where I used “INIT”, and get back to you later today…

I have no experience with C or C++. Plain JavaScript never gives me the headaches of memory management, typing, pointers, etc.

I wish there was a better way to write these Core apps without these headaches that take forever to solve with trial & error. Each time I crash a Core, I wonder if it is a sensor’s library that has the error, and then find out its a slight change in code like this STRING issue.

My goal is to develop a complete solution to run a “wine cellar”, where air-conditioning, humidifier and heater will be watching indoor temp/humidity, plus outdoor data. This means lots of sensors and relays, plus logging into a cloud database, plus IONIC/Cordova app for mobiles, all running with real-time data from multiple Cores in different places.

The “wine cellar” concept can also go beyond to monitor cold rooms, meat packing, tracking containers on ships, etc. But, if I can’t get a stable Core to work reliably 100% of the time, its no use. So far these are very stable, low in power and heat dissipation, etc. We’ve been burning some Cores in for 60 days without any problems.

Cheers,

Marcus

1 Like

@Marcus1, I once had a similar problem and I did loads of factory resets and all the fresh setup stuff, till someone told me to use CLI to safe me some hassle.
If you have a working tiny binary, you just put the Core into yellow blinking dfu-mode (reset + mode button, release reset and keep mode pressed till yellow blinking), then you flash this dummy firmware.

spark flash --usb dummy.bin
// or if you have no dummy.bin just flash tinker like this
spark flash --usb tinker

This way you can save yourself the extra WiFi setup and other stuff (I even had to reflash the deep-update eacht time, since I got Cores from the first batch).

http://docs.spark.io/cli/

1 Like

@Marcus1, I’ve just flashed an adapted version of your code on one of my Cores, and it seems to work without problems.

This is what I’ve tried out:

// This #include statement was automatically added by the Spark IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"

void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(A0, DHT22, dht_wrapper);

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}


char zError[24] = "INIT";

void setup()
{
    
    Serial.begin(115200);
    Spark.variable("errorX", zError, STRING);
    while(!Serial.available())
        SPARK_WLAN_Loop();
        
}

void loop()
{
    int result = DHT.acquireAndWait();

    switch (result) {
        case DHTLIB_OK:
            sprintf(zError , "OK");
            break;
        case DHTLIB_ERROR_CHECKSUM:
            sprintf(zError , "Checksum");          
            break;
        default:
            sprintf(zError, "default: %d", result);
    }      
    Serial.println(zError);
    delay(2500);
}

So there must be another reason for your crash than the INIT thing - which didn’t really make much sense to me anyhow.

Maybe you could post your full code. Maybe we can see some other reason.