Particle Functions not showing

Hey. I was using the Particle to open and close my gate and it was working fine all these days. So i decided to make some changes to the code and publish again via wifi and suddenly now my functions dont appear. I’m using a library liquidcrystal-i2c. When i comment all the lines of code which uses this library the functions appear. But all of this was working fine before i flashed the new firmware.

Please help me get this fixed.

Thanks

This is my code:

// This #include statement was automatically added by the Particle IDE.
#include "LiquidCrystal_I2C_Spark/LiquidCrystal_I2C_Spark.h"


LiquidCrystal_I2C *lcd;

const char OLED_Address  = 0x27;


const String version = "v1";


const int pinLED1 = D6;
const int pinLED2 = D7;
const int pinReedSensor = A0;
const int pinRelayGND = D3;
const int pinRelaySignal = D2;
int lastSecond = 0;

long lastClosedTime;
long lastOpenedTime;
int isClosed = 0;
bool isCleared = false;
bool isInitiated = false;
bool checkGateOpen = false;
bool showClock = false;
int screenOffDelay = 60000;//time before screen turns off when door is closed
int doorButtonDelay = 1000;

void setup()
{
    Serial.begin(9600);
    lcd = new LiquidCrystal_I2C(0x27, 16, 2);
    initiateDisplay();
    //pin initializations
    pinMode(pinLED1,OUTPUT);
    digitalWrite(pinLED1, LOW);     
    pinMode(pinLED2,OUTPUT); 
    digitalWrite(pinLED2, LOW);    
    
    pinMode(pinRelayGND,OUTPUT);    
    digitalWrite(pinRelayGND, LOW);    
    pinMode(pinRelaySignal,OUTPUT);    
    digitalWrite(pinRelaySignal, LOW);    
   
    pinMode(pinReedSensor,INPUT); 
    Time.zone(+8);
    
    isClosed = digitalRead(pinReedSensor);

    Spark.function("OpenDoor",OperateDoor);  
    
    Spark.variable("isClosed", &isClosed, INT);
    
}

void loop()
{
    
    if(isClosed==0 && digitalRead(pinReedSensor)==1){
        lcd->clear();
        lcd->print("DOOR CLOSED");
        lastClosedTime = millis();
        isCleared = false;
        isInitiated = false;
        
    }
    if(isClosed==1 && digitalRead(pinReedSensor)==0){
        showClock = false;
        lcd->clear();
        lcd->print("DOOR OPEN");
        lastOpenedTime = millis();
    }  
    
    if((lastOpenedTime + (60000 * 5))<millis()  && isClosed == 0 && checkGateOpen == false){
        Particle.publish("Gate Is Open");
        checkGateOpen = true;
    }
    
    if((lastClosedTime + 30000)<millis() && !isInitiated && isClosed == 1){
        initiateDisplay();
        isInitiated = true;
    }
    
    
    if((lastClosedTime + screenOffDelay)<millis() && !isCleared && isClosed == 1){
        lcd->clear();
        isCleared = true;
        checkGateOpen = false;
        showClock = true;
    }
    
    if(showClock == true && isClosed ==1)
    {
        if (Time.second() != lastSecond)
        {
            lcd->clear();
            Serial.print(Time.timeStr());
            lcd->setCursor(3,0);
            lcd->print("TIME CHECK");
            lcd->setCursor(0,1);
            lcd->print(Time.hour() < 10? "   0" : "    ");
            lcd->print(Time.hour());
            lcd->print(Time.minute() < 10? ":0": ":");
            lcd->print(Time.minute());
            lcd->print(Time.second() < 10? ":0": ":");
            lcd->print(Time.second());
            lastSecond = Time.second();
        }
    }
    
    isClosed = digitalRead(pinReedSensor);
    Serial.println(isClosed);
}

int OperateDoor(String args){
    int status_code = -1;
    if(args == "OPEN" && isClosed==1){
        lcd->clear();
        lcd->backlight();
        lcd->print("OPENING DOOR");
        digitalWrite(pinRelaySignal, HIGH);
        digitalWrite(pinLED2, HIGH);
        delay(doorButtonDelay);
        digitalWrite(pinRelaySignal, LOW);
        digitalWrite(pinLED2, LOW);
        status_code = 1;  
        
    }else if(args == "CLOSE" && isClosed == 0){
        lcd->clear();
        lcd->print("CLOSING DOOR");
        digitalWrite(pinRelaySignal, HIGH);
        digitalWrite(pinLED2, HIGH);
        delay(doorButtonDelay);
        digitalWrite(pinRelaySignal, LOW);
        digitalWrite(pinLED2, LOW);
        status_code = 0;
        
    }
    
    return status_code;
}

int initiateDisplay(){
    lcd->init();
    lcd->backlight();
    lcd->clear();
    lcd->setCursor(1,0);
    lcd->print("*SMART GARAGE*");
    lcd->setCursor(6,1);
    lcd->print("V.1");
}

Try placing your Particle.functions and Particle.variables at the beginning of your Setup(), and see if that helps.

Yes that did help. But all this time it was working before ? Do you know what was the cause of this problem ? Because i was using this gate system for about a week and suddenly i just added a new function and tested the code and it stopped working. Then i reverted back to the old version and it still wasnt showing the functions. Now after i moved the functions and variables to the top its working again :confused:

Seeing as I don’t know what you changed since last time, it’s hard to tell. Most likely something in the setup that was being called before the functions was blocking for too long, causing the functions not to be registered.

Hey. My particle keeps restarting like every hour or so. Some times longer but it does happen. I have set IFTTT recipes to let me know when it goes offline and online so i keep getting notifications all the time. I have power at home when this happens and my internet does not drop off either. Can someone shed some light on this problem?

Thanks.

With this little info we can just guess, and I’m guessing your device goes into some SOS fault and restarts

Hey. No it does not go into SOS fault. Its a setup i have done to open and close my garage. It works fine. Its just that from time to time the device goes offline blinking green and connects to the cloud again within seconds. This happens every hour or two hours every day. I can post the code here for you to check it out.


// This #include statement was automatically added by the Particle IDE.
#include "LiquidCrystal_I2C_Spark/LiquidCrystal_I2C_Spark.h"


LiquidCrystal_I2C *lcd;

const char OLED_Address  = 0x27;


const String version = "v1";


const int pinLED1 = D6;
const int pinLED2 = D7;
const int pinReedSensor = A0;
const int pinRelayGND = D3;
const int pinRelaySignal = D2;
int lastSecond = 0;

long lastClosedTime;
long lastOpenedTime;
int isClosed = 0;
bool isCleared = false;
bool isInitiated = false;
bool checkGateOpen = false;
bool showClock = false;
int screenOffDelay = 60000;//time before screen turns off when door is closed
int doorButtonDelay = 1000;

void setup()
{
    Spark.function("OpenDoor",OperateDoor);  
    Spark.variable("isClosed", &isClosed, INT);
    Serial.begin(9600);
    lcd = new LiquidCrystal_I2C(0x27, 16, 2);
    initiateDisplay();
    //pin initializations
    pinMode(pinLED1,OUTPUT);
    digitalWrite(pinLED1, LOW);     
    pinMode(pinLED2,OUTPUT); 
    digitalWrite(pinLED2, LOW);    
    
    pinMode(pinRelayGND,OUTPUT);    
    digitalWrite(pinRelayGND, LOW);    
    pinMode(pinRelaySignal,OUTPUT);    
    digitalWrite(pinRelaySignal, LOW);    
   
    pinMode(pinReedSensor,INPUT); 
    Time.zone(+8);
    
    isClosed = digitalRead(pinReedSensor);
    
}

void loop()
{
    
    if(isClosed==0 && digitalRead(pinReedSensor)==1){
        lcd->clear();
        lcd->print("DOOR CLOSED");
        lastClosedTime = millis();
        isCleared = false;
        isInitiated = false;
        
    }
    if(isClosed==1 && digitalRead(pinReedSensor)==0){
        showClock = false;
        lcd->clear();
        lcd->print("DOOR OPEN");
        lastOpenedTime = millis();
    }  
    
    if((lastOpenedTime + (60000 * 5))<millis()  && isClosed == 0 && checkGateOpen == false){
        Particle.publish("Gate Is Open");
        checkGateOpen = true;
    }
    
    if((lastClosedTime + 30000)<millis() && !isInitiated && isClosed == 1){
        initiateDisplay();
        isInitiated = true;
    }
    
    
    if((lastClosedTime + screenOffDelay)<millis() && !isCleared && isClosed == 1){
        lcd->clear();
        isCleared = true;
        checkGateOpen = false;
        showClock = true;
    }
    
    if(showClock == true && isClosed ==1)
    {
        if (Time.second() != lastSecond)
        {
            lcd->clear();
            Serial.print(Time.timeStr());
            lcd->setCursor(3,0);
            lcd->print("TIME CHECK");
            lcd->setCursor(0,1);
            lcd->print(Time.hour() < 10? "   0" : "    ");
            lcd->print(Time.hour());
            lcd->print(Time.minute() < 10? ":0": ":");
            lcd->print(Time.minute());
            lcd->print(Time.second() < 10? ":0": ":");
            lcd->print(Time.second());
            lastSecond = Time.second();
        }
    }
    
    isClosed = digitalRead(pinReedSensor);
    Serial.println(isClosed);
}

int OperateDoor(String args){
    int status_code = -1;
    if(args == "OPEN" && isClosed==1){
        lcd->clear();
        lcd->backlight();
        lcd->print("OPENING DOOR");
        digitalWrite(pinRelaySignal, HIGH);
        digitalWrite(pinLED2, HIGH);
        delay(doorButtonDelay);
        digitalWrite(pinRelaySignal, LOW);
        digitalWrite(pinLED2, LOW);
        status_code = 1;  
        
    }else if(args == "CLOSE" && isClosed == 0){
        lcd->clear();
        lcd->print("CLOSING DOOR");
        digitalWrite(pinRelaySignal, HIGH);
        digitalWrite(pinLED2, HIGH);
        delay(doorButtonDelay);
        digitalWrite(pinRelaySignal, LOW);
        digitalWrite(pinLED2, LOW);
        status_code = 0;
        
    }
    
    return status_code;
}

int initiateDisplay(){
    lcd->init();
    lcd->backlight();
    lcd->clear();
    lcd->setCursor(1,0);
    lcd->print("*SMART GARAGE*");
    lcd->setCursor(6,1);
    lcd->print("V.1");
}

I can’t see anything obvious, but a re-lease of the WiFi IP might be causing such reconnects.
Can you check that setting (lease time) on your router?

1 Like