ESP32 WiFi Remote Computer WakeUP

dragonero89
Posts: 1
Joined: Wed Mar 06, 2019 5:44 pm

ESP32 WiFi Remote Computer WakeUP

Postby dragonero89 » Wed Mar 06, 2019 5:49 pm

Hi everyone,


I'm trying to turn on and off my pc remotely and trough wireless.
In order to do this, i used the code shown below.

Code: Select all

#include <WiFi.h>


const char* ssid     = "ssid";
const char* password = "passwifi";
unsigned long check_wifi = 30000;


WiFiServer server(49152);

const int led1 =  22;      // the number of the LED pin

// Client variables
char linebuf[80];
int charcount=0;
byte mac[6];

void setup() {
 
  // initialize the LEDs pins as an output:
  pinMode(led1, OUTPUT);
 
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while(!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("In connessione a: ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  // attempt to connect to Wifi network:
  while(WiFi.status() != WL_CONNECTED) {
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connesso al WiFi");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[0],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.println(mac[5],HEX);
  server.begin();
}

void loop() {
  // listen for incoming clients
  if ((WiFi.status() != WL_CONNECTED) && (millis() > check_wifi)) {
    Serial.println("Riconnessione al WiFi...");
    WiFi.disconnect();
    WiFi.begin(ssid, password);
    check_wifi = millis() + 30000;
  }
 
  WiFiClient client = server.available();
  if (client) {
    Serial.println("Nuovo accesso");
    memset(linebuf,0,sizeof(linebuf));
    charcount=0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        //read char by char HTTP request
        linebuf[charcount]=c;
        if (charcount<sizeof(linebuf)-1) charcount++;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println();
          client.println("<!DOCTYPE HTML><html><head>");
          client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>");
          client.println("<h1>ESP32 - Accensione Remota PC V2.0</h1>");
          client.println("<p>Relay 1: <a href=\"on1\"><button>Triggera</button></a>&nbsp;");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf,"GET /on1") > 0){
            Serial.println("LED 1 ON");
            digitalWrite(led1, HIGH);
            delay(100);
            Serial.println("LED 1 OFF");
            digitalWrite(led1, LOW);
          }

          // you're starting a new line
          currentLineIsBlank = true;
          memset(linebuf,0,sizeof(linebuf));
          charcount=0;
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    //client.stop();
    //Serial.println("client disconnected");
  }
}

This code is loaded in ESP32 that is connected to a relay.
If access to the ESP32 Webpage, and if i press the button "trigger", the relay will switch on and off, and it turn on my pc.

I made a port forwarding on a random port in order to use this outside home.
This system works, but have 1 issue.

Randomly if i use my cellphone to navigate in wifi LAN, the relay turn on my pc.
This is annoying, because i don't want to turn on my pc.
I want turn on my pc only if i access to my webpage and if i press the "Trigger" button.

In attached you can see my router configuration.

Someone can help me resolving this issue?


Thank you all

Davide
Attachments
ESP32Router.JPG
ESP32Router.JPG (20.84 KiB) Viewed 4179 times

Who is online

Users browsing this forum: No registered users and 8 guests