Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- const char* ssid = "INTEL......";
- const char* password = "moises.......";
- int ledPin1 = D4;// pino D1
- int ledPin2 = BUILTIN_LED;//led onboard
- WiFiServer server(80);
- void setup() {
- WiFi.persistent(false);//melhora a estabilidade da placa
- Serial.begin(115200);
- delay(10);
- Serial.println();
- Serial.println();
- Serial.print("Pino 1 = ");
- Serial.println(ledPin1);
- Serial.print("Pino 2 = ");
- Serial.println(ledPin2);
- pinMode(ledPin1, OUTPUT);
- digitalWrite(ledPin1, LOW);
- pinMode(ledPin2, OUTPUT);
- digitalWrite(ledPin2, LOW);
- 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");
- server.begin();
- Serial.println("Server started");
- Serial.print("Use this URL to connect: ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.print("/");
- }
- void loop() {
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- Serial.println("new client");
- while (!client.available()) {
- delay(1);
- }
- String request = client.readStringUntil('\r');
- Serial.println(request);
- client.flush();
- int value1 = LOW;
- int value2 = HIGH; //BUILTIN_LED
- if (request.indexOf("/LED1=ON") != -1) {
- digitalWrite(ledPin1, HIGH);
- Serial.println("Led 1 ligado");
- value1 = HIGH;
- }
- if (request.indexOf("/LED1=OFF") != -1) {
- digitalWrite(ledPin1, LOW);
- Serial.println("Led 1 desligado");
- value1 = LOW;
- }
- if (request.indexOf("/LED2=ON") != -1) {
- digitalWrite(ledPin2, LOW);
- Serial.println("Led 2 ligado");
- value2 = LOW;
- }
- if (request.indexOf("/LED2=OFF") != -1) {
- digitalWrite(ledPin2, HIGH);
- Serial.println("Led 2 desligado");
- value2 = HIGH;
- }
- // Return the response
- String b = "";
- b += (F("<!DOCTYPE html>"));
- b += (F("<html lang='en'>"));
- b += (F("<head>"));
- b += (F("<title>MOISES Example</title>"));
- b += (F("<meta charset='utf-8'>"));
- b += (F("<meta name='viewport' content='width=device-width, initial-scale=1'>"));
- b += (F("<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'>"));
- b += (F("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>"));
- b += (F("<script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>"));
- b += (F("</head>"));
- b += (F("<body>"));
- b +=(F("<input type='button' class='btn btn-info' id='btn1' onclick='clickBtn1()' value='Ligar'></input>"));
- b += (F("<input type='button' class='btn btn-info' id='btn2' onclick='clickBtn2()' value='Ligar'></input>"));
- b += (F("<script type='text/javascript'>"));
- b += (F("function clickBtn1(){"));
- b += (F("xmlhttp = new XMLHttpRequest();"));
- b += (F("btn1 = document.getElementById('btn1');"));
- b += (F("if (btn1.value == 'Ligar') {"));
- b += (F("xmlhttp.onreadystatechange = function() {"));
- b += (F("if (this.readyState == 4 && this.status == 200) {"));
- b += (F("btn1.value = 'Desligar';"));
- b += (F("}"));
- b += (F("};"));
- b += (F("xmlhttp.open('SET', '/LED1=ON', true);"));
- b += (F("xmlhttp.send(); "));
- b += (F("} else {"));
- b += (F("xmlhttp.onreadystatechange = function() {"));
- b += (F("if (this.readyState == 4 && this.status == 200) {"));
- b += (F("btn1.value = 'Ligar';"));
- b += (F("}"));
- b += (F("};"));
- b += (F("xmlhttp.open('SET', '/LED1=OFF', true);"));
- b += (F("xmlhttp.send(); "));
- b += (F("}"));
- b += (F("}"));
- b += (F("function clickBtn2(){"));
- b += (F("xmlhttp = new XMLHttpRequest();"));
- b += (F("btn1 = document.getElementById('btn2');"));
- b += (F("if (btn1.value == 'Ligar') {"));
- b += (F("xmlhttp.onreadystatechange = function() {"));
- b += (F("if (this.readyState == 4 && this.status == 200) {"));
- b += (F("btn1.value = 'Desligar';"));
- b += (F("}"));
- b += (F("};"));
- b += (F("xmlhttp.open('SET', '/LED2=ON', true);"));
- b += (F("xmlhttp.send();"));
- b += (F("} else {"));
- b += (F("xmlhttp.onreadystatechange = function() {"));
- b += (F("if (this.readyState == 4 && this.status == 200) {"));
- b += (F("btn1.value = 'Ligar';"));
- b += (F("}"));
- b += (F("};"));
- b += (F("xmlhttp.open('SET', '/LED2=OFF', true);"));
- b += (F("xmlhttp.send(); "));
- b += (F("}"));
- b += (F("}"));
- b += (F("</script>"));
- b += (F("</body>"));
- b += (F("</html>"));
- client.print(b);
- client.flush();
- client.stop();
- delay(1);
- Serial.println("Client disconnected");
- Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement