Electron keeps timing out

Hey guys

I’m not sure why, but my Electron keeps timing out on me.

I start it up and initially I can make calls to it from a PHP curl call or from the command line with the expected results. However, I’m not sure if it happens after x number of calls, or x minutes of inactivity, or what, but the device just starts timing out on all calls from either the PHP page or the CLI.

If I reset the device it works as it should for a short while before again beginning to timeout.

Command Line call:

particle call shot-bot shoot A

The code on my device is as follows:

#include "cellular_hal.h"
// set the APN to 'internet' for MTN SIM card
STARTUP(cellular_credentials_set("internet", "", "", NULL));

int trigger1 = D1;
int trigger2 = D2;

//pushbutton pins
int BUTTON_1_PIN = D1;
int BUTTON_2_PIN = D2;
//pump/relay pins
int PUMP_1_PIN = D3;
int PUMP_2_PIN = D4;

//Time for pumping stations to turn on in milliseconds
int PUMP_1_TIME = 2500;
int PUMP_2_TIME = 2500;

void setup()
{
    pinMode(trigger1, OUTPUT);
    pinMode(trigger2, OUTPUT);
    Particle.function("shoot",triggerShot);
    digitalWrite(trigger1, LOW);
    digitalWrite(trigger2, LOW);
   
    pinMode(BUTTON_1_PIN, INPUT);
    pinMode(BUTTON_2_PIN, INPUT);

    pinMode(PUMP_1_PIN, OUTPUT);
    pinMode(PUMP_2_PIN, OUTPUT);
}

void loop()
{
    // check for button1 push
    if(digitalRead(BUTTON_1_PIN) == HIGH)
    {
        digitalWrite(PUMP_1_PIN, HIGH);
        delay(PUMP_1_TIME);
        digitalWrite(PUMP_1_PIN, LOW);
    }

    // check for button2 push        
    if(digitalRead(BUTTON_2_PIN) == HIGH)
    {
        digitalWrite(PUMP_2_PIN, HIGH);
        delay(PUMP_2_TIME);
        digitalWrite(PUMP_2_PIN, LOW);
    }
}

int triggerShot(String command) {
    if (command=="A") {
        digitalWrite(PUMP_1_PIN,HIGH);
        delay(PUMP_1_TIME);
        digitalWrite(PUMP_1_PIN, LOW);
        return 1;
    }
    else if (command=="B") {
        digitalWrite(PUMP_2_PIN,HIGH);
        delay(PUMP_2_TIME);
        digitalWrite(PUMP_2_PIN, LOW);
        return 1;
    }
    else {
        return -1;
    }
}

Other things to note, which I haven’t solved and may or may not be related to the issue.
I cannot update my device OTA (annoying)
When the device starts up it flashes cyan really fast for a short time, and then briefly, a few fast red flashes before breathing cyan and successfully connecting to the cloud.

Any insight into this would be much appreciated.

One thing to always do when using buttons is INPUT_PULLUP or INPUT_PULLDOWN (opposite to the level your button closes to) instead of INPUT (unless you have external pull-resistors in place).

Why are you first setting D1/D2 as OUTPUT, write LOW and then set them as INPUT?

You should also avoid delay() (especially in a Particle.function()) but rather go for non-blocking code.

delay() also reduces the responsiveness of your device for OTA flashing and other cloud stuff.

You could also try SYSTEM_THREAD(ENABLED), but only after you de-blocked your code :wink:

I am having similar problems, with no blocking code. I thought it may be a weak cellular connection, but there is no real basis for me to say that.

watching this thread

See the topic we have the same problem.