Retuning text from Particle.function

Hi All

I found a couple of topics similar to this but not exactly the same. I don’t think I can return text from a function back to the cloud, but would really like to so I’d like to vote for this as a future update.

I’m writing some code in node.js which talks to a machine via the rs232 port (Serial1). I’d like to write a string to the function, the function writes to the serial port, waits for a reply and returns this reply to the node.js via the cloud.

I’ve done a workaround, which is to have a global array of char. I call the write function wihich writes then reads the response into the buffer. Then I read the buffer as a variable.

The buggeration factor in all this is that the calls to the cloud from node.js are asynchronous so it’s difficult to get things to happen in order as the RS232 comms are sequential.

Thanks for reading…

Steve

To my knowledge you send a string into the function and get a string returned back.

Hi @peter_a

A particle function (as the OP knows) takes a String and returns an int. There are two ways to get a string out of your Particle device via the cloud: variables and publish.

I do think that publish and subscribe might fit better for what @picitup wants to do, if the relatively low data rate allowed by publish/subscribe is OK in this application.

Otherwise you can switch to TCP or UDP and handle everything yourself.

I do have one other suggestion on the ordering problem: just put a N-digit sequence number at the start of the function call string and return it in the variable (just don’t send it to the RS-232 device). That way you will always know what input gave a particular response.

1 Like

Well that’s made me think. I’ve not used publish/subscribe, but will swot up and try it out. I think that’s where the core calls an event on the cloud. As you’ve guessed, the data volume is low (up to 30 bytes max) and the frequency is low too (once every 10 secs).

I don’t want to switch to to TCP or UDP as it might just break my brain lol.

The sequence number idea is very good and I’ll put some thought into that. A bit like packet numbers on a network. I could even have a circular buffer with ID prefixes scattered throughout as all the data is low order ASCII.

Hmm… it’s 9pm here so I think I’ll have a whisky and play more tomorrow.

Thanks again for your very prompt reply and useful suggestions.

Kind Regards

Steve

1 Like