Confused by serial port enable appearing to stop network data only in system_mode auto

Hi,
Sorry , this is more than likely something silly I am doing, however please bear with me as stuck is still stuckā€¦:slight_smile:
I have extracted the basic problem I am experiencing in this code on which, hopefully, someone will be able to enlighten me on what is happening. The problem is when the spark is set to SYSTEM_MODE(AUTOMATIC); (+ WyFy_on is =0 ) and the serial port is enable via MYDEBUG then all is fine and the network client receives the packet. If the serial port code under the MYDEBUG section is not enabled (D0 is low) then no network data is received. However, if i disable the AUTOMATIC system mode and use SYSTEM_MODE(MANUAL); (+WyFy_on =1) then it sends network data fine with the serial port MYDEBUG enabled or not.
I am confused by the removal of the mydebug jumper causing nothing to be sent but only if in Automatic mode.

However, my end goal is to wake up every 10mins , store data on a local SD then to the network every x wake ups. As such minimize the power consumption by running MANUAL seemed the best approach. This code was supposed to give me some timings to work with as it appeared as if the WiFi.on and WiFi.connect were taking longer to connect locally then just leaving it them in Automatic mode, is that possible?,

As deep sleep turns off the wifi my only other chance to reduce power is to be awake as short as possibleā€¦

(hopefully the code formatted correctly)
Thanks and regards Colin


//SYSTEM_MODE(AUTOMATIC);

SYSTEM_MODE(MANUAL);

int wyfy_on =1 ;    // =0 for Auto mode


TCPClient client;   

byte local_server[] = { 192, 168, 0, 195 };  // t43p

int16_t tim_to_sleep = 95;  // default secs to deep sleep not waiting 10mins for testing

int MYDEBUG =1;
int DetSerial_in = D0;

void setup() {
      
    pinMode(DetSerial_in,INPUT_PULLDOWN);        
    MYDEBUG = digitalRead(DetSerial_in);        


    if (MYDEBUG ==1)
    { 
        
        Serial.begin(9600);
        
        digitalWrite(D7,HIGH);      // to also id the Serial Terminal.
      
        int loop1 =0;
        while(!Serial.available() && loop1 <= 20)   // will hold here and wait for serial input for around 10secs
        {
            Serial.println("connecting to serial...");
            loop1 =loop1 +1 ;
            delay(500);
        }
        
        Serial.print("Time Starting: ");
        Serial.println(millis());
         digitalWrite(D7,LOW);      // to also id the Serial Terminal.
   }
   else
   {
       delay(2000);
   }

    if (wyfy_on ==1)
    {
        WiFi.on();
        if (MYDEBUG ==1)
        {
            Serial.println("turning on wifi...");
        }
        delay(1000);   // give it a chance
        WiFi.connect() ;
        if (MYDEBUG ==1)
        {
            Serial.println("establishing wifi connection...");
        }
        delay(1000);   // give it a chance
    }
    
    int loop2 =0;
    if(!WiFi.ready() && loop2 <= 20)  // try connection
    {
        if (MYDEBUG ==1)
        {
            Serial.println("connecting to wifi...");
        }
        loop2 =loop2 +1 ;
        delay(500);
    }
    
    if (MYDEBUG ==1)
    {
        Serial.println("wifi and connection up...");
    }
   
    //
     // try to connect
    client.connect(local_server, 3054);

    int loopa =0;
    while(!client.connected() && loopa <= 20)
    {
        if (MYDEBUG ==1)
        {
            Serial.println("connecting to network...");
        }
        delay(100);   
        loopa =loopa +1 ;
    }
          
    if (!client.connected()) 
    {
        if (MYDEBUG ==1) 
        {
            Serial.println("Couldnt connect to local server.. ");
        }
        delay(400);
    }
    else
    {
        if (MYDEBUG ==1) 
        {
            Serial.println("connected to local server.. ");
        }
        
        client.println("Hello remote box");
        delay(400);
    }
         if (MYDEBUG ==1) 
        {
           Serial.print("Time stopping: ");
            Serial.println(millis());
        }
   
    delay(200);
    WiFi.off();
    Spark.sleep(SLEEP_MODE_DEEP,tim_to_sleep);     // shut down 
}

Hi @floridact,

Thanks for posting your code! The thing that makes the most sense for me here is maybe removing the serial calls is now revealing a race condition that the slower serial calls masked. What if you used the loop() function and checked states there, instead of using the shorter delays? ā€“ I may be totally crazy, just a thought! :slight_smile:

Thanks,
David

Try this:

  while(!Serial.available() && loop1 <= 20)   // will hold here and wait for serial input for around 10secs
        {
            Serial.println("connecting to serial...");
            loop1 =loop1 +1 ;
            delay(500);
            SPARK_WLAN_Loop();        
}

Probably You Need to add a call to SPARK_WLAN_Loop() to the other flows with Long Loops in Your Setup Routine as well.
Having delays of several seconds in Setup without calling SPARK_WLAN_Loop(); does strange things and prevents correct initialization.

Perhaps this helpsā€¦

Michael

Hi,
Thanks for those suggestions, sorry to say neither made any difference.
Appreciate the effort.
Regards
Colin