Starting multi-tasking

I have a question about the multi-threading on photon. Does photon come with multiple threading function?

Not yet.

You might find some discussions about this question, when searching for blocking, FreeRTOS, threads or threading.

HI @mikelo

Multithreading on the photon is scheduled for release in late September, where the system will run the application loop and system background loop on separate threads.

Cheers,
mat.

2 Likes

I have an app that goes out every few minutes and retrieves data to display on a local TFT. While it is getting the data (can take 10 seconds), the display stops being updated.
Will multi-threading help, or am I expecting too much from the little processor in the Photon?

I am using HttpClient to retrieve data. I assume that I will need a non-blocking version of the library?

Yes, you can run the HttpClient on it’s own thread that fetches the data then puts that data in a queue for your main loop to send to the display.

Similar problem for me…
Has the ‘system background loop’ feature been enabled now?
If so how to use it?

I am taking sensor readings continuously in a loop and every xx mins want to send the data to a server using TCP Client without stopping readings being logged in the main loop.

The data being read in the loop will also probably need saving to an SD card before being sent to my client at intervals too.
How can I achieve this?
Also I tried using Software Timers for the TCP Client function but it never gets called.
I am not using Particle.publish()

This is my code so far and working just great.

// TCP Client
// removed my details
char server[] = "www.myserver.com";
String host = "www.myserver.com";
String url = "/dir/dir/file.php?d=";
TCPClient client;

// GPS VARS
String inWord;
char inByte;
String gps;
int LockLED = D1;  

// XYZ Sensor pins
int xPin = A0;
int yPin = A1;
int zPin = A2;
// VARS
int xAxis;
int yAxis;
int zAxis;
String xyz;

// gp vars
String data;
unsigned long lastTime = 0;

// send data to server every 1 min
// DONT WORK
//Timer myTimer(60000, sendData);


void setup() {
    // cloud variables
    // GPS
    Particle.variable("STU", gps);
    // XYZ
    Particle.variable("XYZ", xyz);
    // GPS Serial
    Serial1.begin(9600);
    // LOCKED LED
    pinMode(LockLED, OUTPUT);
    // BLINK FOR TEST
    digitalWrite(LockLED, HIGH);
    delay(2000);
    digitalWrite(LockLED, LOW);
    // start data upload timer
    //myTimer.start(); // DONT WORK
}



void loop() {
    // read gps sensor
    while (Serial1.available() > 0) {
        inByte = Serial1.read();
        
        if (inByte == '\n') {
            // END OF LINE
            
            // check is data we want
            // you can change this to get any data line values
            if (inWord.startsWith("$GPRMC")) {
                // put data string in variable
                gps = inWord;
                // clear the inword variable
                inWord = "";
                
                // is the data string complete?
                // i.e. does the GPS Receiver have lock?
                // get the char at pos 17 and test to see if it is an A i.e VALID V = INVALID
                char lock = gps.charAt(17);
                if (lock == 'A') {
                    // YES Switch on Lock LED
                    digitalWrite(LockLED, HIGH);
                } else {
                    // NO turn off lock led
                    digitalWrite(LockLED, LOW);  
                }
                
                
            } else {
                // clear the inword variable as not the data we want
                inWord = "";
            }
            
        } else {
            // build data string
            inWord += inByte;   
        }
        
        
    } // end if serial
    
    // read 3-axis accelerometer
    xAxis = analogRead(xPin);
    yAxis = analogRead(yPin);
    zAxis = analogRead(zPin);
    xyz = String(xAxis) + ',' + String(yAxis) + ',' + String(zAxis);
    
    // build data string
    // seems to be control char or space at end of gps data so remove with trim as cant send this in url with GET
    data = gps.trim() + ',' + xyz;
    
    // timer
    unsigned long now = millis();
	if ((now - lastTime) >= 60000) {
		lastTime = now;
		sendData();
	}
    
    
    
} // end loop

void sendData() {
    if (client.connect(server, 80)) {
        
        client.println("GET: " + url + data + " HTTP/1.1");
        client.println("HOST: " + host);
        client.println("User-Agent: Particle Electron");
        client.println("Connection: close");
        client.println();
    } else {
        // FAILED TO CONNECT
    }
}

Same reply as in the other thread

Plus
Have you tried SYSTEM_THREAD(ENABLED)?

Electron V0.5.3
No error messages
Not work means sendData never gets called

I am flashing OTA

Do I need to issue a SYSTEM_THREAD(ENABLED) before using software timers then?

SYSTEM_THREAD(ENABLED) is not required for Software Timers to work,but I think TCPClient wasn’t working too well on 0.5.3.
I’m running 0.6.1-rc.2 and have no issues with TCPClient nor with Software Timers (which already worked on 0.5.3).

If you don’t want the bleeding edge rc.2, official 0.6.0 should have some of the fixes already.

TCPClient is working fine