Function digitalwrite not found when REST API(solved)

I did remember that Particle did provide a REST API to make pin on and off by calling digitalwrite function.

After I loaded a firmware, when I called this API, it doesn’t work. is it because this API only works for the official firmware

for example

curl https://api.particle.io/v1/devices/51ff73165082554910410187/digitalwrite?access_token=c30b010a6fbd25e0f081f6ba6ecfbaa6d6b7f4c4 -d params=D7,HIGH

but the server gives me back a response saying

{
  "ok": false,
  "error": "Function digitalwrite not found"
}

Partly Correct. It is not so much the "official firmware" but rather "adequate code". If you were to create a function called digitalwrite, you'd also be able to toggle it, provided your code handles it correctly. The REST API will also work with your own code, but you'll have to adjust your code/calls accordingly. If you haven't got a function digitalwrite, it becomes really hard to use it, right? All of this is described in the Tinker section of the docs, since that's the code that runs as factory firmware/reset. Over here, it explains how to Use Tinker with your own code.
Here you can see examples of how to use fucntions, and call them correctly.
If you want an easy way to check what functions/ variables are available, and like to call them, you could take a look at this page I've made with the Particle JavaScript library. Just log in with your Particle credentials, and you should be good to go (I won't have access to it!) :smile: Alternatively, you could call the API directly, using this URL:
api.particle.io/v1/devices/{device ID here}?access_token={access token here}

Let me know if you need further explanation.

2 Likes

@helxsz Here is the the code to open a web page that has two buttons you can use to pass a variable to a function. You will have to substitute your function name, your device ID and your token. It works very well. If all you want to do is experiment with running a function this will do it. I’m sure you could expand the example to get variables. Thanks to @bko for this example page.

<html>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  <body>
    <P><br>Toggle Digital Output Pin 0<br><br></P>
    <script type="text/javascript">
      var deviceID    = "123YourDeviceId456";
      var accessToken = "123YourAccessToken456";
      var setFunc = "lightLed0";   <----- replace lightLed0 with the name referenced in particle.function
      var newValue = "0";
      
      function sparkSetLED(newValue) {
      	var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + setFunc + "/";
      	$.post( requestURL, { params: newValue, access_token: accessToken });
      }
    </script>
    <button id="pushmeoff" onclick="sparkSetLED(0);">Push Me Off!</button>
    <button id="pushmeon"  onclick="sparkSetLED(1);">Push Me On!</button>
  </body>
</html>

Hi @Bendrix

You are right that in a browser you cannot just type in a URL to POST to a form which is what a Particle function requires, but curl is smart about figuring out if a request should be a GET or a POST and with -d argument to curl, it will be a POST and work fine.

1 Like

If I prefix the https:// url instructions with ‘curl’, the browser tells me that Google could not find a document. :smile:

That’s because CURL is a command line program you’ve got to install (depending on your OS) and thus has nothing to do with your browser :wink:

1 Like

I guess I’m one of those who just can’t tell a joke… :wink:

1 Like

I’m facing the same problem. Received a Photon Wifi development kit at SOLID con last month. Trying to access the API at http://docs.particle.io/photon/tinker/#the-tinker-api-digitalwrite - get the same “function not found error”. Any idea why their own documentation isn’t working?

You mean this?

curl https://api.particle.io/v1/devices/<YOURDEVICEID>/digitalwrite -d access_token=<YOURACCESSTOKEN> -d params=D0,HIGH

Have you got curl installed and replaced the <YOUR....> portions with your personal data?

Thanks @ScruffR yes I did. digital write or the other functions all return the same error

If I just want a list of devices, that works. What else do I need to do?

Just to make sure that your code is okay, could you check on this page which functions are available, and if you’re able to trigger them?

1 Like

Thanks @Moors7. Yes, I’m able to see my device listed. When I clicked on the events, I can see a set of events…

event: spark/status
data: {“data”:“online”,“ttl”:“60”,“published_at”:“2015-07-12T16:45:07.661Z”,“coreid”:"{deviceID}"}

event: spark/cc3000-patch-version
data: {“data”:“null”,“ttl”:“60”,“published_at”:“2015-07-12T16:45:07.759Z”,“coreid”:"{deviceID"}

This works - https://api.spark.io/v1/devices/{deviceID}?access_token={access_token}

This doesnt - https://api.spark.io/v1/devices/{deviceID}/digitalwrite?access_token={access_token}&params=D0,HIGH.

It returns

{
“ok”: false,
“error”: “Variable not found”
}

btw, it takes a while to get the event stream. you should check out www.zettajs.org for an instant access to web socket data stream.

But, did it show the functions you were looking for, digitalwrite, and if so, were you able to call them successfully using that page? The events are irrelevant for functions/variables.
I'm not familiar with zettajs, but it appears to already support the Particle ecosystem, so there's no reason you shouldn't be able to use that, should you so desire.

Where did you try that, in a browser? Since a function call requires a POST request, that won't work, considering a browser makes GET request.

Thanks @Moors7. On ur site, I only see my photon - don’t see any functions.

I tried the API in browser and Postman. Neither worked

Then it is quite likely that you've got some firmware running on your device that doesn't expose any Spark.function or Spark.variable.
If you want to call digitalwrite you either have to reflash Tinker firmware, or flash your own firmware which exposes such a function, or do a factory reset, which also reverts back to Tinker.

1 Like