The use of the ESP8266 in the world of IoT

User avatar
By Artur Gaspar
#31033 Good morning , I'm trying to put together a test to flash a lamp atravéz cell using IFTTT .
Entr the IFTTT and DeviceHub everything is ok.
The program running on ESP8266 returns me also everything is ok.
But the CALLBACK function is not called .
Does anyone know what could be the problem ?

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "4UNION";
const char* password = "";

#define API_KEY "bcf081da-32a1-42c6-a9c1-ba7068005e7d";
#define PROJECT_ID 5621;
#define DEVICE_UUID "108f8baa-ec02-434b-9227-597966a053b2";
#define ACTUATOR1_NAME "Rele1"

#define sec 1000

char clientId[]          = "Teste";
char actuator1Topic[]     = "/a/bcf081da-32a1-42c6-a9c1-ba7068005e7d/p/5621/d/108f8baa-ec02-434b-9227-597966a053b2actuator/Rele1/state";
char server[]            = "mqtt.devicehub.net";
char message_buffer[150];


WiFiClient Client;

PubSubClient client(server, 1883, callback, Client);


void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(server, 1883);
  client.setCallback(callback);

  if (client.connect(clientId)) {
    //add or remove based on the number of actuators
    if (client.subscribe(actuator1Topic)) {
      Serial.println("Ok to subscrib");
    } else {
      Serial.println("Erro no subscrib");
    }

    Serial.println("Successfuly connected and running!");
  } else {
    Serial.println("Connection problem");
  }
}

void loop(void)
{
  //if client it's not connected or disconnects here we try to reconnect
  if (!client.connected()) {
    Serial.println("reconnecting ...");
    client.connect(clientId);
    delay(3 * sec);
    client.subscribe(actuator1Topic);
  }
  // MQTT client loop processing
  client.loop();
  if (client.loop()) {
    //Serial.println(client.state());
  } else {
    Serial.println("NAO Conectado");
  }

}

// handles message arrived on subscribed topic
void callback(char* topic, byte* payload, unsigned int length)
{
  Serial.println("callback");
  // handles extracting the actuator value from payload
  int i = 0;
  int j = 0;
  int k = 0;
  for (i = 0; i < length; i++) {
    message_buffer[i] = payload[i];
  }
  message_buffer[i] = '\0';
  for (i = 0; i < length; i++) {
    if (!(strncmp(&message_buffer[i], "s", 1) || strncmp(&message_buffer[i + 3], "t", 1))) {
      break;
    }
  }
  for (j = i + 9; j < length; j++) {
    if (!(strncmp(&message_buffer[j], "\"", 1))) {
      break;
    }
  }
  String string_actuator_value = "";
  for (k = i + 9; k < j; k++) {
    string_actuator_value += message_buffer[k];
  }
  int actuator_value = string_actuator_value.toInt();

  // here is the value from the actuator
  Serial.print(topic);
  Serial.print(" value: ");
  Serial.println(actuator_value);
  /*
  // you can use it here accordingly just add or remove based on how manny actuators you have
  if(String(topic) == String(actuator1Topic)){
    //DO actuator1 action
  }else if ((String(topic) == String(actuator2Topic))){
    //DO actuator2 action
  }else if ((String(topic) == String(actuator3Topic))){
    //DO actuator3 action
  }
  */
}


void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
User avatar
By Artur Gaspar
#31036 Found!
The correct code:
Code: Select allchar actuator1Topic[]     = "/a/bcf081da-32a1-42c6-a9c1-ba7068005e7d/p/5621/d/108f8baa-ec02-434b-9227-597966a053b2/actuator/Rele1/state";


Now partially working.
Every change in the status CALLBACK function is called correctly. But unfortunately the value is always to be 0. With the actuator being turned on or off in DeviceHub .
Now you need me to read that correct value for this to work .
User avatar
By Artur Gaspar
#31241 Update.
If I enter the site (dashboard.devicehub.net) and directly change the state it correctly reflects the value in the serial output . Button returns 1 for on and 0 for off.
But if you use the Button 's IFTTT the result is always 0 in serial output , but in dashbord shows the button on and off correctly.
User avatar
By Artur Gaspar
#31256 I got some result but not the way he wanted.
I wanted a single button that changes the state of the actuator in DeviceHub .
The code would look like in IFTTT :
Code: Select all{ "state" : " Toggle " , " apiKey " : ....

But always returns to zero while the dashboard is correct .
Created now two buttons .
One with :
Code: Select all { "state" , " 1", " apiKey " : ....

And another :
Code: Select all{ "state" : "0" , " apiKey " : ....

Now get the correct values ​​in the serial output .