Webhook to do a get

More obvious questions from the brat with the usual number of toes:

I write web software using asp.net. I have a “web page” that takes ?number=x in the url and saves the number in a database for no particular purpose. You can add numbers to my database all you like at:

http://barefootelectronics.com/particleposttest.aspx?number=222

(Until I kill it because I want to make something useful)

I don’t have a page to show you the numbers, but I see the numbers and times in the table in my database.

So I made a web hook to try and put a number from my particle electron. I think I have a screen shot at

And I made my program:

// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------

// First, let's create our "shorthand" for the pins
// Same as in the Blink an LED example:
// led1 is D0, led2 is D7

int led1 = D6;
int led2 = D7;

// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Particle function
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).

int power = A5;
int analogvalue;

void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

   // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
   Particle.function("led",ledToggle);
   // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

   // For good measure, let's also make sure both LEDs are off when we start:
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);
       pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
    pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

    // Next, write the power of the photoresistor to be the maximum possible, so that we can use this for power.
    digitalWrite(power,HIGH);

    // We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("analogvalue", &analogvalue, INT);

}


// Last time, we wanted to continously blink the LED on and off
// Since we're waiting for input through the cloud this time,
// we don't actually need to put anything in the loop

unsigned long timewas = 0 ;

void loop()
{
  unsigned long timeis = millis();
  if ((timeis % 1000) > 500)
  {
      digitalWrite(led2,HIGH);
  }
  else
  {
      digitalWrite(led2,LOW);
  }
  analogvalue = analogRead(photoresistor);
  
  if ((timeis/1000) > timewas )
  {
      timewas = timeis/1000;
      String temp = String(analogvalue);
      Particle.publish("number", temp, PRIVATE);
  }

}

// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.


int ledToggle(String command) {
    /* Particle.functions always take a string as an argument and return an integer.
    Since we can pass a string, it means that we can give the program commands on how the function should be used.
    In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
    Then, the function returns a value to us to let us know what happened.
    In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
    and -1 if we received a totally bogus command that didn't do anything to the LEDs.
    */

    if (command=="on") {
        digitalWrite(led1,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        return 0;
    }
    else {
        return -1;
    }
}


You see in my loop, I particle.publish to my “number” hook every second. I can turn the other LED on and off with the function, and I can see the light level in the variable, but I don’t get entries in my useless table from it getting the url… So obviously, I have my web hook wonky or my logic isn’t actually posting it every second.

Oh! I can see the events on the particle console, so the program logic is logicing, just the web hook.

Exactly the interesting part of your URL is missing in the screenshot.
You can go to the console, edit the webhook and select the CUSTOM TEMPLATE tab to get a copy/pastable JSON definition of your webhook. That would help more :wink:
image

question i have is , why the screenshoot did not render? maybe because the screenshot url was,
http://barefootelectronics.com/freaks/webhook.gif?
so why is a “screenshot” a .gif? there is absolutely no reason i know of to have a “static” data image saved as a .gif
maybe this discussion software will not render a .gif?
which, if so, i find that to be an amazingly good thing since i find .gif files to be one of the most replusive. mind numbing filetypes. i spend a good bit of time dis-allowing .gif files from being rendered in my browser. especially icons for users of various forums which allow .gif filetypes as icons. maybe try a .png of the file and see if it displays on the forum for future reference? if so, to the OP, please move away from the .gif filetype and do a .png or .jpg for your screenshots and consider giving up on .gif files totally as a new year resolution. . off-topic, i know. thanks. Happy New Year.

edit: there is a distinct possibility the .gif did not render in my browser because i have it configured that way. still, there is no reason to have a .gif filetype for static images.

I edited my query parameters:

{
  "number": "{{{number}}}"
}
          

And now my page is getting called but the “number” is always zero. Seems I’m not getting the parameters in the Request.QueryString as coreid and published_at are both null.

The “missing” url is:

http://barefootelectronics.com/particleposttest.aspx?number=222

and I’m too asd to pay attention to your discussion of the relative merits of gif vs png for a screen capture.

Do you see a new entry in your database?

Lots of entries, but my querystring variables seem to contain 0 and null…

     SqlCommand cInsert = new SqlCommand("insert into bfeTestTable( LogDateTime, LogNumber,coreid, publishedat) values (getdate(), @LogNumber, @coreid, @publishedat)", cn);
    Param("@LogNumber", Request.QueryString["number"], cInsert);
   Param("@coreid", "Shozbot", cInsert) ; //Request.QueryString["coreid"], cInsert);
   Param("@publishedat", "now", cInsert); //Request.QueryString["published_at"], cInsert);

If I put Request.QueryString(“published_at”) I get no inserts 'cause it’s nul.

Do you see a value 24680 arriving just a few minutes ago?

I setup a webhook like this

{
    "event": "test",
    "url": "http://barefootelectronics.com/particleposttest.aspx",
    "requestType": "GET",
    "noDefaults": true,
    "rejectUnauthorized": true,
    "query": {
        "number": "{{{PARTICLE_EVENT_VALUE}}}",
        "coreid": "{{{PARTICLE_DEVICE_ID}}}",
        "published_at": "{{{PARTICLE_PUBLISHED_AT}}}"
    }
}

which results in a GET request like that

GET /particleposttest.aspx?published_at=2019-01-01T16%3A22%3A38.656Z&coreid=api&number=24680 HTTP/1.1
User-Agent: ParticleBot/1.1 (https://docs.particle.io/webhooks)
host: barefootelectronics.com
Connection: keep-alive

and rendered this response

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Tue, 01 Jan 2019 16:22:38 GMT
Content-Length: 554
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="./particleposttest.aspx?published_at=2019-01-01T16%3a22%3a38.656Z&amp;coreid=api&amp;number=24680" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZHWUPh5EHcWL4cJzrpF8k7PnsGEweBb92wSMpcAvwxuu" />

<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="441DB91A" />
        <div>

        </div>
    </form>
</body>
</html>

Also, since coreid and published_at are not part of your URL (plus encoded parameters) I’m not surprised you don’t get the respective values.
But with my request you should see these values too.

Yes! I see your number a 9:22 (Mountain time).

And I think your webhook shows me what I need to do…

Thanks!

(surely more silly questions to come)

1 Like

And it’s working! My electron is sending posts and they’re going to my database.

Thanks!

(Now to start thinking about the real application.)

1 Like