Photon shows offline but still publishing events

Hi guys,

Im recently new to photons and have a slight issue, i have a photon that flashes fine but after 60 seconds shows offline, this is set to subscribe to a topic and react by turning on leds depending on the topic message handled by a handler, i also have another 2 photons publishing their button event, this always works even when the photon shows offline, if i disconnect the 2 publishing photons the subscribing photon comes back online, I’ve flashed tinker to the subscriber and it stays online so nothing wrong with the photons, My problem is if i need to re-flash an update when it’s in situ I’m buggered, Ive narrowed it down to the subscribe command, if i remove that it stays online forever.

Ive even tried flashing the on board LED to try to keep the photon awake if it was sleeping.

Any ideas?

int led0 = D0;
int led1 = D1;
int led2 = D2;
int led3 = D3;
int led4 = D4;
int led5 = D5;
int led6 = D6;
int led7 = A0;
int led8 = A1;
int led9 = D7;

void setup() {
  pinMode(led0,OUTPUT); 
  pinMode(led1,OUTPUT); 
  pinMode(led2,OUTPUT); 
  pinMode(led3,OUTPUT); 
  pinMode(led4,OUTPUT); 
  pinMode(led5,OUTPUT); 
  pinMode(led6,OUTPUT); 
  pinMode(led7,OUTPUT); 
  pinMode(led8,OUTPUT); 
  pinMode(led9,OUTPUT); 

  Particle.subscribe("mtla", myHandler);
 
  digitalWrite(led0,HIGH);
  delay(200);
  digitalWrite(led1,HIGH);
  delay(200);
  digitalWrite(led2,HIGH);
  delay(200);
  digitalWrite(led3,HIGH);
  delay(200);
  digitalWrite(led4,HIGH);
  delay(200);
  digitalWrite(led5,HIGH);
  delay(200);
  digitalWrite(led6,HIGH);
  delay(200);
  digitalWrite(led7,HIGH);
  delay(200);
  digitalWrite(led8,HIGH);
  delay(200);
  digitalWrite(led9,HIGH);
  delay(800);
  digitalWrite(led0,LOW);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
  digitalWrite(led5,LOW);
  digitalWrite(led6,LOW);
  digitalWrite(led7,LOW);
  digitalWrite(led8,LOW);
  digitalWrite(led9,LOW);
  digitalWrite(led0,HIGH);
  delay(200);
  digitalWrite(led1,HIGH);
  delay(200);
  digitalWrite(led2,HIGH);
  delay(200);
  digitalWrite(led3,HIGH);
  delay(200);
  digitalWrite(led4,HIGH);
  delay(200);
  digitalWrite(led5,HIGH);
  delay(200);
  digitalWrite(led6,HIGH);
  delay(200);
  digitalWrite(led7,HIGH);
  delay(200);
  digitalWrite(led8,HIGH);
  delay(200);
  digitalWrite(led9,HIGH);
  delay(800);
  digitalWrite(led0,LOW);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
  digitalWrite(led5,LOW);
  digitalWrite(led6,LOW);
  digitalWrite(led7,LOW);
  digitalWrite(led8,LOW);
  digitalWrite(led9,LOW);
  delay(800);
  digitalWrite(led0,HIGH);
  digitalWrite(led1,HIGH);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,HIGH);
  digitalWrite(led5,HIGH);
  digitalWrite(led6,HIGH);
  digitalWrite(led7,HIGH);
  digitalWrite(led8,HIGH);
  digitalWrite(led9,HIGH);
  delay(800);
  digitalWrite(led0,LOW);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
  digitalWrite(led5,LOW);
  digitalWrite(led6,LOW);
  digitalWrite(led7,LOW);
  digitalWrite(led8,LOW);
  digitalWrite(led9,LOW);
  delay(800);
  digitalWrite(led0,HIGH);
  digitalWrite(led1,HIGH);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,HIGH);
  digitalWrite(led5,HIGH);
  digitalWrite(led6,HIGH);
  digitalWrite(led7,HIGH);
  digitalWrite(led8,HIGH);
  digitalWrite(led9,HIGH);
  delay(800);
  digitalWrite(led0,LOW);
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
  digitalWrite(led5,LOW);
  digitalWrite(led6,LOW);
  digitalWrite(led7,LOW);
  digitalWrite(led8,LOW);
  digitalWrite(led9,LOW);

}


void loop() {

  delay(200);
  digitalWrite(led9,HIGH);
  delay(200);
  digitalWrite(led9,LOW);
}

void myHandler(const char *event, const char *data)
{


  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l1off")==0) {
    
  digitalWrite(led0,LOW);
  }
  else if (strcmp(data,"l1on")==0) {
    digitalWrite(led0,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l2off")==0) {
    
  digitalWrite(led1,LOW);
  }
  else if (strcmp(data,"l2on")==0) {
    digitalWrite(led1,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l3off")==0) {
    
  digitalWrite(led2,LOW);
  }
  else if (strcmp(data,"l3on")==0) {
    digitalWrite(led2,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l4off")==0) {
    
  digitalWrite(led3,LOW);
  }
  else if (strcmp(data,"l4on")==0) {
    digitalWrite(led3,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l5off")==0) {
  digitalWrite(led4,LOW);
  }
   else if (strcmp(data,"l5on")==0) {
    digitalWrite(led4,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l6off")==0) {
    
  digitalWrite(led5,LOW);
  }
  else if (strcmp(data,"l6on")==0) {
    digitalWrite(led5,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l7off")==0) {
    
  digitalWrite(led6,LOW);
  }
  else if (strcmp(data,"l7on")==0) {
    digitalWrite(led6,HIGH);  
  }
  else {
  }}
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"l8off")==0) {
    
  digitalWrite(led7,LOW);
  }
  else if (strcmp(data,"l8on")==0) {
    digitalWrite(led7,HIGH);  
  }
  else {
  }}
  
  if (strcmp(event,"mtla")==0) {
      
    if (strcmp(data,"eoff")==0) {
    
  digitalWrite(led8,LOW);
  }
  else if (strcmp(data,"eon")==0) {
    digitalWrite(led8,HIGH);  
  }
  else {
  }}
}

Where does it show offline? The RGB LED on the device or in console or somewhere else?
Could it be that your publishers overwhelm the receiver?
Showing the publisher code too might help.

Your code could do with a good amount of cleanup :wink:
Have you considered using an array for your pin/led assignments?
Have you got an option to adapt your event messages to make it more IoT friendly/machine readable?
Your events should not use the default settings PUBLIC/ALL_DEVICES but rather PRIVATE/MY_DEVICES.
You repeatedly use if (strcmp(even, "mtla") == 0) why not make that a single, overarching condition?
Make your loop() non-blocking
e.g. like this

void loop() {
  static uint32_t msBlink = 0;
  if (millis() - msBlink < 200) return;
  msBlink = millis();
  digitalWrite(led9, !digitalRead(led9));
}
1 Like

ScruffR, thanks for your reply, sorry when i say new im a couple of weeks into it, just to complete a project at work then ill tidy the code and add arrays ETC,

OK so the offline shows below (MaterialsDesk1)

image

and below you see it go offline

And the publisher as below

int callmat = D0; 
int boardLed = D7;

void setup() {
  pinMode(boardLed,OUTPUT); 

  digitalWrite(boardLed,HIGH);
  delay(2000);

  digitalWrite(boardLed,LOW);
  delay(500);

  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);


  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);



}



void loop() {
 

  if (digitalRead(callmat) == HIGH) {

   
        Particle.publish("mtla","l5on");
        digitalWrite(boardLed,HIGH);
        delay(500);

    
  }else{
          Particle.publish("mtla","l5off");
        digitalWrite(boardLed,LOW);
        delay(500);
  }
        delay(5000);

}

You are not setting the pin mode for callmat and without that there won’t be any internal pull-resistors applied. Do you have an external pull-up/-down wired or what is the signal source?

Just a quick hint about concise coding

void loop() {
  int state = digitalRead(callmat);
  Particle.publish("mtla", state ? "l5on" : "l5off");
  digitalWrite(boardLed, state);
  delay(5500);
}

(this does basically the same thing as your loop() - but that blocks for 5.5 seconds and keeps publishing unchanged states over and over)
When you have many publishers that do the same thing, your receiver might be overwhelmed with all the parallel events to deal with.

However, I’d do it this still slightly differently

void loop() {
  static uint32_t ms PublishLimit = 0; // timing variable to ensure obeying the publish rate limit
  static int      preState = -1;       // remember previously reported state
  int             currState = digitalRead(callmat);

  if (state == prevState || millis() - msPublishLimit <= 1000) return;
  // only act when something has changed and we are not violating the rate limit
  Particle.publish("mtla", state ? "l5on" : "l5off", PRIVATE);
  digitalWrite(boardLed, state);
  msPublishLimit = millis();  
  prevState = currState;

  delay(250); // lazy button debounce (with some more effort this could be really non-blocking)
}

(this is mostly non-blocking and doesn’t re-publish already published states)

SctuffR,

thanks for the reply, im using a 2.2K resistor from GND to D0 and a switch from VCC to D0 so using a pull down resistor, dam i never saw that i hadn’t set a pin state for callmat (see noob lol).

i think i found part of the issue, ive set a 30 second delay between loops and up to now the photon has been online since i started this thread, ill go with your code as it seems a lot more tidier.

for future reference how often can you publish?

thanks again.

https://docs.particle.io/reference/device-os/firmware/photon/#particle-publish-

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.