Particle.subscribe problem

Hi!,
when running the following code, i expect that when event “hook-response/wit-request-text” is generated, the function extractWITtext is invoked, so the first thing i should view when monitoring the serial port on my PC, is the message “Response from Wit for text input:”, but i just see “Requesting the meaning of a sentence to WIT.AI!”, despite that i checked on the Dashboar that the hook-response “hook-response/wit-request-text” is generated … could anybody help me to find which is the problem???
thaaaaaanks
best regards

#include "application.h"

// name the pins
#define BUTTONPIN D2
String myDeviceID;

// This routine runs only once upon reset
void setup()
{
  pinMode(BUTTONPIN, INPUT); 
  Serial.begin(115200);
  
  myDeviceID = System.deviceID();
  Spark.subscribe("hook-response/wit-request-text", extractWITtext, myDeviceID);
}

// This routine loops forever
void loop()
{
  if(digitalRead(BUTTONPIN) == 1) {                    
    Serial.println("Requesting the meaning of a sentence to WIT.AI!");
    Spark.publish("wit-request-text", "prueba-wit-text-1");
    // wait for 1 seg to avoid multiple requests when pushing button
    delay(1000);    
  }
}

void extractWITtext(const char * event, const char * data)
{
    Serial.println("Response from Wit for text input:");

    String str = String(data);
    String msg_id = extractstring(str, "msg_id", ",");
    if (msg_id != NULL) {
        Serial.println("msg_id: " + msg_id);
    }
}

String extractstring(String str, const char* start, const char* end) {
    if (str == NULL) {
        Serial.println("ERROR: No response received from Wit!");
        return NULL;
    }
    int startidx = str.indexOf(start);
    if (startidx < 0) {
        return NULL;
    }
    int endidx = str.indexOf(end);
    if (endidx < 0) {
        return NULL;
    }
    
    return str.substring(startidx + strlen(start), endidx);
}

Hmm! No, I would not expect the message from the handler to be printed before a actual event has been published.
The first Serial.print() statement that will be executed will be the one in loop().
Only after the Spark.publish() (which should be changed into Particle.publish() before your code breaks due to deprecating the ol’ Spark) a webhook will get triggered.

And with that I’m not too sure that you should subscribe as myDeviceID.
Try it without the third parameter or with MY_DEVICES but then you need to be consistent with that when publishing (PRIVATE vs. PUBLIC).


BTW:
When using a button you’d usually need some sort of pull-resistor (either external or internal by use of INPUT_PULLUP or INPUT_PULLDOWN).
And you might want to consider waiting for a button release before retriggering on a button hold.

Hi ScruffR … yes, you are right … i don’t expect to be printed the message “Response from Wit for text input:” first, but my problem is that it is not printed at all, even when i check that tue event is published and the response is sent from the hook … that is the problem i tried to explain. thanks for your help.

Hi BTW … i agree, it was just a coarse implementation, just to validate the webhook. thanks!

OK, then I guess it’s the part of my post that talks about myDeviceID-subscribe vs. PUBLIC-publish.

There is an open issue about some inconsistency between Core/Photon and different firmware versions.

Hi ScruffR! … that was the problem!. now it is solved. thanks for your help!!!
best regards

1 Like