HTTP commands open up wider way to control. This is the corrosponding GitHub repository with a basic example matched with this guide. Already we have shown the serious usages of the method through our Controlling AC Powered Appliances With ESP32 guide. Here is a Discussion on URL Format or Sending Commands Over HTTP (cURL) to IBM Watson IoT.
IBM’s platform is configured to accept events
from the device and commands
from an application i.e. the cURL command over HTTP. We need different headers for authentication. The devices authenticate with user-token-auth and the device’s token. But the applications authenticate with API keys. Here is documentation on IBM’s website.
Look at the code on GitHub. Notice the below lines :
---
1 2 3 4 5 | … #define CMD_STATE "/gpio/" const char commandTopic[] = "iot-2/cmd/+/fmt/+"; … |
When we run this command the LED becomes ON :
1 | curl -u <use-the-API-Key>:<use-auth-token> -H "Content-Type: text/plain" -v -X POST http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/gpio -d "on" |
When we run this command the LED becomes OFF :
1 | curl -u <use-the-API-Key>:<use-auth-token> -H "Content-Type: text/plain" -v -X POST http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/gpio -d "off" |
Our command URL is this :
1 | http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/gpio |
We can not alter this format much as that will not match IBM’s server configuration. You can change the phrase gpio
to something like led
to test – that will work when you change the #define CMD_STATE "/gpio/"
line to #define CMD_STATE "/led/"
. IBM’s configuration supports nomenclature such as gpio1
, led1
and so on. That is a helpful trick to add multiple things.
The second part you need to know is about this line :
1 | const char commandTopic[] = "iot-2/cmd/+/fmt/+"; |
I used +
as a wildcard in two places – the +
between /cmd/
and /fmt/
could be changed to gpio
for the above example code.
You can change it the code to something like the below :
1 2 | const char commandTopic1[] = "iot-2/cmd/led/fmt/+"; const char commandTopic2[] = "iot-2/cmd/led1/fmt/+"; |
So, in your code if you decide to change the end of the command to fool
, you should change two lines on code :
1 2 3 | #define CMD_STATE "/fool/" const char commandTopic[] = "iot-2/cmd/fool/fmt/+"; |
The command to make it working will be :
1 | http://<your org>.messaging.internetofthings.ibmcloud.com:1883/api/v0002/application/types/<yourDeviceType>/devices/<yourDeviceId>/commands/fool |
So, you are changing 3 things to rename a thing :
1. The #define CMD_STATE
line
2. The const char commandTopic[]
line
3. End of the URL to send cURL command i.e. ../devices/
We already know that we can use an Android app to create button and send the cURL requests to switch on and switch off the LED.
This ends this guide. Our next chapters will be connecting it to Node-RED to create a web toggle button.
Tagged With how to send a curl command to an iot device