Using Spark.publish

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);
}

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

1 Like

Thanks!

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

What have you tried so far?

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);
}

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:

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

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:

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"); 

?

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.

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);
}

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?

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

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.

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

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.

1 Like

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

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

1 Like