Hello everyone, i have issues to subscribe events sent from an application android, the events are actually published but i can’t recuperate them on my photon … i have already used the function subscribe and it worked if the events published by my own photon or from another photon, but it didn’t worked if the events are published from the app …
Please help me if there any solution ! thx
Are the events using compatible scopes PRIVATE
goes with MY_DEVICES
while PUBLIC
goes with ALL_DEVICES
(the latter two are the defaults).
When using PRIVATE
both ends must be owned by the same account.
For Product devices additional requirements exist ontop of that.
Thank you for your reply, actuary i used the instruction ALL_DEVICES with the function subscribe, and it didn’t work, if i try to publish once more these events i get on events the word “null” in the name and the data both : this is my code
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Particle.subscribe("com", myHandler,ALL_DEVICES);
Particle.subscribe("commande", myHandler,ALL_DEVICES);
}
void loop()
{ }
int i = 0;
void myHandler(const char *event, const char *data)
{
if ((String)data =="avancer")
{
Serial1.write(_avancer);
}
else if ((String)data =="reculer")
{
Serial1.write(_reculer);
}
else if ((String)data =="droite")
{
Serial1.write(_droite);
}
else if ((String)data =="gauche")
{
Serial1.write(_gauche);
}
else if ((String)data =="arreter")
{ Particle.publish("AL 7AMDO LILLALLAH","ISLAM");
Serial1.write(_arreter);
}
}
Thank you again …
I guess this is not your entire code - you are using variables that are defined nowhere.
You are subscribing to com
and commande
since both terms start with com
the second subscription is superfluous due to this fact
https://docs.particle.io/reference/device-os/firmware/photon/#particle-subscribe-
We also don’t see how you are publishing the respective events.
Instead of using (String)data == "xxxx"
you can/should use strcmp(data, "xxxx") == 0
and the common indentation scheme for your else if()
branches would not accumulate indentations.
If you add a final else
branch that just does a Serial.printlnf("%s: <%s>", event, data);
you could check whether the handler gets called at all and what you actually receive.
Having sait that, I’d rewrite your handler like this
void myHandler(const char *event, const char *data)
{
if (!strcmp(data, "arreter")) {
Particle.publish("AL 7AMDO LILLALLAH","ISLAM");
Serial1.write(_arreter);
}
else if (!strcmp(data, "avancer"))
Serial1.write(_avancer);
else if (!strcmp(data, "reculer"))
Serial1.write(_reculer);
else if (!strcmp(data, "droite"))
Serial1.write(_droite);
else if (!strcmp(data, "gauche"))
Serial1.write(_gauche);
else
Serial.printlnf("Unknown option <%s>: <%s>", event, data);
}
(actually I’d rather use a for()
loop and an array of compare terms and variables to print)
Thank you mate, i have used your code this time, it didn’t work again … still i got the event published from the app, but the photon doesn’t read it, even by publishing “arreter”, Particle.publish(“AL 7AMDO LILLALLAH”,“ISLAM”); doesn’t work, i get noting back
I used this code this time :
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Particle.subscribe("commande", myHandler,ALL_DEVICES);
}
void loop()
{ }
int i = 0;
void myHandler(const char *event, const char *data)
{
if (!strcmp(data, "arreter")) {
Particle.publish("AL 7AMDO LILLALLAH","ISLAM");
Serial1.write(_arreter);
}
else if (!strcmp(data, "avancer"))
{Serial1.write(_avancer);}
else if (!strcmp(data, "reculer"))
{Serial1.write(_reculer);}
else if (!strcmp(data, "droite"))
{Serial1.write(_droite);}
else if (!strcmp(data, "gauche"))
{Serial1.write(_gauche);}
else
Serial.printlnf("Unknown option <%s>: <%s>", event, data);
}
Do you get anything on USB Serial?
But you still haven’t got any of your _XXXXX
variables defined in your code, so I’m not convinced you are actually running that code. Without these variables declared your code won’t even build, let alone flash.
For testing I’d also add this to loop()
void loop() {
static uint32_t ms = 0;
if (millis() - ms < 5000) return;
ms = millis();
Serial.println("test event sent");
Particle.publish("commande", "test", PUBLIC);
}
BTW, have you selected PUBLIC when you sent your test events?
However, PRIVATE/MY_DEVICES is the recommended combination.
I think you misunderstand me somewhere, i’am sending the events (avancer, reculer, …) from my app android, and i actually receiving theme in the Cloud ( NAME : commande, DATA : arreter, avancer …, DEVICE : api ) now i want to subscribe this event on my photon, but the photon doesn’t read it, if i sent the same event from another photon my photon read it so i can build conditions based on it, for exmple :
if the cloud receive “avancer” my photon publish 1
(this is works only if the event is sent by photon, not by my app)
I don't thinks so. Even when you send from your Android App you need to state the scope for the event you want to send.
But you may not have followed my line of thinking here.
When you want to test your device application then you should exclude any other potential cause for the issue you are seeing and one way of doing that is to remove your application (and it's potential contributions to the issue) from the equation.
This exactly was my point, it's obviously not the device application but your Android App either using the wrong scope or targeting the wrong account.
Sorry for bothering you, i didn’t get you … i mean the event is really published on my own account like it appears on the photo that i have already sent ( one click on the button “avancer” on the app, i get this event on my Particle Cloud)
To be clear i send you this code and it works perfectly :
as you can see, it controle the photon’s led according to the event received (“ON”), this event is published on the photon from another photon
furthermore adding ALL_DEVICES in subscribe function, how can edit this code to do the same thing but this time the event (“ON”) is sent from my app, because in this case despite the event is recieved the photon doesn’t read it
Can you just try Particle Console | Build your connected product
and see what happens?
Also try incorporating my suggested loop()
code.
These suggestions should help locate the issue. Diverting the discussion to some other code on an other device is hardly helpful here.
BTW, your Particle.subscribe("canalMTI", envoi_data)
is exactly tha same as Particle.subscribe("canalMTI", envoi_data, ALL_DEVICES)
- that's what I originally indicated by saying this
Thank you so much
What’s the outcome of these tests?
How are you publishing the event from your Android App?