Controlling Prodino WiFi board from ubidots


#1

Hello All,

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();
  }

#2

Greetings, the payload in the callback function just contains a raw number, and it seems that this method SetRelayState() receives not only a state but a relay number that you are actually not extracting. If you are gonna to turn on the relay number 1, this should be what you should write in your callback function:

if ((char)payload[0]=='1'){
  // Turns on
   KMPDinoWiFiESP.SetRelayState(1, 1);
  }
  else{
   // Turns off
    KMPDinoWiFiESP.SetRelayState(1, 0);
  }

All the best