Trying to understand how to use particle.publish

I am trying to understand how to get pin data from Particle to a web page using particle.publish. When the pin is HIGH, I want the variable “result” to contain the data “open”, and so forth. However, when I run “particle list” from the cli, the device is listed but no variables, so I assume something is wrong with my particle.publish statement. Any help getting me started is greatly appreciated.

int pin = D0 ;

void setup()
{        pinMode(pin, INPUT);    }

void loop()

if (digitalRead(pin) == HIGH) 
{ Particle.publish("result","open"); }

if (digitalRead(pin) == LOW) 
{ Particle.publish("result","closed"); }

{ Particle.publish("result","something is wrong"); }


Particle.publish() does not “create” a Particle.variable(). It just publishes an event that can be Particle.subscribe()d to or subscribed by any other means like JS.

If you want a requestable variable you need to do this

int pin = D0;
char state[16];

void setup() {
  Particle.variable("pinState", state);
  pinMode(pin, INPUT);    

void loop() {
  sprintf(state, "%s", digitalRead(pin) ? "open" : "closed"); 

There’s nothing wrong with your Particle.publish statements (see note below), but as @ScruffR said, they don’t create a Particle.variable, so you can’t see them in the CLI that way. If you want to see the results of your publishes in the CLI, type in, particle subscribe mine . This will allow you to see anything published from any of your devices. You could also type in particle subscribe result mine if you only want to see events published with the name “result” (see the docs here for the CLI variations). You can also go to the Particle console, and see your events published there.

Note: You shouldn’t publish more than once per second (except for short bursts), so you need to put a delay in your loop to make sure you don’t exceed that limit.

To expand on the logic of the original post.
The conditions don’t make a lot of sense.

Since digitalRead() will never ever have any other result than HIGH or LOW the else branch would not make any sense at all, but since the two if() statements are not linked correctly this else branch will even produce wrong results as it will always report a problem when the pin is HIGH

That was great help. Now that I can see the contents of a variable using “particle get” from the CLI, is there a simple means to access the contents of the variable using HTML?

Unless I misunderstand, the particle API can work with REST. So I tried the following statement in my HTML.

<script> get "" </script> <input type="text" name="var_name">

Am I headed down the wrong path or is something else wrong?


There already is a tutorial for that


I struggled with very similar code most of the day yesterday only to discover the problem wasn’t with my code. Every time I did a digital read the result was coming back HIGH… no matter what. I was starting to think I’d fried my chip… then I added a 10 kohm pull down resistor and my problems were solved! Just because the circuit between 3.3 volts and the chip is open doesn’t mean you’re going to get a “LOW” from the digital read. If there’s any stray voltage on the pin it’s not going to switch. If you run a high value resistor (like 10 kohms) from the input pin to ground then it drains that stray voltage away and you get the result you’re looking for.

Hope this helps

You can also set the pinMode to INPUT_PULLDOWN, so you don’t need an external resistor (usually). The internal resistor is around 40k, and sometimes you need a stronger pulldown than that, but the internal one has always worked for me.

1 Like

I noticed INPUT_PULLDOWN as one of the options in the pinmode command online documentation, but there was no discussion of its intended use. The name implies it has something to do with the pulldown resistor… but no documentation that I could (quickly) find. I’ll give it a try… one less component to deal with.