I’m trying to wrap my head around webbooks and I’m not completely sure if it’s what I need for the things I’m trying to achieve.
What I want is for the data that is being published into the ‘events’ tab of my sandbox console to be sent to a URL. By this I mean that the data is being put at the end of the URL. I then have php and sql code that is basically fetching the values from the URL and sending them to a phpMyAdmin database. For example:
I would want the webhook to be posting the data after the ‘?’
Do I have the right idea about what a webhook does? Does the integration actually send the data to the URL like I’m imagining? Is it possible to integrate the data with a phpMyAdmin database?
Yes, you can use webhooks for that - although with a simple HTTP target you could use HTTPClient just the same.
Webhooks are mainly used to request data from a remote server - particularly from HTTPS targets.
That is up to your target server to interpret the request and insert data into your DB.
your information is encrypted from the Particle device to the cloud, then from there it depends if your server supports HTTPS.
your firmware does Particle.Publish(data) and the webhook takes care of sending the data.
you need to create the webhook according to your needs.
creating the webhook can get complex sometimes.
HTTPClient:
your information is not encrypted.
your firmware needs to manage the HTTP connection: establish it when needed, send the data, close the connection, or what happens with the connection when there is a disconnect, reconnect, timeout.
your firmware can get complex in that sense.
I would read about webhooks here, and if that is your cup of coffee, go ahead and try one.
If you feel more natural at c++ level, HTTPClient might be what you want.
Is this for personal (or other low-volume) use, and do you intend to run the PHP server on your own server, possibly on your own LAN, with NAT? If so, here’s how I would do it:
Instead of using a webhook, use the Google cloud integration. This eliminates the need to have a public IP address for your receiving server, and also implements it securely, so you don’t need a TLS certificate for your server.
The other thing about the GCP integration is that it queues events when they are not being fetched. So if your server goes down temporarily, the events will not be lost. This is handy when you are running on a server at your home.
There are GCP pub/sub clients for many languages, including PHP. This will will allow you fetch available events from GCP and add them to your database in PHP.
You still just Particle.publish from your Particle device.
I guess I should mention that I really just want the data to be sent to the URL like I described. I have done similar projects before so I think it would make things easier for me in the long run since I’m familiar, but I’m not dead set on it.
I have used HTTPClient with wifi projects before on firmware, but I would like to learn how to use the webhooks.
Just so I understand correctly - if I want the webhook to be sending data to the end of a URL like I described, I would need to do the following:
have an event name (ex - sensor) that I define in the webhook
in my firmware use this event name : Particle.publish("sensor", aReading, PRIVATE);
Since it has the “sensor” trigger, the webhook will take it from the cloud console and send it to the URL
But how do I get it to be in the format of : http://url.com/sample.php?sensorNum=1&turb=3&tds=5&time=64
Do I change the HTTP settings in the webhook and add something here?
I did what you said and got the publish to send data like this in my firmware Particle.publish("reading","?sensorNum="+sensorNum+"&turb="+turb+"&tds="+tds+"&time="+what, PRIVATE);
The console seems to be receiving this and the webhook is saying it’s working
But, nothing is being sent to the server. If I copy and paste the ?sensorNum=2&turb=0.000000&tds=0.000000&time=44 being posted in the data column of the console on the end of my URL (simulating it being sent to it), my server does receive the data and it is sent to the database.
So, I know the receiving part is working and then webhook is initiating, but it’s still not actually sending the data to the end of my specified URL. I’m assuming there’s some setting that’s off in my webhook, but I’m not sure which direction to go in
If you want to pass the arguments in the URL like you originally posted, you’ll need to switch to GET instead of POST.
If you need both the parameters from the event and data sent from the device, you’ll need to switch things around completely and move to sending JSON from the device and JSON in the webhook using POST in order to combine both methods.
Technically, that’s not entirely true. In HTTP you can mix query and POST parameters, but I think PHP will only return the POST data.
In the pictures above, you selected POST and default form fields. All of those fields, event, data, coreid, and published_at are sent form URL encoded to your server in the body of the post (not the URL).
The query parameters are blank, right? If you put anything in there it will mess up the URL that you set.
After switching to GET, what does the actual URL look like when you view the integration logs?