I want to know if the vibration sensor is sensing vibrations.
The code below is what I am trying to put together. My thought was that since the code for the vibration sensor blinked an LED when vibration was detected. I would just show the status of the ED on the Webserver. I am able to get Connected to the Web and create a web page and the LED connected to Pin 9 blinks when vibration is detected. What I cant seem to find is how to get the webpage to display the status of Pin 9
I have tried several different tutorials but cant seen to get things in the correct order. Could someone point out the way it should be, or point me to some instruction that explains how to monitor the status of an output. I dont need buttons to turn on outputs. I just want to know if the LED is blinking or not.
Thanks everyone for the help
// Load Wi-Fi library
#include <WiFi.h>
#define Sensor_Out_Pin 10
#define LED_Pin 9
#define ON HIGH
#define OFF LOW
int present_condition = 0;
int previous_condition = 0;
//network credentials
const char* ssid = "My Network";
const char* password = "Password";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output9State = "On";
// Assign output variables to GPIO pins
const int output9 = 9;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output9, OUTPUT);
pinMode(Sensor_Out_Pin, INPUT);
// Set outputs to LOW
digitalWrite(output9, LOW);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
IPAddress staticIP(172, 27, 12, 201);
IPAddress gateway(172, 27, 12, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);
WiFi.config(staticIP, gateway, subnet, dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void LED_Pin_blink(void);
void Update_Vibration_Status(void);
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
//Update_Vibration_Status();
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n')
{
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
//Display current state for Pin 9
client.println("<p>9 - State " + output9State + "</p>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
}
else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
else
{
// if you got a newline, then clear currentLine
currentLine = "";
}
}
}
}
Update_Vibration_Status();
// // Clear the header variable
// header = "";
// //Close the connection
// client.stop();
// Serial.println("Client disconnected.");
// Serial.println("");
}
void LED_Pin_blink(void) {
digitalWrite(LED_Pin, ON);
delay(250);
digitalWrite(LED_Pin, OFF);
delay(250);
digitalWrite(LED_Pin, ON);
delay(250);
digitalWrite(LED_Pin, OFF);
delay(250);
}
void Update_Vibration_Status(void)
{
// Status of LED Pin 9
previous_condition = present_condition;
present_condition = digitalRead(Sensor_Out_Pin); // Reading digital data from the Pin9 of the esp32
if (previous_condition != present_condition) {
LED_Pin_blink();
} else {
digitalWrite(LED_Pin, OFF);
}
}