Executing curl from particle function

I want to execute a curl command from a Particle function.

I have the below code, but it does not compile. Any help would be highly appreciated.

// -----------------------------------

void setup()
{
   Particle.function("DSon",dsOn);
   Particle.function("DSoff",dsOff);
}

void loop()
{
   // Nothing to do here
}

int dsOn(String command) {
    Process proc = Process::run("curl http://localhost:80/myapp/mediaplayer/setStandbyMode?value=false");
    proc.wait();
}
int dsOff(String command) {
    Process proc = Process::run("curl http://localhost:80/myapp/mediaplayer/setStandbyMode?value=true");
    proc.wait();
}
tester.cpp: In function 'int dsOn(String)':
tester.cpp:32:5: error: 'Process' was not declared in this scope
 
     ^

tester.cpp:32:13: error: expected ';' before 'proc'
 
             ^

tester.cpp:33:5: error: 'proc' was not declared in this scope
 void loop()
     ^

I just tested your code building locally, and there was nothing wrong it, except you defined dsOn and dsOff too late:

Here is your corrected code:


#include "Particle.h"

int dsOn(String command)
{
        Process proc = Process::run("curl http://localhost:80/myapp/mediaplayer/setStandbyMode?value=false");
        proc.wait();
        return 0;
}

int dsOff(String command)
{
        Process proc = Process::run("curl http://localhost:80/myapp/mediaplayer/setStandbyMode?value=true");
        proc.wait();
        return 0;
}


void setup()
{
        Particle.function("DSon",dsOn);
        Particle.function("DSoff",dsOff);
}

void loop()
{
// Nothing to do here
}

Also, how are you building? Make sure you are building for Raspberry Pi.

Have you double checked you set your build target for RPi?

While prototyping or early declaration is good practice, that statement is not 100% true.
The Wiring preprocessor will do the prototyping for you when cloud building an .ino file :wink:

1 Like

well...

not 100% of the time!

Sometimes I have to prototype functions, but not always. Why may that be (not RPi related)?

2 Likes

I’ve seen your post about the struct parameter which left me puzzled as well, but in above case that shouldn’t be a problem.

However AFAIK, @jvanier had tweaked the preprocessor not too long ago to cause less hassle with “one-line if() ... else blocks” and these ridiculous blank-line-or-not trip-ups.
Maybe that might be a side-effect of that.

Have you double checked you set your build target for RPi?

This may sound kind of completely ignorant, but how would I do/check that? Couldn't find it in the docs.

Thanks for all your suggestions. As you can see I am not a regular coder but really appreciate your help.

How are you building? Web IDE, Particle Dev, Local?

How are you building? Web IDE, Particle Dev, Local?

I think I figured it out. I selected my RPi as my device. Building from Web IDE.

Selecting RPi as my device now gives a very different output, but still doesn't compile.

Processing  /workspace/tester.ino
make -C ../newlib_nano 
make[1]: Entering directory '/firmware/newlib_nano'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/firmware/newlib_nano'
make -C ../user 
make[1]: Entering directory '/firmware/user'
Building cpp file: /workspace/tester.cpp
Invoking: ARM GCC CPP Compiler
mkdir -p ../build/target/user/platform-31/workspace/
arm-unknown-linux-gnueabi-gcc -DPLATFORM_THREADING=0 -DPLATFORM_ID=31 -DPLATFORM_NAME=raspberrypi -DUSBD_VID_SPARK=0x1D50 -DUSBD_PID_DFU=0x607F -DUSBD_PID_CDC=0x607D -DEMBEDDED_TARGET=0 -g3 -O3 -gdwarf-2 -Wno-unused-local-typedefs -DINCLUDE_PLATFORM=1 -DPRODUCT_ID=31 -DPRODUCT_FIRMWARE_VERSION=65535 -DSYSTEM_VERSION_STRING=0.6.0-rc.1 -DRELEASE_BUILD -I./inc -I../wiring/inc -I../system/inc -I../services/inc -I../communication/src -I../hal/inc -I../hal/shared -I/workspace/ -I./libraries -I. -MD -MP -MF ../build/target/user/platform-31/workspace/tester.o.d -ffunction-sections -fdata-sections -Wall -Wno-switch -Wno-error=deprecated-declarations -fmessage-length=0 -fno-strict-aliasing -DSPARK=1 -DPARTICLE=1 -DSTART_DFU_FLASHER_SERIAL_SPEED=14400 -DSTART_YMODEM_FLASHER_SERIAL_SPEED=28800 -DSPARK_PLATFORM_NET=gcc -fno-builtin-malloc -fno-builtin-free -fno-builtin-realloc  -DLOG_INCLUDE_SOURCE_INFO -DMODULE_VERSION=0 -DMODULE_FUNCTION=3 -DMODULE_DEPENDENCY=0,0,0 -D_GNU_SOURCE -D_WINSOCK_H -DLOG_MODULE_CATEGORY="\"app\""  -fno-rtti -fcheck-new -std=gnu++11 -c -o ../build/target/user/platform-31/workspace/tester.o /workspace/tester.cpp
/workspace/tester.cpp: In function 'int dsOn(String)':
/workspace/tester.cpp:20:5: error: expected ';' before 'return'
 void setup();
     ^

/workspace/tester.cpp:21:1: warning: no return statement in function returning non-void [-Wreturn-type]
 void loop();
 ^
/workspace/tester.cpp: In function 'int dsOff(String)':
/workspace/tester.cpp:28:1: error: expected ';' before '}' token
 }
 ^

../build/module.mk:264: recipe for target '../build/target/user/platform-31/workspace/tester.o' failed
make[1]: Leaving directory '/firmware/user'
../build/recurse.mk:11: recipe for target 'user' failed
make[1]: *** [../build/ta

rget/user/platform-31/workspace/tester.o] Error 1
make: *** [user] Error 2

It sounds like you are missing two semicolons. What happens if you try the code I posted above?

If you use @nrobinson2000’s code it should compile.

Your code in the opening post is missing the return instructions in the functions that are supposed to return an int.

But actually you should not “wait” in a Particle.function() callback - neither by using delay() nor by proc.wait() (unless you’ll be sure to return from the callback within 3sec).

1 Like

Code verified. Good work!

It even compiles when the functions are put after the loop()

Good work indeed. I can now continue my quest. Thanks so much!

But actually you should not "wait" in a Particle.function() callback - neither by using delay() nor by proc.wait() (unless you'll be sure to return from the callback within 3sec).

Yes, I can see why that is.

I actually copied the docs from the reference pages:

// SYNTAX
process.wait();

// EXAMPLE USAGE
// Run a Javascript program
Process proc = Process::run("node /home/pi/update.js");
proc.wait();

I will test without the wait().

Thanks for flagging this!