Publishing Millis

WGuys,

I am really really new to Particle and very rusty with my Arduino programming (several years). So I am starting a small project, and adding slowly to it, trying to teach myself again.

The problem I am having is with the Particle.Publish command and the Millis() command.

I will post my code below.

Basically, I want to FIRST start by just “publishing” a time event to the particle cloud that correlates to the PIN going HIGH or LOW.

As you see below, I have a publish event shortly after the PIN change.

I keep getting the error: “invalid conversion from ‘long unsigned int’ to ‘const char*’ [-fpermissive]”

I googled for quite a while and I don’t know the “best” way to fix what I am trying to do.

unsigned long DOT; //Door Opened Time
unsigned long DCT;//Door Closed Time

void setup() {
    pinMode(D0, INPUT);
    Particle.publish("System","System Started");
}

void loop() {
   if (digitalRead(D0) == LOW) {
       delay(1000);
       DOT = millis();
       Particle.publish("Dog Food Bin","Door Opened");
       Particle.publish("Door Opened", DOT);
        while (digitalRead(D0) == LOW){
            delay(1000);
        }
   }
    else if (digitalRead(D0) == HIGH) {
       delay(1000);
       DCT = millis();
       Particle.publish("Dog Food Bin","Door Closed");
       Particle.publish("Door Closed", DCT);
        while (digitalRead(D0) == HIGH){
            delay(1000);
        }
    }
}

Gutted by

This tells you exactly what the problem is: You are trying to provide a long unsigned int as parameter where only const char* is allowed.
If you look at the docs for Particle.publish() you’ll see that the data parameter has to be a string and not a numeric type.

BTW, when using a button or switch you must make sure your input pin never floats.
For that you’d need a pull-up/-down resistor opposite to the level your button/switch is closing to.
INPUT_PULLUP or INPUT_PULLDOWN would be the options to apply internal pull-resistors.

I’d also advise to incorporate your two publishes into one single event and please use the PRIVATE modifier to have these events confined to your own account and not the publich event stream.

One way to do that would be

  int  btn = digitalRead(D0);
  char data[64];

  snprintf(data, sizeof(data), "Door %s (%lu)", btn ? "closed" : "open", btn ? DCT : DOT);
  Particle.publish("DogFoodBin", data, PRIVATE);

snprintf(data, sizeof(data), “Door %s (%lu)”, btn ? “closed” : “open”, btn ? DCT : DOT);

This is about 9 levels over my head. But I will hit up the google to figure it out.

The code I wrote worked just fine, before trying to incorporate a time stamp using millis().

The end result is to actually subtract the DOT from the DCT and produce a TOT “Total Open Time”. Which would be published.

I would really like to elaborate on the code I already have, working my way to what you provided which looks more complex but in the long run, simpler!! :wink:

I am a slow learner and need to learn the hard way.

The main point was: You can’t publish a numeric datatype (e.g. unsigned long) direct, you need to convert it into a string first.
And that is what this complicated command did (amongst other things).

If you only want the conversion you could do one of the following

  Particle.publish("DogFoodBin", String(DCT), PRIVATE);  // the easiest, but bears some risks
  // or
  char data[16];
  itoa(DCT, data, 10);                       // integer to ASCII with base of 10
  Particle.publish("DogFoodBin", data, PRIVATE);    
  // or 
  char data[16];                             // a bit much for only one variable, but most powerful 
  snprintf(data, sizeof(data), "%lu", DCT);  // create formatted string with variables
  Particle.publish("DogFoodBin", data, PRIVATE);    

http://www.cplusplus.com/reference/cstdlib/itoa/
http://www.cplusplus.com/reference/cstdio/snprintf/
With the latter you could get more data into one publish

  char data[64];   
  snprintf(data, sizeof(data), "%lu - %lu = %ld", DCT, DOT, DCT - DOT);  
  Particle.publish("DogFoodBin", data, PRIVATE);