[SOLVED]POST Request not working

Hello I am trying to use a website to control my photon. However whenever I send a POST request for my function is says:

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

However, when I go to https://api.particle.io/v1/devices/My_ID?access_token=My_Token it shows:

   {
      "id": "Device_id",
      "name": "steve",
      "connected": true,
      "variables": {},
      "functions": [
        "ledControl"
      ],
      "cc3000_patch_version": "wl0: Nov  7 2014 16:03:45 version 5.90.230.12 FWID 01-6176f4f",
      "product_id": 6,
      "last_heard": "2016-01-24T17:27:04.938Z",
      "status": "normal"
    }

Output of particle list in CLI is:

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\Username>particle list
steve [Device_id] (Photon) is online
  Functions:
    int ledControl(String args)

Help?

Seeing the code you’re using would help in understanding why it’s not working. You could try this page to check if your functions are working properly. If so, then there’s probably an issue with your HTML

Here’s the HTML of my site. Essentially the same as the Internet LED example on Particle.io.

The Spark Dashboard page does send the commands successfully

<!DOCTYPE html>
<html>
<head>
    <title>Led Controller!</title>
</head>
<body>
<center>
    <br><br>
    <table>
        <tr>
            <th>Strand 1</th>
            <th>Strand 2</th>
        </tr>
        <tr>
            <td>
                <form action="https://api.particle.io/v1/devices/Device_ID/ledControl?access_token=Token" method="POST">
                <br>
                <input type="radio" name="args" value="1,255,0,0">Red
                <br>
                <input type="radio" name="args" value="1,255,255,0">Orange
                <br>
                <input type="radio" name="args" value="1,0,255,0">Green
                <br>
                <input type="radio" name="args" value="1,0,255,255">Teal
                <br>
                <input type="radio" name="args" value="1,0,0,255">Blue
                <br>
                <input type="radio" name="args" value="1,255,0,255">Purple
                <br>
                <input type="radio" name="args" value="1,255,255,255">White
                <br>
                <input type="radio" name="args" value="1f,15,255">Slow Fade
                <br>
                <input type="radio" name="args" value="1f,1,255">Fast Fade
                <br>
                <input type="radio" name="args" value="1j,150">Fast Jump
                <br>
                <input type="radio" name="args" value="1c">Off
                <br>
                <br>
                <input type="submit" value="Do it!">
            </td>
            <td>
                <form action="https://api.particle.io/v1/devices/Device_ID/ledControl?access_token=Token" method="POST">
                <br>
                <input type="radio" name="args" value="2,255,0,0">Red
                <br>
                <input type="radio" name="args" value="2,255,255,0">Orange
                <br>
                <input type="radio" name="args" value="2,0,255,0">Green
                <br>
                <input type="radio" name="args" value="2,0,255,255">Teal
                <br>
                <input type="radio" name="args" value="2,0,0,255">Blue
                <br>
                <input type="radio" name="args" value="2,255,0,255">Purple
                <br>
                <input type="radio" name="args" value="2,255,255,255">White
                <br>
                <input type="radio" name="args" value="2f,15,255">Slow Fade
                <br>
                <input type="radio" name="args" value="2f,1,255">Fast Fade
                <br>
                <input type="radio" name="args" value="2j,150">Fast Jump
                <br>
                <input type="radio" name="args" value="2c">Off
                   <br>
                <br>
                <input type="submit" value="Do it!">
            </td>
        </tr>
    </table>
    <br>
    <br>
    <b>Both Strands</b>
    <form action="https://api.particle.io/v1/devices/Device_ID/ledControl?access_token=Token" method="POST">
    <br>
    <input type="radio" name="args" value="3f,15,255">Slow Fade
    <br>
    <input type="radio" name="args" value="3f,1,255">Fast Fade
    <br>
    <input type="radio" name="args" value="3j,150">Fast Jump
    <br>
    <input type="radio" name="args" value="3b,47,255,255,255">Strobe
    <br>
    <input type="radio" name="args" value="3c">Off
    <br>
    <br>
    <input type="submit" value="Do it!">
    <br>
    <br>
    <br>
    <br>

    <form action="https://api.particle.io/v1/devices/Device_ID/ledControl?access_token=Token" method="POST">
      Custom entry!<br>
      
      <input type="text" name="args">
      <br>
        <input type="submit" value="Do it!">
  </form>
</body>
</html>

Just want to double check that you are changing the word “Token” in the above to be your actual hex number token, right?

Yes I am, just removed it for security’s sake

The forum software might have messed up parts of your code due to lacking code formatting. If that happened, could you re-upload that by editing your post? Make sure you format it correctly by using three backtics ( ` ) before and after your code, on their own lines.
If the code you’re using, is the one that’s currently displayed (double check it), then you should really take another look at your code, since it’s missing entire parts. I don’t think it’ll run whatsoever in this state.

That is the actual code I am using. I forgot to add the closing body and html tags but that didn’t change the functionality and the HTML displays just fine in Firefox and Chrome.

I am just taking the code from https://docs.particle.io/guide/getting-started/examples/core/#use and adding more forms and options to the forms. What parts would I be missing?

Edit: Here’s the code on my photon. Not much going on because it just sends commands to an arduino based controller I had already built.

void setup()
{
    Spark.function("ledControl",ledString);

    Serial1.begin(115200);
}


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


int ledString(String command)
{   
    Serial1.print(command+'\n');
    return 53;

}

I suggest completing the code as in making sure everything is closed properly. Currently, it looks as though you’ve got nested forms which may, or may not, cause issues.

Disregarding the issues you’re seeing, you might be interested in a Javascript implementation. It’s nicer in that it doesn’t cause page reloads. It also has the added benefit of there being a library available. Just sayin’ :wink:

I have no idea how to write JavaScript for this kind of application or at all (I do know regular java pretty well though). Know of any good tutorials?

You can have a look at the SDK to see how it works.
There are some (node.js) examples included.
I’ve got an example over here, and here. The page I linked you to earlier is build on the same thing as well. Check the source of the pages to see what’s going on. Shouldn’t be too hard to follow.

Thank’s for the links. I realized that I didn’t close any of the forms with which is what caused the errors. Whoops, looks like I need to invest in a rubber ducky

1 Like