Void Setup running more than once :-(

Hi there,

Brand new to the world of Arduino so please be gentle! :slight_smile:

I've created some code (using a Wemos D1 Mini) which literally just checks to see if a button is pressed and then fires off a request to a URL (IFTTT) to notify me on my phone.

This all works fine however I noticed when pulling up the Serial Monitor that the void setup part of the code runs more than once, in fact it looks like it's running every second or even less than that.

I'm fairly certain it can't be as :

  1. It's not meant to and
  2. If it were then I don't believe my code would ever run

However I've got some Serial.println lines in there that print out to the monitor when the board is connecting to WIFI and it just seems to print over and over again and again so just wondering why this might be?

My code is here below :

/* SELECT BOARD - LOLIN(WEMOS) D1 R2 & mini */

/* Include required libraries */
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

/* Set up our variables */
const int   checkPin = 14;
int         pinState = 0;

/* Set up variables for WiFi Information */
const char* ssid = "MyWiFi Name";
const char* password = "********";
/* Variables above blanked out for posting to forum, WiFi does indeed connect */


void setup() {
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  delay(50);
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED OFF (HIGH is the voltage level)

  // put your setup code here, to run once:
  Serial.begin(115200);
  /* Attempt to connect to WIFI network */
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to :\n");
    Serial.println(ssid);
    }
}

void loop() {
  // put your main code here, to run repeatedly:
  pinState = digitalRead (checkPin);

  // Check WiFi connection status and if connection has been made on GPIO14

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to:\n");
    Serial.println(ssid);

  }
  if ((WiFi.status() == WL_CONNECTED) && (pinState == HIGH)) {
      Serial.println("Connecting to IFTTT :\n");

    // Declare an object of class HTTPClient 
    HTTPClient http;
    // Specify request destination
    http.begin("http://maker.ifttt.com/trigger/door_state_changed/with/key/****************");
    // Send the request
    int httpCode = http.GET();

    // Check the returning code
    if (httpCode > 0) {

      // Get the request response payload
      String payload = http.getString();
      // Print the response payload
      Serial.println(payload);

    digitalWrite(LED_BUILTIN, LOW);   // turn the LED OFF (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, HIGH);    // turn the LED ON by making the voltage LOW

 
    }
    
    // Close connection
    http.end();
 
  }

delay(50);

}

So just wondering why my serial monitor goes crazy and prints out :

Connecting to : MyWiFi Name

over and over again and again instead of just the once?

One other question if I may also?

The LED turns on at the start when the board is turned on but I have that piece of code in the void setup which is (I thought) supposed to turn the LED off. It does turn off and do what it's supposed to do once you've pressed the button once but I wanted the LED to be off as default when the board is turned on. Just wondering what I've done wrong there with respect to that?

Many thanks in advance for any help with this.

Best wishes,

Mark

If you don't get connected, this

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to :\n");
    Serial.println(ssid);
    }

is going to loop.

You get that over and over because you are never connecting to the WiFi AP.

Change your connect loop to this:

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(WiFi.status() );
    }

WiFi.status()
0 WiFi is in process of changing between statuses
1 SSID cannot be reached
2 Scan Completed
3 Successful connection is established
4 Password is incorrect
5 Connection Lost
6 Module is not configured in station mode

WL_CONNECTED is == 3, which is what your loop is testing for.
If you print the status you will have a clue why you aren't connecting to the AP.

Try connecting to the wifi first without the whole procedure . The code is in loop due to not connecting to wifi .Here's a simple example to print and check the reason for unestablished connection

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char ssid[] = "MyWiFi Name";
const char password[] = "********";


void setup() {
  
  // put your setup code here, to run once:
  Serial.begin(9600)
  WiFi.begin(ssid, password);
 }

void loop(){

if(WiFi.status() != WL_CONNECTED || 3) {

    Serial.println(WiFi.status);

    }

else{

Serial.println("connected successfully");

}    }

Try solving the problem.
Some causes might be : Incorrect password(Check case sensitivity )
Mac address blocking setup in router(might not allow other devices)
Range(too far)