Go Down

Topic: getstatus() help to use in If condition (Read 563 times) previous topic - next topic

salvq

Hello, not sure if this is issue or just I am newbie and do not know how to correctly call the function :)

I am using this connection handler for GSM MKR connection to cellular network. In my routines I would like to check what is current status of the network at certain time and based on that execute routine in main loop or call other routine. I use if condition i.e. do not execute condition in main loop until modem is connected and I have set the CONNECTIONREADY variable in void onNetworkConnect(void *_arg) however this gets reseted once I get out from the routine. Can you help to understand why is variable reseted to 0 when I got out from onNetworkConnect?

Code: [Select]

void loop()
{

  conMan.update();

  static uint32_t s1=millis();

  if ( (millis()-s2) > 50000 && CONNECTIONREADY) {
    s2 = millis();

    Serial.println("Fifty SECONDS");
  }

void onNetworkConnect(void *_arg) {
  Serial.println(">>>> CONNECTED to network");
  bool CONNECTIONREADY = true;
}



Is there better way to set CONNECTIONREADY to 1 or use function which is mention on the readme getStatus(). It says it returns the status, which variable I can read this value from ? I was not able to make a condition using getStatus(). Can you help me how to add condition using getstatus() to validate if condition in main loop ?

Please help me with this,
Thanks,

pert

Code: [Select]
  bool CONNECTIONREADY = true;
Here you declare a variable named CONNECTIONREADY that is local to the scope of the onNetworkConnect function. That variable will go out of scope as soon as the function returns.

Code: [Select]
  if ( (millis()-s2) > 50000 && CONNECTIONREADY) {
Here you reference a variable named CONNECTIONREADY. Since you didn't declare that variable in the scope of the loop function and you seem to imply that your code compiles, I have to assume that you have a global variable named CONNECTIONREADY. I can't be sure though because you didn't post your full code.

You need to understand that the variable named CONNECTIONREADY that is local to the onNetworkConnect function is a completely different variable from your global variable of the same name. If you want to define the global variable in that function then you should not be declaring a new variable of that name.

This is a variable declaration:
Code: [Select]
bool CONNECTIONREADY = true;
This is a variable definition:
Code: [Select]
CONNECTIONREADY = true;

Having two variables of the same name, one within a narrower scope is known as variable shadowing. It is valid in C++, but is a frequent source of bugs because it gets confusing to know which of the two variables of the same name you're working with.

By the way, the common convention is to use all caps names only for macros (with spaces replaced by underscores). You are welcome to name your variables as you like, but it's usually best to follow the standard conventions unless you have a very good reason to diverge from them.

salvq

Thank you for replaying...Ohhh, still lot of stuff to learn :) i will study more about global and local variable to really understand before next post...i was not aware of this difference.

Anyway, what about getStatus() that is mention on the readme page Connetion handler It says it returns the status of current connetion however I do not have clue and tried almost all combinations to get this variable and put against if condition.

Which variable I can read this connection value from ? I was not able to make a condition using getStatus(). Can you help me how to add condition using getstatus() so condition if will be true when millis -s2 is bigger than 50000 && connection is "CONNECTED" as stated in library connectionhandler.h?

Example:
Code: [Select]


void loop()
{

  conMan.update();

  if ( (millis()-s2) > 50000) {
    s2 = millis();

    Serial.println("Fifty SECONDS");
  }
}

salvq

I will just reply for anybody else learning / having same issue to read variable

Need to add "conMan.getStatus() == (NetworkConnectionState::CONNECTED)" to use in If condition.

Also make sure you have classes initiated:
Code: [Select]
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);


Code: [Select]
  if ( (millis()-s2) > 50000 && conMan.getStatus() == (NetworkConnectionState::CONNECTED)) {
    s2 = millis();

    Serial.println("Fifty SECONDS");
  }

pert

I'm glad to see you got your if statement figured out. Thanks for taking the time to post the solution you found! I'm sure anyone else with the same question will be very grateful to you.

Go Up