Cloud Communication - Publish
Cloud Communication
Events Part 1: Publishing Events
- Events are messages sent from a Photon 2 to the cloud as often as you choose
- Events are private (viewable by only by you and devices in your account)
- Events can be accessed in Particle console, app, or by other devices (subscribing)
Events in Particle Console
Publish Syntax
Particle.publish(<<EVENT_NAME>>, <<EVENT_VALUE>>);
Example
Particle.publish("lightValue", "bright");
Particle.publish("tempFahr", String(85.9));
- Names and values must always be strings
- Should only publish 1 event / sec (or burst of 4 events in 1 sec)
Publishing on Device Startup
- With threading enabled,
SYSTEM_THREAD(ENABLED);
setup()
will run before cloud connectivity is enabled, meaningParticle.publish()
will fail -
If you need to execute code only once when the device starts, create a boolean flag and check to make sure the Photon 2 has connected to the cloud (
Particle.connected()
) before running the code once. -
For example
bool runOnce = true; void loop() { if (runOnce == true && Particle.connected() == true) { runOnce = false; // put Particle.publish(...) statement here }
-
In order to get the weather initially when the Photon 2 turns on, we need to set a boolean flag and do a check in
loop()