Lavanya
October 24, 2019, 11:21am
1
Hi All,
I want to control relay from ubidots dashboard. I created variable and assigned a switch widget to it. I uploaded following code in NodeMCU.
I do 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 relay contacts, could anyone help on this context.
subscribe-ubidots.ino (1.9 KB)
Hi there, add this line to your setup() function
client.ubidotsSetBroker("industrial.api.ubidots.com");
For more information, please refer to the library docs .
All the best
Lavanya
November 13, 2019, 10:14am
4
Thanks jota, it really worked.
Now Im testing Prodino WiFi board. https://www.tindie.com/products/kmpelectronics/prodino-wifi/
I tested the board using code available in ubidots webpage https://help.ubidots.com/en/articles/2118658-connect-an-esp8266-stand-alone-to-ubidots-over-mqtt
It works. Im able to get my device online and publishing some analog values. Now I want to control the relays from ubidots. I wrote the code but it is not controlling the relay. Please help me out. The code I used:
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
#include <KMPDinoWiFiESP.h>
#include <KMPCommon.h>
/****************************************
* Define Constants
****************************************/
#define TOKEN "BBFF-qrPikZay9mA75c1S3VIDcakxdDCpb3" // Your Ubidots TOKEN
#define WIFINAME "*****" //Your SSID
#define WIFIPASS "*******" // Your Wifi Pass
#define DEVICE_LABEL "my-new-device" // Put here your Ubidots device label
#define VARIABLE_LABEL "relay" // Put here your Ubidots variable label
const char* TOPIC_INFO = "/KMP/ProDinoWiFi/Info";
const char* TOPIC_COMMAND = "/KMP/ProDinoWiFi/Cmd";
const char* CMD_REL = "rel";
Ubidots client(TOKEN);
int status;
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
if (strcmp(topic,"/v1.6/devices/my-new-device/relay/lv")==0){
if ((char)payload[0]=='1'){
KMPDinoWiFiESP.SetRelayState(CharToInt(payload[4]), CharToInt(payload[6]) == 1);
// KMPDinoWiFiESP.SetRelayState(Relay1, status == 1);
}
else{
KMPDinoWiFiESP.SetRelayState(CharToInt(payload[4]), CharToInt(payload[6]) == 0);
// KMPDinoWiFiESP.SetRelayState(Relay1, status == 0);
}
Serial.println();
}}
/*void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
// Check topic.
if (strcmp(topic,"/v1.6/devices/my-new-device/relay/lv")==0){
Serial.println("Payload:");
for (uint i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
// Command structure: [command (rel):relay number (0..3):relay state (0 - Off, 1 - On)]. Example: rel:0:0
size_t cmdRelLen = strlen(CMD_REL);
if (strncmp(CMD_REL, (const char*)payload, cmdRelLen) == 0 && length >= cmdRelLen + 4)
{
KMPDinoWiFiESP.SetRelayState(CharToInt(payload[4]), CharToInt(payload[6]) == 1);
}
else
{
KMPDinoWiFiESP.SetRelayState(CharToInt(payload[4]), CharToInt(payload[6]) == 0);
}
}}*/
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
client.ubidotsSetBroker("industrial.api.ubidots.com");// Sets the broker properly for the business account
client.setDebug(true); // Pass a true or false bool value to activate debug messages
Serial.begin(115200);
KMPDinoWiFiESP.init();
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
}
client.loop();
}
Serial Monitor Output:
Hey @Lavanya ,
You’ve probably already managed to control the relay using Ubidots. However, if you haven’t, the code below can serve as a reference to help you do so:
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
if ((char)payload[0]=='1'){ // Turn the relay ON
digitalWrite(16, HIGH);
}
else{ // Turn the relay OFF
digitalWrite(16, LOW);
}
Serial.println();
}
Full code here .
Cheers,
Maria H.