Thread freezed in multithreading

Hi,

I am working on the IOT product which reads bulk data from sensor through SPI and publishing to MQTT broker.

Created one thread to collect data from sensor and store it in buffer of size 4000 bytes.

===============================================

// This #include statement was automatically added by the Particle IDE.
#include "MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "SparkFunLSM9DS1.h"

// This #include statement was automatically added by the Particle IDE.
#include "LSM9DS1_Registers.h"

// This #include statement was automatically added by the Particle IDE.
#include "LSM9DS1_Types.h"

// This #include statement was automatically added by the Particle IDE.
#include "Acquisition_Data.h"

// This #include statement was automatically added by the Particle IDE.
#include "Data.h"

//sting fn prototypes
#include <string.h>

#include "math.h"

SYSTEM_MODE(SEMI_AUTOMATIC)

LSM9DS1 imu

//Thread def for Sensor read and fill in buffer
os_thread_return_t Measurement(void* param){
    CAcquisition_Data *recParam = (CAcquisition_Data *)param;

	// Start never ending loop
	do
	{
		Acquisition2cont(recParam->nAcqFuncStep);
	}while(1);
}
===============================================

Created another thread to publish the data to MQTT when buffer is full.

===============================================


void loop()
{
   uint32_t count = 0;
        if(bBufferFull == true)
	    {
	        if (client.isConnected())
	        {
	            client.publish(topicPub,(uint8_t*)(Senddata), sizeof(Getdata));
#if DEBUG
	            Serial.println("sending data succeed.");
#endif
	            m_acqData.lastAttemptTime = millis();
		        bBufferFull = false;
    		}
		 }
    	 else
    	 { 
		    delay(30);
		 }
}
===============================================

Setup function looks like this
===============================================

void setup() 
{
 	imu.settings.device.commInterface = IMU_MODE_SPI;//IMU_MODE_I2C;//
	imu.settings.device.mAddress = LSM9DS1_M_CS;
	imu.settings.device.agAddress = LSM9DS1_AG_CS;
	
	if (!imu.begin())
	{
		while (1)
		      ;
	}
	
	WiFi.on();//Dynamic ip assign
	WiFi.useDynamicIP();
	//Assign new wifi credential
	WiFi.setCredentials("wandboard", "wandboard");
    //Connecting to wifi network
    WiFi.connect();

    MeasurementThread = new Thread("Measurement", Measurement, &m_acqData);

	delay(1000);
}

===============================================

After running for certain amount of time(~ 1 hour) one of the 2 threads freezes and never recover. On board reset solves the issue, same repeats after a while.
Most of the time transmission get freezed. Analyzed by introducing “Serial.println()”.

Any suggestion would be helpful.

Many Thanks.

Ideas:

  1. check for heap - System.freeMemory(); - odd things like application freeze happens when heap is too low. You need 20K odd minimum, but your code and the deepest nesting of function calls will determine what is the “no go below” level.
  2. Replace String (BTW #include <string.h> is not required) use with char arrays. It could be that you are over running the initial string space allocation when this happens it exponentially grows the string space allocation and hence it works for a while until the space runs out.
1 Like

Setting the credentials on EVERY boot is not good practice. You should check if credentials exist and only set if they don't.

It is difficult to advise, besides what @ninjatill pointed out, without seeing the code for Acquisition2cont() and/or the class details for Acquisition_Data.

1 Like

Have you tried commenting out the call to Acquisition2cont() to see if the problem goes away?

Also, Serial is not thread-safe. If you are doing Serial.print from multiple threads you need to surround each call like this:

WITH_LOCK(Serial) {
	Serial.printlnf("counter=%d", counter);
}
1 Like