0

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!!

3
  • put an endlessloop (while(1); at the end of your mainloop. btw. your code is hard to read without correct indentation. Commented Apr 8, 2019 at 5:32
  • this command (while(1); gives an error. Commented Apr 8, 2019 at 6:21
  • If you want your code to be called once, create a function of it and call it in the setup() function. And to add endless while: the syntex is while(1){}. Commented Apr 8, 2019 at 8:07

2 Answers 2

1

Put an endless loop at the end of the main loop

    ....
    }else {
        Serial.print("Wrong ssid");
    } 
    while(1)                     //endless loop
    {
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

why the delay int he while loop?
Thank You so much @Mike
I have one more doubt if you can help ?
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?
I guess this is a new question.
|
1

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();
 }
}

2 Comments

why do you connect to wifi if status is connected? I think you're missing a negation here
Thanks for your feedback, I have updated the code block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.