Electron publishing only while USB connected

Hey all,

Having a bit of a weird issue with an Electron/E-Series sketch.

Breathing cyan and working perfectly while connected to USB.

  • Variables available via GET
  • Publishing to webhook every 5s

When disconnected from USB:

  • Still breathing cyan
  • Variables still available via GET
  • No longer publishes

Code Snippets:

void loop() {
  unsigned long now = millis(); //used for sending periodic message

  //Transmit engine run CAN message periodically
  if (publishEnabled && ((now - lastPublish) >= PUBLISH_RATE)) {
    lastPublish = now;
    publishAll();
  }
void publishAll()
{
    char publishString[64];
    int len = 0;
    
    if((EngineSpeed!=EngineSpeed_lastPublish) || (OilPressure!=OilPressure_lastPublish) || (CoolantTemp!=CoolantTemp_lastPublish)) {
        len = sprintf(publishString, "{\"spd\":%d,\"prs\":%d,\"tmp\":%d}", EngineSpeed, OilPressure, CoolantTemp);

        Serial.print(publishString);
        Serial.println(" publishString");
        Serial.print(len);
        Serial.println(" length of string"); 
        
        Particle.publish("firepush",publishString); 
        
        EngineSpeed_lastPublish = EngineSpeed;
        OilPressure_lastPublish = OilPressure;
        CoolantTemp_lastPublish = CoolantTemp;
    }
}

Thank you kindly in advance!

I would try taking out the part where you only publish if the speed, pressure or temp changes. Since you don’t show how those variables are updated, we can’t really tell if that part of the code is some how failing when USB is not connected.

If your code uses USB serial, I would check those sections well!

1 Like

You should also publish as PRIVATE not to spam the public event stream.
Also assuming PUBLISH_RATE is greater or equal 1000.

Whether or not USB is connected should not have any impact as long your (hidden) code does no somehow block without it or the battery is too low (which shouldn’t be the case due to the fact that variables are still requestable).

1 Like

Thanks heaps to you both for your assistance, it is greatly appreciated.

The source of the issue was actually hardware, as I had unintentionally powered an external component off the USB 5V out. As such it was powering down when unplugged. A rookie error.

I’ve changed published to PRIVATE as ScurffR has suggested and will remember that in future.

Thanks again, please feel free to close thread.

4 Likes