#include <ESP8266WiFi.h>
#include "ESP8266Ping.h"
#include <EasyButton.h>
#define MOSFET 10
#define SWITCH 0
#define DETECT A0
#define LED 13
#define GLED 5
#define DEBUG True
EasyButton button(SWITCH);
// REMOVED SOME PART OF CODE FOR READABILITY
const char* google_ip= "www.google.com";
bool isButtonPressed = false;
void onPressedForDuration();
void led_blink();
void setup() {
pinMode(SWITCH, INPUT); // Initialize the GPIO2 pin as an output
pinMode(MOSFET, OUTPUT); // Initialize the GPIO0 pin as an output
pinMode(DETECT, INPUT);
pinMode(LED, OUTPUT);
pinMode(GLED, OUTPUT);
digitalWrite(MOSFET, LOW); // Turn MOSFET ON (Note that LOW or HIGH is the voltage level
digitalWrite(LED, LOW);
digitalWrite(GLED, HIGH);
button.begin();
button.onPressedFor(2000, onPressedForDuration);
}
void loop() {
button.read();
digitalWrite(GLED, HIGH);
while (!isButtonPressed)
{
while(wifiManager.autoConnect() == false)
{
button.read();
digitalWrite(LED, HIGH);
delay(2000);
digitalWrite(LED, LOW);
delay(2000);
cicle_MOSFET();
}
while(googleFails < googleFailsMax and yahooFails < yahooFailsMax){
button.read();
//delay between two check cicle
delay_check();
#ifdef DEBUG
Serial.print("Pinging vpn Server ");
Serial.println(google_ip);
#endif
if(Ping.ping(IPAddress(8,8,8,8))){
button.read();
digitalWrite(GLED, LOW);
#ifdef DEBUG
Serial.println(Ping.averageTime());
button.read();
Serial.print("Success! Will try ping again in ");
Serial.print(minuteBetweenCheck);
Serial.println(" minutes");
button.read();
#endif
}
else {
digitalWrite(GLED, HIGH);
button.read();
#ifdef DEBUG
Serial.println("PING FAILED!");
Serial.print("Incrementing vpnFail count to ");
button.read();
Serial.println(googleFails);
#endif
++googleFails; // Increment googleFails counter
if (!Ping.ping(IPAddress(1,1,1,1))){
button.read();
yahooFails++;
}
}
}
cicle_MOSFET();
}
}
void onPressedForDuration()
{
isButtonPressed = true;
Serial.println("BUTTON PRESSED . BUTTON PRESSED FOR 5 SECONDS.");
delay(2000);
led_blink();
Serial.println("Button pressed");
}
void led_blink()
{
int i = 0;
while(i < 3)
{
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
digitalWrite(LED, HIGH);
i = i + 1;
}
}
void delay_check() {
while(eslapedCheck < minuteBetweenCheck*60) {
eslapedCheck++;
delay(100);
}
eslapedCheck = 0;
}
void cicle_MOSFET() {
Serial.println("CIRCLING MOSFET. CIRCLING MOSFET.");
digitalWrite(MOSFET, HIGH); // Turn MOSFET OFF
while(flashing < 5) {
delay(50); // Delay 100 ms * 100 = 10 seconds
flashing++;
delay(50); // Delay 100 ms * 100 = 10 seconds
led_blink();
Serial.println("CIRCLING MOSFET. CIRCLING MOSFET.");
}
digitalWrite(MOSFET, LOW); // Turn MOSFET ON
digitalWrite(LED, LOW);
}
One thing that stood out looking at your code is ... Is you're
#define SWITCH 0
For ESP8266 boards the numbers stenciled on the board are not the same as code values. There are constants set up under the covers for you depending on what board type you're using that are taken care of when you use the proper board setting in the Arduino IDE.
The standard way of handling this is... if you're switch is physically connected to the pin label D0, set your
#define SWITCH D0 // D0 is actually mapped to GPIO16.
The non-standard way if you really want to use #define SWITCH 0, is make sure your switch wire is connected to pin marked D3. Here is a user friendly chart about 1/3rd the way down the page...
https://randomnerdtutorials.com/esp8266-pinout-reference-gpios/
If I'm all wrong, reply and I'll take a harder look at your code.
Good luck
w/ GUI Admin Client, Drag & Drop File Manager, OTA Built-In, Access Point Manager,
Performance Metrics, Web Socket Comms, App API, All running on ESP8266...
Even usable on ESP-01S --- Please check it out!
https://inqonthat.com/inqportal-the-three-line-promise/
https://InqOnThat.com/inqportal
The code as written attempts to use GPIO10 for MOSFET. That is a complete no-no as GPIO10 is part of the flash ROM interface where code is read from.
The code shown is also not complete as there are several variables not declared.
There is also the use of 'and' to perform logic rather than && which should produce compilation errors unless 'and' itself has been defined which would be strange.