Particle.subscribe

Has anyone got this implementation working? Other than the VOID issue I can never get the handler to actually get called.

class Subscriber {
  public:
    void subscribe() {
      Particle.subscribe("some_webhook", &Subscriber::handler, this);
    }
    void handler(const char *eventName, const char *data) {
      Serial.println(data);
    }
};

Subscriber mySubscriber;
// nothing else needed in setup() or loop()

I think there are two reasons this doesn’t work.

If the intention is to subscribe during the constructor, the constructor is not declared properly.

But even if you fixed that, it still wouldn’t work properly because global C++ objects are instantiated too early. It actually happens before setup(), before you can call Particle.subscribe() successfully.

You’re better off making a setup() method that does the subscribe, and calling that from setup().

I would have thought the same but no matter how I code it if I place Particle.subscribe in the C++ class it fails to call the handlers and along with that you can’t declare the class handler in the subscribe call.

The only way I can get this to work is to create functions in the “ino” file and then call the handler in my C++ class.

I’m not sure why it’s not working for you. This is what I tested and works:

sublibtest.cpp (my main program):

#include "Particle.h"
#include "sublib/sublib.h"

SubLib subLib;

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

	subLib.setup();
}

void loop() {

}

sublib/sublib.h, the library header file

#ifndef __SUBLIB_H
#define __SUBLIB_H

class SubLib {
public:
	SubLib();
	virtual ~SubLib();

	void setup(void);

private:
	void handler(const char *eventName, const char *data);
};

#endif /*__SUBLIB_H */

sublib/sublib.cpp, the library implementation file:

#include "Particle.h"
#include "sublib.h"


SubLib::SubLib() {
}

SubLib::~SubLib() {

}

void SubLib::setup(void) {
	Particle.subscribe("sublibTest", &SubLib::handler, this);
}


void SubLib::handler(const char *eventName, const char *data) {
	Serial.printlnf("got handler eventName=%s data=%s", eventName, (data != NULL) ? data : "NULL");
}

The command I used to generate the event:

$ particle publish sublibTest hello
posted event!

And what I saw in the serial monitor:

$ particle serial monitor
got handler eventName=sublibTest data=hello

Very strange… Here is what I have and it will not compile for some reason. I am using ATOM but have tried both in the Particle Build environment and ATOM. It’s probably something stupid but I am pulling my hair out trying to figure it out.

// testwebhook.ino file
#include "Particle.h"
#include "weather.h"

Weather myWeather;

void setup() {
    Serial.begin(9600);  
    myWeather.setup();
}

void loop() {
}
// End testwebhook.ino file

// Weather.cpp File
#include "Particle.h"
#include "weather.h"

Weather::Weather()
{
}
Weather::~Weather()
{
}
void Weather::setup(void)
{
  Serial.println("subscribe to hook");
  Particle.subscribe("hook-response/w2day_hook", &Weather::v2DayHandler, this);
}

void Weather::v2DayHandler(const char *name, const char *data)
{
  Serial.println("Made to the handler");
}
// End Weather.cpp File

// Weather.h File
#ifndef _weather_h
#define _weather_h
#include "Particle.h"

class Weather
{
  public:
    Weather();
	virtual ~Weather();
    void setup(void);
    
  private:
    void v2DayHanlder(const char *name, const char *data);
};

#endif //_weather_h
// End Weather.h File