Split a TCP payload

Hello,

I am trying to split a payload from a TCP connection.
So, i send a request to my arduino with TCP and i receive this data

"s01on,a01off,s02on,a02off,s03on,a03off,s04off,a04off,slon,alon,suon,auoff,s05on,a05off,s06on,a06off,s07on,a07off,s08on,a08off,s09on,a09off,s10on,a10off,s11on,a11off,r01on,r02on,r03off,r04off,r05off,r06off,r07off,r08off,p01off,p02off,p03off,s12on,s13on,s14on,n01on,n02on,q1on,qa1off,q2on,qa2off,q3off,qa3off,q4on,qa4off"

After i receive this data, i need to split it into a single message
This is what i done so far but i only get the last message from the array.

function onPoll() {
    console.log("Polling...");
    tcp.send("req2");
    sleep(1000);
    var data = tcp.receive({timeout: 1000});
    console.log("message received: " + data);
var lastResponse = data.trim().split(",").pop();

    console.log("response: " + lastResponse);
    switch (lastResponse) {
        case "qa4off":
            console.log("S01 is on");
            break;
        case "s01off":
            console.log("S01 is off");
            break;
    }
}

This is my log

Polling...
connected
message received: s01on,a01off,s02on,a02off,s03on,a03off,s04off,a04off,slon,alon,suon,auoff,s05off,a05off,s06off,a06off,s07on,a07off,s08on,a08off,s09on,a09off,s10on,a10off,s11on,a11off,r01on,r02on,r03off,r04off,r05off,r06off,r07off,r08off,p01off,p02off,p03off,s12on,s13on,s14on,n01on,n02on,q1on,qa1off,q2on,qa2off,q3off,qa3off,q4on,qa4off

response: qa4off
S01 is on
disconnected

Can someone help me with this ?
Thank you.

Hi meccip,
for such problems you can use the free AI like MS Pilot or Google Gemeni …
I copied your question and asked the Pilot AI - here is the answer:

It looks like you’re only capturing the last message from the array because you’re using the .pop() method, which returns the last element of the array. If you want to process each message individually, you should iterate through the array and handle each message accordingly.

Here’s an updated version of your onPoll function that processes each message:

function onPoll() {
    console.log("Polling...");
    tcp.send("req2");
    sleep(1000);
    var data = tcp.receive({timeout: 1000});
    console.log("message received: " + data);
    
    var responses = data.trim().split(",");
    responses.forEach(function(response) {
        console.log("response: " + response);
        switch (response) {
            case "qa4off":
                console.log("S01 is on");
                break;
            case "s01off":
                console.log("S01 is off");
                break;
            // Add more cases as needed
        }
    });
}

This code will iterate through each message in the array and process it according to the cases defined in the switch statement. You can add more cases to handle other messages as needed.

Here is another version improved by the AI:

function onPoll() {
    console.log("Polling...");
    tcp.send("req2");

    setTimeout(() => {
        const data = tcp.receive({timeout: 1000});
        console.log("message received: " + data);

        const responses = data.trim().split(",");
        responses.forEach(handleResponse);
    }, 1000);
}

function handleResponse(response) {
    console.log("response: " + response);
    switch (response) {
        case "qa4off":
            console.log("S01 is on");
            break;
        case "s01off":
            console.log("S01 is off");
            break;
        // Add more cases as needed
        default:
            console.log("Unknown response: " + response);
    }
}

This version of the code is more modular and uses modern JavaScript practices.

Thank you, Kalle
This is what i am looking for. The improve version give me some trouble, block my arduino and app. I use the first one.

This is my log:

connected
Polling...
connected
message received: s01on,a01off,s02on,a02off,s03on,a03off,s04off,a04on,slon,alon,suon,auoff,s05off,a05off,s06off,a06off,s07on,a07off,s08on,a08off,s09on,a09off,s10on,a10off,s11on,a11off,r01on,r02on,r03off,r04off,r05off,r06off,r07off,r08off,p01off,p02off,p03off,s12on,s13on,s14on,n01on,n02on,q1off,qa1off,q2off,qa2off,q3off,qa3off,q4off,qa4off

response: s01on
response: a01off
response: s02on
response: a02off
response: s03on
response: a03off
response: s04off
response: a04on
response: slon
response: alon
response: suon
response: auoff
response: s05off
response: a05off
response: s06off
response: a06off
response: s07on
response: a07off
response: s08on
.........

Nice to hear one of the solutions does the work :wink:

Offtopic question, were you on the NetIO App forum when it was active?

Puh, that was a long time ago, but I think it was also KalleVC or just Kalle or maybe VoxCommando, at least I signed every post of mine with Kalle. Were you also on the NetIO forum?

Kalle

Yes, I was. I still use netIO. It was nice that online designer.

Yes, I actually still use NetIO on one of my tablets. But if changes need to be made, it’s too time-consuming for me without a designer.
I am glad that THR is not dependent on a cloud service.