Skip to content

Instantly share code, notes, and snippets.

@xyzshantaram
Created December 27, 2018 16:03
Show Gist options
  • Save xyzshantaram/f6321a28ab00bce77cddcdec1fcf706d to your computer and use it in GitHub Desktop.
Save xyzshantaram/f6321a28ab00bce77cddcdec1fcf706d to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
const char* ssid = "Ranjit";
const char* password = "avantika98";
const char* host = "IP OF THE ESP8266"; //it will tell you the IP once it starts up
//just write it here afterwards and upload
int ledPin = D3;
WiFiServer server(80); //just pick any port number you like
void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
pinMode(ledPin, OUTPUT);
digitalWrite(D3, LOW);
// 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() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();
// Match the request
if (req.indexOf("") != -10) { //checks if you're on the main page
if (req.indexOf("/ATTN") != -1) { //checks if you clicked the attn button
toneAndBlink(4, ledPin, D0, 100, 5000);
Serial.println("You want attention");
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "<br><input type=\"button\" name=\"bl\" value=\"I Want Attention! \" onclick=\"location.href='/ATTN'\">";
s += "<br><br><br>";
s += "</html>\n";
client.flush();
// Send the response to the client
client.print(s);
delay(1);
}
void toneAndBlink(int no, int led, int buzzer, int dur, int freq) {
for (int i = 0; i < no; i++) {
digitalWrite(led, HIGH);
tone(buzzer, freq);
delay(dur);
digitalWrite(led, LOW);
noTone(buzzer);
delay(dur);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment