Calling functions over the internet (CURL)?

Hi All,
I am trying to use the RcSwitch library and port over some Arduino code I am using. I am unsure how to send a curl message so that I can get my 433 outlets to turn on and off. With this code in Arduino I can just open a serial and press q or whatever to turn it on or off. Anyone kind enough to take a look and offer any suggestions what the curl would look like for this code base? Thank you!

include "RCSwitch/RCSwitch.h"

int message = 0; 

RCSwitch mySwitch = RCSwitch();

void setup() {

  // Transmitter is connected to Spark Core Pin D0
  mySwitch.enableTransmit(D0);

  // Optional set pulse length.
  mySwitch.setPulseLength(185);

}

void loop() {

 delay(5); 
if(Serial.available() > 0){ 
message = Serial.read(); 
switch(message){ 
case 'q': 
mySwitch.send(5321987, 24); 
break; 
case 'w': 
mySwitch.send(5321996, 24); 
break; 
case 'e': 
mySwitch.send(5592515, 24); 
break; 
case 'r': 
mySwitch.send(5592524, 24); 
break; 
case 't': 
mySwitch.send(5592835, 24); 
break; 
case 'y': 
mySwitch.send(5592844, 24); 
break; 
case 'u': 
mySwitch.send(5594371, 24); 
break; 
case 'i': 
mySwitch.send(5594380, 24); 
break; 
case 'o': 
mySwitch.send(5600515, 24); 
break; 
case 'p': 
mySwitch.send(5600524, 24); 
break; 
}

In order to gain access to the USB serial you have to do something like Serial.begin(115200); inside setup(). BTW: The same would be advisable for Arduino too :wink:

But if you want to do this via WiFi (e.g. using curl), you’d need to set up some Spark.function()

Docs are a great asset - but only when you use them!

1 Like

Thanks. Yes I want to use it wiFi and I have reviewed and studied the docs, just do not get it, sorry not a native programmer so just trying to get some help and trying to make it work. I guess I just do not understand how to set up a spark.function, I will keep researching and reading ( I have read the docs many, many times), I guess some things just do not come as easy as old hat programmers, was only trying to get some help.

That’s the code from the docs plus some of the code for your purpose

include "RCSwitch/RCSwitch.h"

RCSwitch mySwitch = RCSwitch();

int brewCoffee(String command);  // name it as you wish (and the one bellow accordingly)

void setup()
{
  // Transmitter is connected to Spark Core Pin D0
  mySwitch.enableTransmit(D0);
  // Optional set pulse length.
  mySwitch.setPulseLength(185);

  // register the cloud function
  Spark.function("brew", brewCoffee);
}

void loop()
{
  // this loops forever
}

// this is where the logic of your original loop would come in
// if you send the respective number, you can use this
int brewCoffee(String command)
{
  int cmd = command.toInt();

  mySwitch.send(cmd, 24); 

  return cmd;  // report back what was received
}

Sample curl command

curl https://api.particle.io/v1/devices/<YOUR-DEVICE-ID>/brew -d access_token=<YOUR-ACCESS-TOKEN> -d "args=5321987"

Not that complicated :wink:

2 Likes

Do check this out:

2 Likes

Thank you both I greatly appreciate it. I will give this a shot. I am going to study hard just tough to know where to begin so I appreciate the community and help. Would either of you suggest learning C or C+ in order to better understand this stuff, or should I study another language to help avoid these questions in future? Again, thank you very much, I really appreciate it. Fingers crossed as this will open up a less expensive way to automate my home.

1 Like

You are brilliant, it worked! I can not thank you enough, now to figure out what is going on but it worked like a charm. Again, many thanks!

2 Likes

I’d say start with C and work your way up to C++. C is the basics you should probably know. For web development, Javascript is a good choice, considering you can also run server applications with it thanks to node.js. It’s the standard scripting language, supported by all browsers. Worthwhile getting to know if you’re planning on doing anything on the web.

1 Like

One last thing I promise, I would like to set this up as a Http POST in tasker, not sure if you have any experience here but I am not sure how to handle the args part. I found the below, but not sure how to format with this code you provided above. It seems the data is where I would put the args, but not sure what format or how to go about this, or if I included the -d in the path or not. Again, no pressure just thought I would ask as I would really like to be able to use voice with tasker to turn on my outlets. I kept the above code with Brew and everything so if you have any idea please let me know and again THANK You SO MUCH!!!

Server:Port
https://api.spark.io

Path
/v1/devices/xxxxxxxxxxxxxx/exposed_func?access_token=zzzzzzzzzzzzzzzzzzzzzzzzzzzz

Data/File
args=mode=2,flag_one=0

I got it figured out in Tasker thanks so much again!

Sorry for not responding, but I didn’t see your post any earlier.

Good to know you’ve figured it out :+1:

Just for people finding this thread, could you always also describe your found solution?

@ScuffR- will do, again thank you!

Hi All,
So I set up a home automation system and have a mix of wifi, Zwave, NRFL24L01, and 833 Mhz. It has been hard to find a central control that allows all these types of communications. Laking the time to custom build a solution I found a great application called Tasker. I can now use Particle to manage these different types of connections and communications. From there, I am using Tasker to control scenes and devices. Thanks to everyone here, my last hurdle was trying to get my 833 outlets to work, I now can control those with the photon by using Tasker to manage commands and strings I need to send.

To Add HTTP POST to Tasker using Particle Cloud

Open Tasker
Press 'Menu’
Select 'Preferences’
Select the ‘UI’ tab
Untick 'Beginner Mode’
Exit ‘Preferences’ (press ‘Back’ button)
Press the plus symbol
Enter a new profile name
(This would normally be your current/desired location e.g. ‘Home’)
Press the tick symbol
(You are now shown a list of Profile options)
Select ‘State’
(You are now shown the Select State Category screen)
Select ‘Net’
(You are now shown the Select Net State screen)
Select ‘Wifi Near’
(You are now shown the State Edit screen)
Press the magnifying glass on the SSID section
(You will see a list of one or more Wifi Access Points - select yours from the list. The SSID name is now filled in.)
Press Back
Select 'New Task’
Enter a new task name
(This would normally be an action name e.g. ‘Set Indicator Home’)
Press the tick symbol
(You are now shown the Task Edit screen)
Press the plus symbol
(You are now shown the Select Action Category screen)
Select ‘Plugin’
(You are now show the Action Plugin screen)
Select ‘RESTask’
(You are now shown the Action Edit screen)
Press the pencil symbol
(You are now shown the RESTask edit screen)
Change ‘Request Type’ to POST
Change ‘Host’ to https://api.spark.io/v1/devices/{device_id}/{function_name}
(Where {device_id} is the Device ID of your Spark Core and {function_name} is the exposed function in your Spark Core firmware)
Swipe across to 'Parameters’
Press 'Add More’
In the ‘name’ box, type 'access_token’
In the ‘value’ box, type the Access Token for your Spark Core
(Your access token can be found in the Spark Build web IDE on the ‘Settings’ page)
Press 'Save’
Press 'Add More’
In the ‘name’ box, type 'args’
In the ‘value’ box, type the data you want to pass
Press 'Save’
Press the save (floppy disk) symbol
Keep pressing the back button to save and exit out of Tasker
Your first Tasker Profile and Task have now been set up to set the position of the Indicator :slight_smile:

Now add voice control with autovoice for tasker, or control it by time, date, or even location.

1 Like

Just wanted to update this to a better method I detailed in this post