Turn a sensor on or off using the rest of Arduinio IOT Cloud APIs

Good morning, I am new to arduino and arduino IOT CLOUD, I was doing tests in postman with the apis to be able to turn my sensor on and off, but I could not identify the specific api to be able to do it, I was trying with:

[https://api2.arduino.cc/iot/ v2/things/{thing_id}/ properties/{property_id}/ publish]

This endpoint does update the boolean value of my sensor and it is reflected in the arduino iot cloud dashboard but it fails to turn on my sensor. Could someone please help me? I will appreciate your prompt response.

Hello, well if you are really new to arduino is complicate when you don't understand the basic, example

void loop()
{
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(1000);
}

It can be frustrating at first not knowing the basics, but I would not recommend touching IOT CLOUD, without knowing the basics... my recommendation would be to start turning a led on and off using the mobile's Bluetooth, do very basic things like control a servo and etc... Once you understand this... Basically there is not much history, little difference you will see between turning on a Led and a motor of a giant machine.
The better way to understand it is use C

Like https://youtu.be/Bz4MxDeEM6k

And https://youtu.be/ssJY5MDLjlo

You can learn a lot in just 15 hours.

Thanks for the answer, what happens is that I am working with Auduino Iot cloud I already have my code in C and the configuration of my dasboard in arduinio iot cloud what I really want is to make my own dasboard in angular and for that reason I Doubt I want to turn on the sensor from my own dasboard, for this I went to the rest api provided by arduino iot cloud what I want you to help me if possible I turn my sensor on and off through these rest api

1 Like

I have deleted your other cross-post @unsaacdevelop.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Can you share the full API call you are using? (body and headers).
What is the return code and message that you get?

Hi @unsaacdevelop,
can you also share the C code running on your board?

This is the request in postman and according to the documentation the return is with status 200 . Please, maybe I'm making a mistake in the api, maybe it's another one so I can turn my led on and off in this case, because if I can do this, I can turn any sensor that I implement on and off

This is the code which interacts with the board which turns a led on and off because I want to do the same thing using the rest api provided by the arduino iot cloud but I can't make my led turn on and off more, just update the value to true more It is not reflected that my led turns on or off I attach the images of the code in C and the execution in postman of the api that changes the value of the led property


Can you try without the device_id?

Hi @unsaacdevelop ,
I was able to turn on and off a LED with Arduino IOT REST API, using CURL from the command line, I haven't tried with Postman but it should be quite similar.
These steps are mostly documented in https://docs.arduino.cc/arduino-cloud/getting-started/arduino-iot-api (slightly adapted to use CURL).

Pre-requisites: collect the following variables (which, from your screenshots, you should already have)

  • client_id
  • client_secret
  • thing_id
  • property_id
  1. Get the authorization token <access_token>
curl --request POST \
  --url 'https://api2.arduino.cc/iot/v1/clients/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'grant_type=client_credentials' \
  --data 'client_id=<client_id>' \
  --data 'client_secret=<client_secret>' \
  --data 'audience=https://api2.arduino.cc/iot'
  1. Extract the access_token from the response
{
  "access_token":"eyJh[...]GERs",
  "expires_in":300,
  "token_type":"Bearer"
}
  1. Using this access_token, launch this CURL command to turn the led ON:
curl -X PUT \
  --url "https://api2.arduino.cc/iot/v2/things/<thing_id>/properties/<property_id>/publish" \
  -d '{"value": true}' \
  -H "Authorization: Bearer <access_token>" \
  -H "Content-Type: application/json"
  1. To turn it OFF, just replace "value": true with "value": false.
    I think you can use also "value": 0 or "value": 1 since they will be interpreted as boolean values.

Regarding the sketch, this is what I used, it should be almost identical to yours (I've just used the built-in LED for simplicity.

// .ino file
#include "thingProperties.h"

void setup() {
  Serial.begin(9600);
  delay(1500); 

  initProperties();

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
}

void onLedChange()  {
  digitalWrite(LED_BUILTIN, led);
}

I hope I didn't do any mistakes copying and pasting my code :smiley:

Let me know if this helps.

Christian.

Thank you very much helped me a lot and as you indicate you do not need to send the device id thanks for your estimated response

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.