Particle publish

I am publishing an event onto the console of my photon and that is a trigger for an IFTTT applet to send me a text when it is published. However I am publishing a variable once it gets over a certain amount, so then it publishes it non stop. Therefore texts me repeatedly. Is there a simple timer or something I could put into the code to only publish once or twice without having to do a counter of like 100? thanks

Show us your code. Are you simply checking the value every time through your loop?

1 Like

In psuedo code below; check if it crosses the threshold value and if publishing is allowed, if so then publish. Then disallow publishing until it goes below the threshold value again (+ an offset to make sure it doesn’t jump around the threshold value and keeps publishing).

That’s the basic idea, though there are many other ways you could solve this.

bool AllowPublish = true;
int threshold = 2000;

void loop(){
  if (value < threshold - 200)
    AllowPublish = true;

  if (value > threshold && AllowPublish == true){
    Particle.publish("stuff", data, PRIVATE);
    AllowPublish = false;
  }
}
1 Like

I simply want to publish that the “panfull” only once or twice a day

We don’t like looking a screenshots of code since it is a strain on the eyes and doesn’t allow us to just grab the code and copy/paste it into Web IDE for quick validity checks.

If you have it in Web IDE you should provide a SHARE THIS REVISION button and post a link.

However, we are usually enablers and don’t see ourselves as spoon feeders of ready-meal-solutions.

2 Likes

https://go.particle.io/shared_apps/5bef0c94dc06bd9564000688

Some notes

  • you have multiple includes of the same files - that’s messy
  • your Particle.subscribe() instructions with the device ID won’t work as expected, you should use MY_DEVICES instead of the device ID
  • are you actually using the Tinker functions in your project - if not remove the overhead
  • when you are subscribing with MY_DEVICES you need to publish PRIVATE
  • the post by @Moors7 above provides valuable pointers what you could do instead of the block following if just publised, you just want to limit the resetting of AllowPublish = true to once or twice a day (e.g. by use of uint32_t lastPublish = millis(); which you use as a timeout flag - if (millis() - lastPublish > 43200))
1 Like

Thank you. Your suggestions are much appreciated, and again thank you for dealing with my ignorance to the community.

https://go.particle.io/shared_apps/5bfd6d4b7e9526a5ac001402

I am still trying to publish the event “panfull” only one time. I swear I had it working last week then I guess something changed. Could someone look over my code. Thanks

It appears you are performing the analogRead(photocell) and it’s Publish (when >2400) every trip through loop(), which can be ~ 1000 times each second.

You can use the same technique ScruffR showed you for the analogRead as for ThingSpeak:
if (millis() - lastPublish > {X} ))
ie:
if (millis() - lastAnalogRead > {Y} ))

How often do you want to check the photocell?
How often do you want to be alerted when analogRead(photocell) > 2400 ?
How often do you want to log the Temperature and Humidity Values to ThingSpeak ?

The frequency of each of those 3 things can easily be defined with (3) different uint32_t as above.

2 Likes

Okay, thank you. I want to read the values to Thinkspeak once a minute. I want to publish if photocell is > 2400 once every 12 hours. Frequency of reading the photocell can be anything I think. But I am not too familiar with the function you mentioned at the end. Thank you