Hi there,
Brand new to the world of Arduino so please be gentle!
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 :
- It's not meant to and
- 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