Удаленное включение/выключение/перезагрузка ПК/фермы с помощью NodeMCU

Код:

#include <ESP8266WiFi.h>
#include <string.h>

#define RELAY1R_PIN 4 // reboot D2
#define RELAY1P_PIN 14 // power D5

#define RELAY2R_PIN 12 // reboot
#define RELAY2P_PIN 13 // power

const char* ssid = "SSID";
const char* password = "password";

const boolean debug = 1;

int timeOut = 0;
int count = 0;

String header = "";
String footer = "";
String s = "";

WiFiServer server(80);

extern "C" {
#include "user_interface.h"
  bool wifi_set_sleep_type(sleep_type_t);
  sleep_type_t wifi_get_sleep_type(void);
}

void setup() {
  
  if (debug==1) {
    Serial.begin(115200);
    delay(10);
  };
    
  if (debug==1) {
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
  };
  WiFi.mode(WIFI_STA);
  wifi_set_sleep_type(NONE_SLEEP_T);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (count==0) {
      count=1;
    } else {
      count=0;
    };
    if (debug==1) Serial.print(".");
  }
  // Start the server
  server.begin();
  if (debug==1) {
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("Server started");
    Serial.println(WiFi.localIP());
  };
  header = "HTTP/1.1 200 OK\r\n";
  header = header + "Content-Type: text/html\r\n\r\n";
  header = header + "\r\n";
  header = header + "\r\n";
  header = header + "\r\n";
  //header = header + "";
  header = header + "FERMA-A REBOOT\r\n";
  header = header + "\r\n";
  header = header + "\r\n";
  footer = "\r\n";
  footer = footer + "\r\n";
}

void loop() {

  if (debug==1) {
    Serial.println(WiFi.status());
  };
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    delay(1000);
    return;
  }
  // Wait until the client sends some data
  if (debug==1) Serial.println("new client");
  while(!client.available()){
    delay(1);
    timeOut = timeOut +1;
    if (timeOut>=15) { // 500
      client.stop();
      client.flush();
      timeOut = 0;
      return; // break
    };
  }
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  if (debug==1) {
    Serial.println(req);
  }
  client.flush();
  float heap = ESP.getFreeHeap();
  // Match the request
  if (req.indexOf("/favicon.ico") != -1) {
    s = "HTTP/1.1 404 Not found\r\n";
    client.print(s);
  } else if (req.indexOf("/r1") != -1) {
    client.print(header);
    pinMode(RELAY1R_PIN, OUTPUT);
    delay(1000);
    pinMode(RELAY1R_PIN, INPUT);
    client.println("reboot complete
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/r2") != -1) { client.print(header); pinMode(RELAY2R_PIN, OUTPUT); delay(1000); pinMode(RELAY2R_PIN, INPUT); client.println("reboot complete
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/p1_on") != -1) { client.print(header); pinMode(RELAY1P_PIN, OUTPUT); delay(1000); pinMode(RELAY1P_PIN, INPUT); client.println("power switch on
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/p2_on") != -1) { client.print(header); pinMode(RELAY2P_PIN, OUTPUT); delay(1000); pinMode(RELAY2P_PIN, INPUT); client.println("power switch on
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/p1_off") != -1) { client.print(header); pinMode(RELAY1P_PIN, OUTPUT); delay(10000); pinMode(RELAY1P_PIN, INPUT); client.println("power switch off
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/p2_off") != -1) { client.print(header); pinMode(RELAY2P_PIN, OUTPUT); delay(10000); pinMode(RELAY2P_PIN, INPUT); client.println("power switch off
"); client.println("
"); client.stop(); client.flush(); return; } else if (req.indexOf("/h") != -1) { //String answer="HTTP/1.1 " + String(h) + " OK\r\n"; //client.print(answer); } else { client.print(header); //client.print(t); client.println("
"); client.println("
"); client.println("
"); client.println("
"); client.println("
"); client.println("
"); client.println("
"); client.println(footer); client.stop(); client.flush(); return; } delay(1); if (debug==1) Serial.println("Client disonnected"); }

Скачать (исходный код скетча под Arduino IDE)

7 мая 2017
Версия 0.3
Денис Пак, генеральный директор