Wireless bite alarm

Hi looking for a bit of guidance. I’ve designed a wireless bite alarm the works on a vibro sensor and adapted a sketch to incorporate a buzzer for the times I don’t need the wireless function the problem is when I initially turn on the device it looks for a wireless device to connect to if it don’t find one it wont continue. I would like to incorporate a time out function so if no wireless is found with in a time frame then continue without it. can you help me adapt this sketch pls.

#define vibsensorPin  2
#define ledPin  14
const int buzzPin = 4;
#include <ESP8266WiFi.h>

const char* ssid = "JONPHONE";
const char* password = "Mypassword";
boolean bite = false;

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(ledPin, OUTPUT);
  pinMode(vibsensorPin, INPUT);
  pinMode(buzzPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());



}

void loop() {
  
  long fishAlarm = pulseIn(vibsensorPin, HIGH); 
  if (fishAlarm > 100) 
  {
    bite = true;
    digitalWrite (buzzPin, HIGH);
    delay (250);
    digitalWrite (buzzPin, LOW); 
    Serial.println(fishAlarm);
    Serial.println(bite);
  }
 
   
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
  
  // Match the request
  int val;
  if (req.indexOf("/stop/0") != -1)
    val = 0;
  else if (req.indexOf("/start/1") != -1)
    val = 1;
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  // Set GPIO according to the request
  digitalWrite(ledPin, val);
  
  client.flush();

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n Content-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n";
  if (bite == true)
   {
    s += "!!!BITE!!!";
    bite= false;
   }

  else
  {
    s += "no bite";
  }

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");
}

Instead of this:

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

Add a global flag 'wifiDetected ' and do this:

  wifiDetected = true;
  int timeoutCnt = 10; // this will result in a 5 second timeout.  Adjust as desired.
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (--timeCnt == 0)
    {
      Serial.println("");
      Serial.println("No WiFi detected.  Proceeding without WiFi functionality.");
      wifiDetected = false;
      break;
    }
  }

Now you can use the wifiDetected flag in loop() in order to determine whether to execute wifi functions.

ToddL1962:
Instead of this:

  while (WiFi.status() != WL_CONNECTED) {

delay(500);
   Serial.print(".");
 }




Add a global flag 'wifiDetected ' and do this:



wifiDetected = true;
 int timeoutCnt = 10; // this will result in a 5 second timeout.  Adjust as desired.
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
   if (--timeCnt == 0)
   {
     Serial.println("");
     Serial.println("No WiFi detected.  Proceeding without WiFi functionality.");
     wifiDetected = false;
     break;
   }
 }




Now you can use the wifiDetected flag in loop() in order to determine whether to execute wifi functions.

Hi thanks for the help got it working. the only problem (no biggy) is that it don't print the ip address when connected but when I'm out in the field with it it wont matter. thanks again.

What in the world is a "bite alarm"???

RayLivingston:
What in the world is a "bite alarm"???

Hi its for fishing. it detects movement of the rod when a fish is biting.