Skip to content

Instantly share code, notes, and snippets.

@myarduinosale
Created November 17, 2018 09:16
Show Gist options
  • Save myarduinosale/a37bd9c716ae97f4f98697ff30183406 to your computer and use it in GitHub Desktop.
Save myarduinosale/a37bd9c716ae97f4f98697ff30183406 to your computer and use it in GitHub Desktop.
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
*********/
// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
const char* ssid = "my_arduino";
const char* password = "0927566556";
// Web Server on port 80
WiFiServer server(80);
int button = 16;
int temp = 0;
int valuebutton = 0;
void setup() {
// Initializing serial port for debugging purposes
pinMode(button, INPUT);
Serial.begin(115200);
delay(10);
// Connecting to WiFi network
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");
// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
Serial.println(WiFi.localIP());
}
// runs over and over again
void loop() {
// Listenning for new clients
WiFiClient client = server.available();
if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
temp = digitalRead(button);
if (temp == HIGH) {
valuebutton = 1;
Serial.println("LED Turned OFF");
delay(1000);
}
else {
valuebutton = 0;
Serial.println("LED Turned ON");
delay(1000);
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// your actual web page that displays temperature and humidity
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head><body><h1>ESP8266 - Button Test Myarduino.net</h1><h3>button status:");
client.println(valuebutton);
client.println("</h3><h3>");
client.println("</body></html>");
break;
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment