Using Spark.publish


#1

I want to use Spark.publish to publish a event every time the led (D7) on my Photon goes high and then low. Here is the existing code I am using to blink my led

void setup(){
  pinMode(D7, OUTPUT);
}

void loop(){
  digitalWrite(D7, HIGH);
  delay(1000);
  digitalWrite(D7, LOW);
  delay(1000);
}

Making an Led blink when a new event is published
#2

Something like this?!

void setup(){
pinMode(D7, OUTPUT);
}

void loop(){
digitalWrite(D7, HIGH);
Spark.publish("ledStatus", "HIGH");
delay(1000);
digitalWrite(D7, LOW);
Spark.publish("ledStatus", "LOW");
delay(1000);
}

where ledStatus is you event name and HIGH / LOW are the data. As easy as that!
For more info, check out the examples here: http://docs.particle.io/photon/firmware/#spark-publish

Hope this helps!

Happy Hacking!

Mohit


#3

Thanks!


#4

I want to make the blue led (D7) on the Photon blink when a new event called new-email is published.


#5

What have you tried so far?


#6

I am not really sure how to use Spark.subscribe. Here is my current code and I want to modify it to use Spark.subscribe.

void setup(){
  pinMode(D7, OUTPUT);
}

void loop(){
  digitalWrite(D7, HIGH);
  Spark.publish("ledStatus", "HIGH");
  delay(10);
  digitalWrite(D7, LOW);
  Spark.publish("ledStatus", "LOW");
  delay(300);
}

#7

Have a look at the docs and see if you can get that example working. Are you trying to do this with two devices, or on the same one?
Report back with your results, and we’ll go from there :smile:


#8

one but I am using iftt. So that when I get an email iftt publishes an event called new-email.


#9

That shouldn’t be a problem. If you take a look at the example in the docs, it should be a case of simply editing the eventName. After that, you can make changes for the LED to blink.
Give it a try, and report back :slight_smile:


#10

Like this

void setup(){
pinMode(D7, OUTPUT);
}

void loop(){
digitalWrite(D7, HIGH);
Spark.publish("ledStatus", "HIGH");
delay(10);
digitalWrite(D7, LOW);
Spark.publish("ledStatus", "LOW");
delay(300);
}
Spark.subscribe("new-email"); 

?


#11

I’m afraid that won’t work since it doesn’t comply with the syntax required, Spark.subscribe("eventName", myHandler);
Please do take a look over here: http://docs.particle.io/photon/firmware/#spark-subscribe
Then, copy that example, and change the eventname.


Also, the delays in your latest two code examples are WAY too short. They need to be a minimum of 1000, or you will be penalised for publishing too quickly. You can find the rules regarding that over here: http://docs.particle.io/photon/firmware/#spark-publish
Also, a delay of 10 isn’t even visible to the human eye, so that doesn’t make any sense, regardless of penalties.


#12

I tried this code and I got an error(s)

void setup(){
  pinMode(D7, OUTPUT);
}

void loop(){
  digitalWrite(D7, HIGH);
  Spark.publish("ledStatus", "HIGH");
  delay(1000);
  digitalWrite(D7, LOW);
  Spark.publish("ledStatus", "LOW");
  delay(30000);
}

#13

That code compiles without errors for me. Not that it was a surprise, considering you took the example code provided to you above, and just increased the length of the delays.
Some questions:

  • Do you know what the delays do?
  • Do you know how long those delays are?
  • Why do you need a delay greater than 1000?

And more importantly:

  • Have you read the docs regarding these subjects, i.e. publish/subscribe?

#14

Sorry I forgot to add Spark.subscribe(“new-email”, myHandler); at the bottom.


#15

Don’t bother, that wouldn’t have worked either.
Some simple steps:

  1. Go to the docs pages over here: http://docs.particle.io/photon/firmware/#spark-subscribe
  2. Read that whole part. Twice.
  3. Copy the example code from the docs. That’s the code on the right side of that page. Not the code from this topic.
  4. Paste said code in a new project.
  5. Change the eventName.
  6. Try to see if it compiles.
  7. Post that code over here.

If you encounter any problems while doing any of the above, return to step 1.


#16

Sorry When and event named new-email gets published I want the Photon to run this code.

void setup(){
  pinMode(D7, OUTPUT);
}

void loop(){
  digitalWrite(D7, HIGH);
  delay(10);
  digitalWrite(D7, LOW);
  delay(300);
}

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy


#17

Okay, at this point I have to ask: have you read any of my previous posts?
I have read yours, and I understand what you’re trying to do. I’m also giving you suggestions on how to achieve that. If you want me to write the code for you, then please, by all means say so, I’ll write it, and we’ll be done with it.
If you actually want to try something yourself, and maybe learn something, please, read my previous comments, and try the suggestions. They’re not that hard; it’s literally copy&paste.


#18

I have read your posts and read the docs but I got stuck.


#19

Have you tried doing this? Because I just tried that, and it works perfectly for me.