[SOLVED] Control LED using Ubidots Dashboard

Hi All,

I have managed to control Led using particle function, and now i can read from ubidots if the led ON or OFF, however, i need to control it now using ubidots switch.

I used this function:
Particle.function(“led”,ledToggle);

when i try to send this function to ubidots it says its string “i cant send it”.

any help please.

Many Thanks

Greetings, please refer to the fifth example of this article.

All the best

1 Like

Thank you @jotathebest

I tried method 5. Create Ubidots Webhook to Particle to Control Things, but still not working, can you please confirm to me the below changes:

URL: https://api.particle.io/v1/devices/events (i leave it as its)

Headers:
Content-Type: application/x-www-form-urlencoded (i leave it as its)

Body: name=UbidotsWebhook&data={{last_value}}&access_token=(i put my particle Token here)

is that right?
and do we need to put Ubidots access token?

Thank you very much

Hi All,

I managed to control the LED using the Webhook but its not sufficient if i can say so, when i turn the switch in ubidots i can see the response and working fine (LED On/Off), however, if i left my desk for 5 minutes and back and switch on and off nothing happen (no response), any idea why?

Thank you

From my side I cannot give you additional ideas of your particular issue, you should see your webhoook/function logs to debug the issue, is your function being triggered from your event?

Another way to control your devices is to use MQTT, you can find an example here using Particle devices. You just need to edit the callback function to turn off or on your led.

All the best.

1 Like

@jotathebest Thank you very much, i have tried the example you sent me, i did edit the callback function and i did get my device online and the variable also is showing online but when i toggle the switch in ubidots seems doing nothing, i can not see any change of the LED, could please have a look to my code below and help if i miss something.

// This #include statement was automatically added by the Particle IDE.
#include <UbidotsMQTT.h>



#ifndef TOKEN
#define TOKEN "..................."  // Add here your Ubidots TOKEN
#endif

#define VARIABLE_LABEL_TO_SUBSCRIBE "SWITCH_STATUS"
#define DEVICE_LABEL_TO_SUBSCRIBE ".........."

int led_switch = D7; //led1 is D7 
//Functions

void callback(char* topic, uint8_t* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println("payload obtained from server:");
  for (int i = 0; i < length; i++) {
    Serial.print(
        (char)payload[i]);  // prints the answer of the broker for debug purpose
  }
  
  // Toggle digital pin 7 depending on values received from ubidots server
  
  if ((char)payload[0]=='1'){
         
         digitalWrite(led_switch, HIGH);
         
    }
  else{
    digitalWrite(led_switch, LOW);
  } 


  Serial.println();
  
  
}

// Instances
 

UbidotsMQTT clientMQTT(TOKEN, callback);


void setup() {
  Serial.begin(115200);
 // client.ubidotsSetBroker("industrial.api.ubidots.com");

  clientMQTT.ubidotsSetDebug(true);
  pinMode(led_switch, OUTPUT);
  
  // Uncomment below line if you have an Ubidots for Education account
  // clientMQTT.ubidotsSetBroker("things.ubidots.com");
  //clientMQTT.ubidotsSetBroker("industrial.api.ubidots.com");
 //client.initialize();
  // Connect to broker.
  clientMQTT.connect(5);

  if (clientMQTT.isConnected()) {
    // 1st parameter is the device to subscribe and the 2nd is the variable label
    
    clientMQTT.ubidotsSubscribe(DEVICE_LABEL_TO_SUBSCRIBE,VARIABLE_LABEL_TO_SUBSCRIBE);
  }
}

void loop() {
  if (!clientMQTT.isConnected()) {
    clientMQTT.connect(5);

    clientMQTT.ubidotsSubscribe(DEVICE_LABEL_TO_SUBSCRIBE,VARIABLE_LABEL_TO_SUBSCRIBE);
  }
  

  clientMQTT.loop();
  delay(1000);
}

From the example of the docs, I tested the script below to turn on/off the embedded led of a photon and it seems to work properly, please give it a try


/****************************************
 * Include Libraries
 ****************************************/

#include <UbidotsMQTT.h>

/****************************************
 * Define Constants
 ****************************************/

#ifndef TOKEN
#define TOKEN "__"  // Add here your Ubidots TOKEN
#endif

#define VARIABLE_LABEL_TO_SUBSCRIBE "__"
#define DEVICE_LABEL_TO_SUBSCRIBE "__"

int led = D7;

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, uint8_t* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println("payload obtained from server:");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);  // prints the answer of the broker for debug purpose
  }
  // Some stuff to make with the payload obtained
  if ( (char)payload[i] == '1') {
        digitalWrite(led, HIGH);
    } else {
        digitalWrite(led, LOW);
    }

  Serial.println();
}

/****************************************
 * Instances
 ****************************************/

UbidotsMQTT clientMQTT(TOKEN, callback);

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  Serial.begin(115200);

  // Comment below line to disable debug messages.
  clientMQTT.ubidotsSetDebug(true);

  // Uncomment below line if you have an Ubidots for Education account
  // clientMQTT.ubidotsSetBroker("things.ubidots.com");

  // Connect to broker.
  clientMQTT.connect(5);

  if (clientMQTT.isConnected()) {
    // Insert as first parameter the device to subscribe and as second the
    // variable label
    clientMQTT.ubidotsSubscribe(DEVICE_LABEL_TO_SUBSCRIBE,
                                VARIABLE_LABEL_TO_SUBSCRIBE);
  }
  
  pinMode(led, OUTPUT);
}

void loop() {
  if (!clientMQTT.isConnected()) {
    clientMQTT.connect(5);
    // Insert as first parameter the device to subscribe and as second the
    // variable label
    clientMQTT.ubidotsSubscribe(DEVICE_LABEL_TO_SUBSCRIBE,
                                VARIABLE_LABEL_TO_SUBSCRIBE);
  }

  clientMQTT.loop();
  delay(5000);
}

All the best

1 Like

thank you very much @jotathebest. working fine :+1: