/* Создание WiFi точки доступа и запуск на ней Web - сервера. */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
/* Установка имени точки доступа и пароля входа в неё */
const char *ssi = "ESP_ksm_890m";
const char *passw = "ksm890mcom";
ESP8266WebServer server(80);
const int led0 = 0; // led0 - это GPIO0
const int led2 = 2;
const int led4 = 4;
const int led5 = 5;
const int led12 = 12;
const int led13 = 13; // led13 - это GPIO13
const int led14 = 14;
const int led15 = 15;
const int led16 = 16;
/* Web - сервер из браузера виден по адресу http://192.168.4.1 */
/* Определения функций, при обращении к которым сервер будет пересылать браузеру соответствующие сообщения */
void led0_on() {
digitalWrite(led0, 0);
server.send(200, "text/html","gpio0 on");
}
void led0_off() {
digitalWrite(led0, 1);
server.send(200, "text/html","gpio0 of");
}
void led2_on() {
digitalWrite(led2, 0);
server.send(200, "text/html","gpio2 on");
}
void led2_off() {
digitalWrite(led2, 1);
server.send(200, "text/html","gpio2 of");
}
void led4_on() {
digitalWrite(led4, 0);
server.send(200, "text/html","gpio4 on");
}
void led4_off() {
digitalWrite(led4, 1);
server.send(200, "text/html","gpio4 of");
}
void led5_on() {
digitalWrite(led5, 0);
server.send(200, "text/html","gpio5 on");
}
void led5_off() {
digitalWrite(led5, 1);
server.send(200, "text/html","gpio5 of");
}
void led12_on() {
digitalWrite(led12, 1);
server.send(200, "text/html","gpio12 on");
}
void led12_off() {
digitalWrite(led12, 0);
server.send(200, "text/html","gpio12 of");
}
void led13_on() {
digitalWrite(led13, 1);
server.send(200, "text/html","gpio13 on");
}
void led13_off() {
digitalWrite(led13, 0);
server.send(200, "text/html","gpio13 of");
}
void led14_on() {
digitalWrite(led14, 0);
server.send(200, "text/html","gpio14 on");
}
void led14_off() {
digitalWrite(led14, 1);
server.send(200, "text/html","gpio14 of");
}
void led15_on() {
digitalWrite(led15, 1);
server.send(200, "text/html","gpio15 on");
}
void led15_off() {
digitalWrite(led15, 0);
server.send(200, "text/html","gpio15 of");
}
void led16_on() {
digitalWrite(led16, 0);
server.send(200, "text/html","gpio16 on");
}
void led16_off() {
digitalWrite(led16, 1);
server.send(200, "text/html","gpio16 of");
}
void pwm120() {
analogWrite(12, 0);
server.send(200, "text/html","Slider12 0");
}
void pwm121() {
analogWrite(12, 5);
server.send(200, "text/html","Slider12 1");
}
void pwm122() {
analogWrite(12, 15);
server.send(200, "text/html","Slider12 2");
}
void pwm123() {
analogWrite(12, 60);
server.send(200, "text/html","Slider12 3");
}
void pwm124() {
analogWrite(12, 100);
server.send(200, "text/html","Slider12 4");
}
void pwm125() {
analogWrite(12, 150);
server.send(200, "text/html","Slider12 5");
}
void pwm126() {
analogWrite(12, 255);
server.send(200, "text/html","Slider12 6");
}
void pwm130() {
analogWrite(13, 0);
server.send(200, "text/html","Slider13 0");
}
void pwm131() {
analogWrite(13, 5);
server.send(200, "text/html","Slider13 1");
}
void pwm132() {
analogWrite(13, 15);
server.send(200, "text/html","Slider13 2");
}
void pwm133() {
analogWrite(13, 60);
server.send(200, "text/html","Slider13 3");
}
void pwm134() {
analogWrite(13, 100);
server.send(200, "text/html","Slider13 4");
}
void pwm135() {
analogWrite(13, 150);
server.send(200, "text/html","Slider13 5");
}
void pwm136() {
analogWrite(13, 255);
server.send(200, "text/html","Slider13 6");
}
void pwm150() {
analogWrite(15, 0);
server.send(200, "text/html","Slider15 0");
}
void pwm151() {
analogWrite(15, 5);
server.send(200, "text/html","Slider15 1");
}
void pwm152() {
analogWrite(15, 15);
server.send(200, "text/html","Slider15 2");
}
void pwm153() {
analogWrite(15, 60);
server.send(200, "text/html","Slider15 3");
}
void pwm154() {
analogWrite(15, 100);
server.send(200, "text/html","Slider15 4");
}
void pwm155() {
analogWrite(15, 150);
server.send(200, "text/html","Slider15 5");
}
void pwm156() {
analogWrite(15, 255);
server.send(200, "text/html","Slider15 6");
}
void root(){
server.send(200, "text/html","<h1>This is web-server on Wifi-esp-12.<br>He controls GPIO.<br>Command:/gpioxxon /gpioxxoff</h1>");
}
void setup() {
// GPIO работают на выход
pinMode(led0, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
pinMode(led16, OUTPUT);
// Чтобы после включения платы светодиоды не горели, на GPIO устанавливаются следующие уровни
// Так распаяна плата
digitalWrite(led0, 1);
digitalWrite(led2, 1);
digitalWrite(led4, 1);
digitalWrite(led5, 1);
digitalWrite(led12, 0);
digitalWrite(led13, 0);
digitalWrite(led14, 1);
digitalWrite(led15, 0);
digitalWrite(led16, 1);
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssi, passw);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/gpio0on", led0_on); // Если набрать адрес http://192.168.4.1/gpio0on
// то браузер получит ответ gpio0 on
server.on("/gpio0off", led0_off);
server.on("/gpio2on", led2_on);
server.on("/gpio2off", led2_off);
server.on("/gpio4on", led4_on);
server.on("/gpio4off", led4_off);
server.on("/gpio5on", led5_on);
server.on("/gpio5off", led5_off);
server.on("/gpio12on", led12_on);
server.on("/gpio12off",led12_off);
server.on("/gpio13on", led13_on);
server.on("/gpio13off",led13_off);
server.on("/gpio14on", led14_on);
server.on("/gpio14off",led14_off);
server.on("/gpio15on", led15_on);
server.on("/gpio15off",led15_off);
server.on("/gpio16on", led16_on);
server.on("/gpio16off",led16_off);
server.on("/", root);
server.on("/pwm120", pwm120);
server.on("/pwm121", pwm121);
server.on("/pwm122", pwm122);
server.on("/pwm123", pwm123);
server.on("/pwm124", pwm124);
server.on("/pwm125", pwm125);
server.on("/pwm126", pwm126);
server.on("/pwm130", pwm130);
server.on("/pwm131", pwm131);
server.on("/pwm132", pwm132);
server.on("/pwm133", pwm133);
server.on("/pwm134", pwm134);
server.on("/pwm135", pwm135);
server.on("/pwm136", pwm136);
server.on("/pwm150", pwm150);
server.on("/pwm151", pwm151);
server.on("/pwm152", pwm152);
server.on("/pwm153", pwm153);
server.on("/pwm154", pwm154);
server.on("/pwm155", pwm155);
server.on("/pwm156", pwm156);
server.begin();
Serial.println("HTTP server started");
}
// Цикл по получению пакетов от браузера
void loop() {
server.handleClient();
}