Shabnam Deep Kaur
Shabnam Deep Kaur

Reputation: 13

How to stop void loop in arduino?

I am trying to make the void loop in arduino code to just run only once. What is the algorithm or command for this? My code is:

#include <String.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
SoftwareSerial s(D6,D5); // (Rx, Tx)
String saavi="ABCD";
String saavi1="123456789";
String shab;
String arr[4];
int a=0;
String arr1[10];
const char* shab1="";
const char* shab3="";
String shab2;
std::string str;
int i,j,k,x;
bool check=true;
void setup() {
 s.begin(9600);
 Serial.begin(9600);
 Serial.print("enter ssid: ");
}
void loop() {
    s.write("s");
    while(s.available() >0) {
    char data=s.read();
    shab += data;
    Serial.print(data);
   }
    if(shab.length()==4) {
    for(i=0;i<shab.length();i++)
    {
      arr[i]=shab.substring(i);
    }
     shab1 = arr[0].c_str();
     //Serial.println(shab1);
      if(arr[0] == saavi)
    { Serial.println();
      Serial.println("correct ssid");
      Serial.println("Enter ur password");
      delay(2000);
      s.write("s");
    while(s.available() >0) {
    char data1=s.read();
    shab2 += data1;
    Serial.print(data1);
    if(shab2.length()==9) {
    for(k=0;k<shab2.length();k++)
    {
      arr1[k]=shab2.substring(k);
    }
    shab3 = arr1[0].c_str();
    //Serial.println(shab3);
    if(arr1[0] == saavi1)
    {
      Serial.println();
    Serial.println("correct password");
    delay(3000);
    Serial.println("Connecting to wifi: ");
    delay(2000);
    Serial.println(shab1);
    Serial.flush();
    WiFi.begin(shab1,shab3);
     while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
    Serial.println(" IP address: ");
    Serial.print(WiFi.localIP());
    delay(2000);
    break;
    }
    else
    {
    Serial.print("wrong password");
    }
    } 
    }
    }else {
      Serial.print("Wrong ssid");
     }  
 }

}

When I run this code it shows "correct ssid and enter ur password" again and again. This just does not stop. So, how to make void loop run only once in this code. Can anyone please help me?

Just one more doubt. In void loop line no. 18, i have added a delay of 2000ms because without adding delay the next line in the code i.e. "while(s.available() >0) " does not work. This while command is so quick that it needs some delay so that i can enter the password"123456789". What should i do so that this while loop does not work so quick until i press a key from my keypad? Please help!!

Upvotes: 0

Views: 6047

Answers (2)

Mike
Mike

Reputation: 4288

Put an endless loop at the end of the main loop

    ....
    }else {
        Serial.print("Wrong ssid");
    } 
    while(1)                     //endless loop
    {
    }
}

Upvotes: 1

priyank goriya
priyank goriya

Reputation: 56

If you want to perform your code only once into the Arduino.

  • Create a separate function for connecting Device with WiFi.
  • Call that function from the setup method. Like this way, your function is not calling repeatedly.
#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();

  // For first time connection with WiFi
  ConnectWiFi();  
}


void ConnectWiFi()
{
  WiFi.begin("network-name", "pass-to-network");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

}
void loop() {

 // if wifi disable inbetween and need to connect again
 if(WiFi.status() != WL_CONNECTED)
 {
 ConnectWiFi();
 }
}

Upvotes: 1

Related Questions