Background
I'll start my epic with a little background. I had to make a project for the 9th grade (yes, now, in order for you to be admitted to the exams, you need to make a project and defend it).
I decided almost immediately that it would be a smart house (although even a sparrow is smarter than him), because for a long time I wanted to do something like this, but my hands did not reach, and then an excellent opportunity turned up + you can show off a little.
This post is my reworked diploma, and indeed the first attempt at writing such things)
Plan
For successful and productive work, you need a plan, for myself I made it something like this:
The idea and ideology of a smart home.
Iron for building a smart home.
Code for each part of the smart home.
In principle, this is more than enough for a start. Now you can disassemble each item separately.
The idea and ideology of a smart home
, , , . , , ( xiaomi), .
. - , , . - .
: - , , , , , , . , .
, :
.
.
.
, , , , .
, , - , wi-fi .
- MQTT, .
, , , ESP-12F . , WI-FI ( , ) , , 100 , 10 .
esp12, - esp12, esp12F. , F- , Flash : E- 1, F- 4. , , F-. - esp12S, ( ) , smd .
WI-FI
, 3.
(wifi) - , ( , ) , .
:
1 - , ( 5, 3.3).
2 - .
, , , , , .
, , .
, , , esp12F 3.3, 5, , .
AC/DC , , 220 5 1 , , 0,3-0,2. 315, .
, esp12F 3.3, 5, , , , AMS1117.
, ( ), 3 .
, , 10 , .
, , , , 5 , - , - , - .
, , DHT11, DS18B20 .
: MQTTT.
3 , , , , !
3 .
....
RTOS-SDK ESP, , Arduino, FreeRTOS .
, , RTOS .
, esp, ( ) 5, esp12 .
... , - 2 ... ! , , ). , , , , , ...
, .
MQTT
, MQTT . :
- , , .
- , "" "".
- , , .
- , .
, , .
yandex iot, , , , wqtt, 300 ).
, : . wifi ip , .
.
, :
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <PubSubClient.h>
, wifi MQTT:
#define RELEPIN 4 //
const char *ssid_ap = "Rozetka_Setup"; //
const char *password_ap = "12345678"; //
const char *ID = "rele_1";// , ID
String ssid = ""; // wifi
String password = ""; // wifi
String device_id; // ID
bool setup_mode; // true - , false -
bool rele;// true - , false -
const char *mqtt_server = "M5.WQTT.RU"; // MQTT
const int mqtt_port = 2602; // MQTT
const char *mqtt_user = "user"; // a
const char *mqtt_pass = "pass"; //
IPAddress local_ip(192, 168, 1, 1);//IP
IPAddress gateway(192, 168, 1, 1);//
IPAddress subnet(255, 255, 255, 0);//
IPAddress local_ip_2(192, 168, 0, 250);//IP WIFI
IPAddress gateway_2(192, 168, 0, 1);// WIFI
IPAddress subnet_2(255, 255, 255, 0);// WIFI
ESP8266WebServer server(80);// server
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server, mqtt_port);
Setup :
pinMode(RELEPIN, OUTPUT);
Serial.begin(115200);
EEPROM.begin(256);// EEPROM
setup_mode = EEPROM.read(45);// EERPOM
rele = EEPROM.read(60);// EERPOM
EEPROM.end();// EEPROM
if (rele) digitalWrite(RELEPIN, HIGH); else digitalWrite(RELEPIN, LOW);
delay(1000);
WiFi.softAP(ssid_ap, password_ap);// ( )
WiFi.softAPConfig(local_ip, gateway, subnet);// (ip )
delay(100);
server.on("/", handle_OnConnect);//handle
server.on("/end_setup", handle_EndSetup);//handle
server.on("/action_page", handleForm);//handle
server.onNotFound(handle_NotFound);
server.begin();//
Serial.println("HTTP server started");
"http://192.168.1.1/", . , Mdns, "http://esp8266/", android ! , , Mdns, ...
hadle ? http://192.168.1.1/, "/" "" handle, handle , . "http://192.168.1.1/end_setup" handle, .
handle:
Handle - -, - -
void handle_OnConnect() {// handle,
server.send(200, "text/html", SendHTML());
}
String SendHTML() {// HTML -
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> WIFI</h1>\n";
ptr += "<h3> wifi </h3>\n";
ptr += "<form action=\"/action_page\">";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_NAME\" value=\"_\">";
ptr += "<br>";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_password\" value=\"123456789\">";
ptr += "<br>";
ptr += " :<br>";
ptr += "<input type=\"text\" maxlength=\"14\" name=\"DEVICE_ID\" value=\"\"><br>";
ptr += "Local_ip:<br>";
ptr += "<input type=\"text\" name=\"LOCAL_IP\" value=\"192.168.0.250\">";
ptr += "<br>";
ptr += "Gateway:<br>";
ptr += "<input type=\"text\" name=\"GATEWAY\" value=\"192.168.0.1\">";
ptr += "<br>";
ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendHTML() {// HTML -
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> WIFI</h1>\n";
ptr += "<h3> wifi </h3>\n";
ptr += "<form action=\"/action_page\">";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_NAME\" value=\"_\">";
ptr += "<br>";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_password\" value=\"123456789\">";
ptr += "<br>";
ptr += " ( ):<br>";
ptr += "<input type=\"text\" name=\"TIMING\" value=\"1\">";
ptr += "<br>";
ptr += " :<br>";
ptr += "<input type=\"text\" maxlength=\"10\" name=\"DEVICE_ID\" value=\"datc_temp\"><br>";
ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
"" handle -
void handleForm() {// -
ssid = server.arg("WIFI_NAME");
password = server.arg("WIFI_password");
device_id = server.arg("DEVICE_ID");
local_ip_2.fromString(server.arg("LOCAL_IP"));
gateway_2.fromString(server.arg("GATEWAY"));
write_string_EEPROM(200, server.arg("LOCAL_IP"));
write_string_EEPROM(220, server.arg("GATEWAY"));
Serial.print("WIFI: ");
Serial.println(ssid);
Serial.print("password: ");
Serial.println(password);
server.send(200, "text/html", SendEndHTML()); //Send web page
}
void handleForm() {// -
ssid = server.arg("WIFI_NAME");
password = server.arg("WIFI_password");
device_id = server.arg("DEVICE_ID");
tmi = server.arg("TIMING").toInt();
Serial.print("WIFI: ");
Serial.println(ssid);
Serial.print("password: ");
Serial.println(password);
server.send(200, "text/html", SendEndHTML()); //Send web page
}
String SendEndHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<a href='/'><h1> </h1></a>\n";
ptr += "<a href='/end_setup'><h1></h1></a>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
" " , "" handle, EEPROM ssid WIFI, -
void handle_EndSetup() {
write_string_EEPROM(0, ssid);
write_string_EEPROM(20, password);
EEPROM.begin(256);
EEPROM.write(45, false);
setup_mode = false;
EEPROM.commit();
EEPROM.end();
WiFi.softAPdisconnect(true);
ESP.reset();
}
EEPROM. -
void write_string_EEPROM (int Addr, String Str) {// EEPROM
//Addr - , str - . 15
byte lng = Str.length();
EEPROM.begin (256);
EEPROM.write(Addr , lng);
unsigned char* buf = new unsigned char[15];
Str.getBytes(buf, lng + 1);
Addr++;
for (byte i = 0; i < lng; i++) {
EEPROM.write(Addr + i, buf[i]);
delay(10);
}
EEPROM.commit();
EEPROM.end();
}
char read_string_EEPROM (int Addr) {// EEPROM
//Addr -
EEPROM.begin(256);
byte lng = EEPROM.read(Addr);
char buf = new char[15];
Addr++;
for (byte i = 0; i < lng && i < 15; i++) buf[i] = char(EEPROM.read(i + Addr));
buf[lng] = '\x0';
EEPROM.end();
return buf;
}
, EEPROM FS.h ( ), .
( - )
, , .
SETUP, :
pinMode(RELEPIN, OUTPUT);
Serial.begin(115200);
EEPROM.begin(256);// EEPROM
setup_mode = EEPROM.read(45);// EERPOM
rele = EEPROM.read(60);// EERPOM
EEPROM.end();// EEPROM
if (rele) digitalWrite(RELEPIN, HIGH); else digitalWrite(RELEPIN, LOW);
local_ip_2.fromString(String(read_string_EEPROM(200)));// IP eeprom
gateway_2.fromString(String(read_string_EEPROM(220)));// eeprom
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
handle_ReturnSetup();
}
}
WiFi.config(local_ip_2, gateway_2, subnet_2);
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
read_auto();
server.on("/", handle_OnConnect_2);
server.on("/select_auto", handle_SelectAuto);
server.on("/rele_auto", handle_ReleAuto);
server.on("/delete_page", handle_Delete);
server.on("/return_setup", handle_ReturnSetup);
server.on("/control_menu", handle_ControlMenu);
server.on("/rele_off", handle_ReleOff);
server.on("/rele_on", handle_ReleOn);
server.on("/auto_off", handle_AutoOff);
server.on("/auto_on", handle_AutoOn);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
client.subscribe("/datk"); //
//client.subscribe("/cmd"); //
client.set_callback(callback);
} else {
Serial.println("Could not connect to MQTT server");
}
MQTT:
void perek(bool per) { //per = true = , per = true =
EEPROM.begin(256);
if (auto_mode) {
if (per) {
rele = 0;
digitalWrite(RELEPIN, LOW);
}
else {
rele = 1;
digitalWrite(RELEPIN, HIGH);
}
}
else {
if (per) {
rele = 1;
digitalWrite(RELEPIN, HIGH);
}
else {
rele = 0;
digitalWrite(RELEPIN, LOW);
}
}
EEPROM.write(60, rele);
EEPROM.commit();
EEPROM.end();
}
void callback(const MQTT::Publish& pub)
{
Serial.print(pub.topic()); //
Serial.print(" => ");
Serial.print(pub.payload_string()); //
String payload = pub.payload_string();
if (String(pub.topic()) == "/datk") //
{
String tmpstr = "", namest, znachs;
int datat;
int mod = 0;
bool tr = false;
for (int i = 0 ; i < payload.length(); i++) {
if (payload[i] != '#') tmpstr += payload[i]; else {
switch (mod) {
case 0:
namest = tmpstr;
break;
case 1:
datat = tmpstr.toInt();
break;
case 2:
znachs = tmpstr;
break;
}
tmpstr = "";
mod++;
}
}
for (int i = 0; i < lng && !tr; i++) {
if (names[i] == namest) {
data[i] = datat;
tr = true;
}
}
if (!tr) {
lng++;
names[lng - 1] = namest;
data[lng - 1] = datat;
znach[lng - 1] = znachs;
}
if (auto_stat && namest == auto_name) {
switch (auto_oper) {
case 0:
if (auto_data > datat) perek(true);
if (auto_data < datat) perek(false);
break;
case 1:
if (auto_data < datat) perek(true);
if (auto_data > datat) perek(false);
break;
case 2:
if (auto_data = datat) perek(true);
if (auto_data != datat) perek(false);
break;
}
}
}
"Perek" .
WIFI, WIFI, .
handle:
void handle_OnConnect_2() {
server.send(200, "text/html", SendMenuHTML());
}
String SendMenuHTML() {
String ptr = "<!DOCTYPE html> <html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>WIFI </h1>\n";
ptr += "<h3> :</h3>\n";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/return_setup'\" /><br>";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/control_menu'\" /><br>";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_auto'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
:
" ", , .
" " .
" " .
handle:
void handle_ReturnSetup() {
EEPROM.begin(256);
EEPROM.write(45, true);
setup_mode = true;
EEPROM.commit();
EEPROM.end();
ESP.reset();
}
handle ( handle handle!):
void handle_ControlMenu() {
server.send(200, "text/html", SendControlMenuHTML());
}
String SendControlMenuHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px; } h3 {color: #444444;margin-bottom: 50px;} h4 {color: #444444;margin-bottom: 20px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> </h1>\n";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/'\" /><br>";
if (rele) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_off'\" /><br>";
}
if (!rele) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_on'\" /><br>";
}
ptr += "<h3>:</h3>\n";
//ptr += "<h4>:</h4>\n";
for (int i = 0 ; i < lng; i++) {
ptr += "<h4>" + names[i] + " : " + data[i] + " " + znach[i] + "</h4>";
}
ptr += "<form action=\"/delete_page\">";
ptr += ": ";
ptr += "<input type=\"text\" name=\"DELETE_NAME\" value=\"\"> ";
//ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "<br><input type=\"button\" value=\"\" onClick=\"document.location = '/control_menu'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
-. , /, , handle:
void handle_ReleOff() {//
rele = 0;
digitalWrite(RELEPIN, LOW);
EEPROM.begin(256);
EEPROM.write(60, 0);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlMenuHTML());
}
void handle_ReleOn() {//
rele = 1;
digitalWrite(RELEPIN, HIGH );
EEPROM.begin(256);
EEPROM.write(60, 1);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlMenuHTML());
}
, . , "".
handle:
void handle_Delete() {
String delname = server.arg("DELETE_NAME");
bool tr = false;
for (int i = 0 ; i < lng; i++) {
if (tr) {
names[i - 1] = names[i];
data[i - 1] = data[i];
znach[i - 1] = znach[i];
}
else if (names[i] == delname) {
tr = true;
}
}
if (tr) lng--;
server.send(200, "text/html", SendControlMenuHTML());
}
, 1 , .
.
handle, - :
void handle_ReleAuto() {
server.send(200, "text/html", SendControlReleAutoHTML());
}
String SendControlReleAutoHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px; } h3 {color: #444444;margin-bottom: 50px;} h4 {color: #444444;margin-bottom: 20px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> </h1>";
if (auto_stat) {
ptr += "<h3> </h3>";
}
else {
ptr += "<h3> </h3>";
}
ptr += "<h3> :" + auto_name + " ";
switch (auto_oper) {
case 0 : ptr += "> ";
break;
case 1 : ptr += "< ";
break;
case 2 : ptr += "= ";
break;
}
if (auto_mode)
ptr += String(auto_data) + " </h3>";
else
ptr += String(auto_data) + " </h3>";
ptr += "<h3> :</h3>\n";
ptr += "<form action=\"/select_auto\">";
ptr += "<select size=\"1\" name=\"DAT_NAME\">";
for (int i = 0 ; i < lng; i++) {
ptr += "<option value=\"" + String(i) + "\">" + names[i] + "</option>";
}
ptr += "</select> ";
ptr += "<select size=\"1\" name=\"OPER\">";
ptr += "<option value=0>></option>";
ptr += "<option value=1><</option>";
ptr += "<option value=2>=</option>";
ptr += "</select> ";
ptr += "</select> ";
ptr += "<input type=\"text\" name=\"DATA_P\" value=\"10\">";
ptr += "<select size=\"1\" name=\"ON_OFF\">";
ptr += "<option value=0></option>";
ptr += "<option value=1></option>";
ptr += "</select> ";
//ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "<br><input type=\"button\" value=\"\" onClick=\"document.location = '/rele_auto'\" /><br>";
if (auto_stat) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/auto_off'\" /><br>";
}
else {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/auto_on'\" /><br>";
}
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
:
String auto_name = "example";
int auto_data = 10;
int auto_oper = 0;
bool auto_stat = false;// false - , true -
bool auto_mode = false; //false - , true -
String names[10];//
String znach[10];//
int data[10];//
int lng = 0;//
-, . , . . , , , "". "" .
handle:
void handle_SelectAuto() {
auto_name = names[server.arg("DAT_NAME").toInt()];
auto_oper = server.arg("OPER").toInt();
auto_data = server.arg("DATA_P").toInt();
auto_mode = server.arg("ON_OFF").toInt();
write_string_EEPROM(70, auto_name);
write_string_EEPROM(110, server.arg("OPER"));
write_string_EEPROM(90, server.arg("DATA_P"));
EEPROM.begin(256);
EEPROM.write(130, auto_mode);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlReleAutoHTML());
}
/ / :
void handle_AutoOff() {//
auto_stat = 0;
server.send(200, "text/html", SendControlReleAutoHTML());
}
void handle_AutoOn() {//
auto_stat = 1;
server.send(200, "text/html", SendControlReleAutoHTML());
}
.
( - )
, .
, , WIFI MQTT.
SETUP, DHT11:
#include "DHT.h"// DHT
#define DHTTYPE DHT11
uint8_t DHTPin = 5;
int tempC = 0;
int humC = 0;
int tmi = 10;//
DHT dht(DHTPin, DHTTYPE);
void setup() {
pinMode(DHTPin, INPUT);
Serial.begin(115200);
EEPROM.begin(256);
setup_mode = EEPROM.read(85);
EEPROM.end();
device_id = read_string_EEPROM(40);
if (!setup_mode) {//
dht.begin();
tmi = String(read_string_EEPROM(200)).toInt(); //
local_ip.fromString(String(read_string_EEPROM(200)));
gateway.fromString(String(read_string_EEPROM(220)));
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
ReturnSetup();
}
}
Serial.println("WiFi connected");
tempC = (int)dht.readTemperature(); //
humC = (int)dht.readHumidity(); //
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
} else {
Serial.println("Could not connect to MQTT server");
}
client.publish("/datk", device_id+"_t" + "#" + String(tempC) + "##");
client.publish("/datk", device_id+"_hm" + "#" + String(humC) + "##");
delay(100);
ESP.deepSleep(tmi * 1000000);//
}
else {//
delay(1000);
WiFi.softAP(ssid_ap, password_ap);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/end_setup", handle_EndSetup);
server.on("/action_page", handleForm);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
}
SETUP ds18b20:
#include <DallasTemperature.h>// ds18b20
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
char temperatureCString[7];
int tempC = 0;
int tmi = 10;//
void getTemperature()//
{
do {
DS18B20.requestTemperatures();
tempC = DS18B20.getTempCByIndex(0);
dtostrf(tempC, 2, 2, temperatureCString);
delay(100);
} while (tempC == 85.0 || tempC == (-127.0));
}
void setup() {
Serial.begin(115200);
EEPROM.begin(256);
setup_mode = EEPROM.read(85);
EEPROM.end();
device_id = read_string_EEPROM(40);
if (!setup_mode) {//
DS18B20.begin();
tmi = String(read_string_EEPROM(200)).toInt(); //
local_ip.fromString(String(read_string_EEPROM(200)));
gateway.fromString(String(read_string_EEPROM(220)));
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
ReturnSetup();
}
}
getTemperature();//
Serial.println("WiFi connected");
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
} else {
Serial.println("Could not connect to MQTT server");
}
client.publish("/datk", device_id + "#" + String(tempC) + "##");;
delay(100);
ESP.deepSleep(tmi * 1000000);//
}
else {//
delay(1000);
WiFi.softAP(ssid_ap, password_ap);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/end_setup", handle_EndSetup);
server.on("/action_page", handleForm);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
}
, - , WIFI 1 , . "" ESP , RTC , , , RTC "Reset", , .
In principle, at a stretch, it can be called a "smart home system". Of course, it works, but there are many nuances, flaws ... As an initial project, I am satisfied, I will refine it, for example, I will add support for several modules - sockets, the conditions and normal use of MQTT capabilities are also more complex. Also, global refactoring of the code will not hurt ...
Listings and files
Listing of the program for the module - sockets
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <PubSubClient.h>
#define RELEPIN 4
const char *ssid_ap = "Rozetka_Setup"; //
const char *password_ap = "12345678"; //
const char *ID = "rele_1";
String ssid = ""; // wifi
String password = ""; // wifi
String device_id; // ID
bool setup_mode; // true - , false -
bool rele;
String auto_name = "example";
int auto_data = 10;
int auto_oper = 0;
bool auto_stat = false;// false - , true -
bool auto_mode = false; //false - , true -
const char *mqtt_server = "M5.WQTT.RU"; // MQTT
const int mqtt_port = 2602; // MQTT
const char *mqtt_user = "u_RSELYN"; // a
const char *mqtt_pass = "bhbtIJue"; //
String names[10];//
String znach[10];//
int data[10];//
int lng = 0;//
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress local_ip_2(192, 168, 0, 250);
IPAddress gateway_2(192, 168, 0, 1);
IPAddress subnet_2(255, 255, 255, 0);
ESP8266WebServer server(80);// server
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server, mqtt_port);
void write_string_EEPROM (int Addr, String Str) {
byte lng = Str.length();
EEPROM.begin (256);
EEPROM.write(Addr , lng);
unsigned char* buf = new unsigned char[15];
Str.getBytes(buf, lng + 1);
Addr++;
for (byte i = 0; i < lng; i++) {
EEPROM.write(Addr + i, buf[i]);
delay(10);
}
EEPROM.commit();
EEPROM.end();
}
char *read_string_EEPROM (int Addr) {
EEPROM.begin(256);
byte lng = EEPROM.read(Addr);
char* buf = new char[15];
Addr++;
for (byte i = 0; i < lng && i < 15; i++) buf[i] = char(EEPROM.read(i + Addr));
buf[lng] = '\x0';
EEPROM.end();
return buf;
}
void setup() {
pinMode(RELEPIN, OUTPUT);
Serial.begin(115200);
EEPROM.begin(256);
setup_mode = EEPROM.read(45);// EERPOM
rele = EEPROM.read(60);// EERPOM
EEPROM.end();
if (rele) digitalWrite(RELEPIN, HIGH); else digitalWrite(RELEPIN, LOW);
if (!setup_mode) { //
local_ip_2.fromString(String(read_string_EEPROM(200)));// IP eeprom
gateway_2.fromString(String(read_string_EEPROM(220)));// eeprom
//Serial.println(read_string_EEPROM(0));
//Serial.println(read_string_EEPROM(20));
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
handle_ReturnSetup();
}
}
WiFi.config(local_ip_2, gateway_2, subnet_2);
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
read_auto();
server.on("/", handle_OnConnect_2);
server.on("/select_auto", handle_SelectAuto);
server.on("/rele_auto", handle_ReleAuto);
server.on("/delete_page", handle_Delete);
server.on("/return_setup", handle_ReturnSetup);
server.on("/control_menu", handle_ControlMenu);
server.on("/rele_off", handle_ReleOff);
server.on("/rele_on", handle_ReleOn);
server.on("/auto_off", handle_AutoOff);
server.on("/auto_on", handle_AutoOn);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
client.subscribe("/datk"); //
//client.subscribe("/cmd"); //
client.set_callback(callback);
} else {
Serial.println("Could not connect to MQTT server");
}
}
else {//
delay(1000);
WiFi.softAP(ssid_ap, password_ap);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/end_setup", handle_EndSetup);
server.on("/action_page", handleForm);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
}
void read_auto() {
EEPROM.begin(256);
auto_mode = EEPROM.read(130);
EEPROM.end();
auto_data = String(read_string_EEPROM(90)).toInt();
Serial.println(read_string_EEPROM(90));
auto_oper = String(read_string_EEPROM(110)).toInt();
auto_name = read_string_EEPROM(70);
}
void perek(bool per) { //per = true = , per = true =
EEPROM.begin(256);
if (auto_mode) {
if (per) {
rele = 0;
digitalWrite(RELEPIN, LOW);
}
else {
rele = 1;
digitalWrite(RELEPIN, HIGH);
}
}
else {
if (per) {
rele = 1;
digitalWrite(RELEPIN, HIGH);
}
else {
rele = 0;
digitalWrite(RELEPIN, LOW);
}
}
EEPROM.write(60, rele);
EEPROM.commit();
EEPROM.end();
}
void loop() {
server.handleClient();
if (client.connected()) {
client.loop();
}
}
void callback(const MQTT::Publish& pub)
{
Serial.print(pub.topic()); //
Serial.print(" => ");
Serial.print(pub.payload_string()); //
String payload = pub.payload_string();
if (String(pub.topic()) == "/datk") //
{
String tmpstr = "", namest, znachs;
int datat;
int mod = 0;
bool tr = false;
for (int i = 0 ; i < payload.length(); i++) {
if (payload[i] != '#') tmpstr += payload[i]; else {
switch (mod) {
case 0:
namest = tmpstr;
break;
case 1:
datat = tmpstr.toInt();
break;
case 2:
znachs = tmpstr;
break;
}
tmpstr = "";
mod++;
}
}
for (int i = 0; i < lng && !tr; i++) {
if (names[i] == namest) {
data[i] = datat;
tr = true;
}
}
if (!tr) {
lng++;
names[lng - 1] = namest;
data[lng - 1] = datat;
znach[lng - 1] = znachs;
}
if (auto_stat && namest == auto_name) {
switch (auto_oper) {
case 0:
if (auto_data > datat) perek(true);
if (auto_data < datat) perek(false);
break;
case 1:
if (auto_data < datat) perek(true);
if (auto_data > datat) perek(false);
break;
case 2:
if (auto_data = datat) perek(true);
if (auto_data != datat) perek(false);
break;
}
}
}
/*if (String(pub.topic()) == "/md"){
if(pub.payload_string()=="ON")
{
rele = 1;
digitalWrite(RELEPIN, HIGH );
EEPROM.begin(256);
EEPROM.write(60, 1);
EEPROM.commit();
EEPROM.end();
}
else
{
rele = 0;
digitalWrite(RELEPIN, LOW );
EEPROM.begin(256);
EEPROM.write(60, 0);
EEPROM.commit();
EEPROM.end();
}
}*/
}
void handle_ReturnSetup() {
EEPROM.begin(256);
EEPROM.write(45, true);
setup_mode = true;
EEPROM.commit();
EEPROM.end();
ESP.reset();
}
void handle_Delete() {
String delname = server.arg("DELETE_NAME");
bool tr = false;
for (int i = 0 ; i < lng; i++) {
if (tr) {
names[i - 1] = names[i];
data[i - 1] = data[i];
znach[i - 1] = znach[i];
}
else if (names[i] == delname) {
tr = true;
}
}
if (tr) lng--;
server.send(200, "text/html", SendControlMenuHTML());
}
void handle_ReleAuto() {
server.send(200, "text/html", SendControlReleAutoHTML());
}
void handle_SelectAuto() {
auto_name = names[server.arg("DAT_NAME").toInt()];
auto_oper = server.arg("OPER").toInt();
auto_data = server.arg("DATA_P").toInt();
auto_mode = server.arg("ON_OFF").toInt();
Serial.println(auto_name);
Serial.println(auto_oper);
Serial.println(auto_data);
Serial.println(auto_mode);
write_string_EEPROM(70, auto_name);
write_string_EEPROM(110, server.arg("OPER"));
write_string_EEPROM(90, server.arg("DATA_P"));
EEPROM.begin(256);
EEPROM.write(130, auto_mode);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlReleAutoHTML());
}
void handle_AutoOff() {
auto_stat = 0;
server.send(200, "text/html", SendControlReleAutoHTML());
}
void handle_AutoOn() {
auto_stat = 1;
server.send(200, "text/html", SendControlReleAutoHTML());
}
void handle_ReleOff() {
rele = 0;
digitalWrite(RELEPIN, LOW);
EEPROM.begin(256);
EEPROM.write(60, 0);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlMenuHTML());
}
void handle_ReleOn() {
rele = 1;
digitalWrite(RELEPIN, HIGH );
EEPROM.begin(256);
EEPROM.write(60, 1);
EEPROM.commit();
EEPROM.end();
server.send(200, "text/html", SendControlMenuHTML());
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML());
}
void handle_OnConnect_2() {
server.send(200, "text/html", SendMenuHTML());
}
void handleForm() {
ssid = server.arg("WIFI_NAME");
password = server.arg("WIFI_password");
device_id = server.arg("DEVICE_ID");
local_ip_2.fromString(server.arg("LOCAL_IP"));
gateway_2.fromString(server.arg("GATEWAY"));
write_string_EEPROM(200, server.arg("LOCAL_IP"));
write_string_EEPROM(220, server.arg("GATEWAY"));
Serial.print("WIFI: ");
Serial.println(ssid);
Serial.print("password: ");
Serial.println(password);
server.send(200, "text/html", SendEndHTML()); //Send web page
}
void handle_EndSetup() {
write_string_EEPROM(0, ssid);
write_string_EEPROM(20, password);
EEPROM.begin(256);
EEPROM.write(45, false);
setup_mode = false;
EEPROM.commit();
EEPROM.end();
WiFi.softAPdisconnect(true);
ESP.reset();
}
void handle_ControlMenu() {
server.send(200, "text/html", SendControlMenuHTML());
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String SendEndHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<a href='/'><h1> </h1></a>\n";
ptr += "<a href='/end_setup'><h1></h1></a>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> WIFI</h1>\n";
ptr += "<h3> wifi </h3>\n";
ptr += "<form action=\"/action_page\">";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_NAME\" value=\"_\">";
ptr += "<br>";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_password\" value=\"123456789\">";
ptr += "<br>";
ptr += " :<br>";
ptr += "<input type=\"text\" maxlength=\"14\" name=\"DEVICE_ID\" value=\"\"><br>";
ptr += "Local_ip:<br>";
ptr += "<input type=\"text\" name=\"LOCAL_IP\" value=\"192.168.0.250\">";
ptr += "<br>";
ptr += "Gateway:<br>";
ptr += "<input type=\"text\" name=\"GATEWAY\" value=\"192.168.0.1\">";
ptr += "<br>";
ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendMenuHTML() {
String ptr = "<!DOCTYPE html> <html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1>WIFI </h1>\n";
ptr += "<h3> :</h3>\n";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/return_setup'\" /><br>";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/control_menu'\" /><br>";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_auto'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendControlMenuHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px; } h3 {color: #444444;margin-bottom: 50px;} h4 {color: #444444;margin-bottom: 20px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> </h1>\n";
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/'\" /><br>";
if (rele) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_off'\" /><br>";
}
if (!rele) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/rele_on'\" /><br>";
}
ptr += "<h3>:</h3>\n";
//ptr += "<h4>:</h4>\n";
for (int i = 0 ; i < lng; i++) {
ptr += "<h4>" + names[i] + " : " + data[i] + " " + znach[i] + "</h4>";
}
ptr += "<form action=\"/delete_page\">";
ptr += ": ";
ptr += "<input type=\"text\" name=\"DELETE_NAME\" value=\"\"> ";
//ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "<br><input type=\"button\" value=\"\" onClick=\"document.location = '/control_menu'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendControlReleAutoHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px; } h3 {color: #444444;margin-bottom: 50px;} h4 {color: #444444;margin-bottom: 20px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> </h1>";
if (auto_stat) {
ptr += "<h3> </h3>";
}
else {
ptr += "<h3> </h3>";
}
ptr += "<h3> :" + auto_name + " ";
switch (auto_oper) {
case 0 : ptr += "> ";
break;
case 1 : ptr += "< ";
break;
case 2 : ptr += "= ";
break;
}
if (auto_mode)
ptr += String(auto_data) + " </h3>";
else
ptr += String(auto_data) + " </h3>";
ptr += "<h3> :</h3>\n";
ptr += "<form action=\"/select_auto\">";
ptr += "<select size=\"1\" name=\"DAT_NAME\">";
for (int i = 0 ; i < lng; i++) {
ptr += "<option value=\"" + String(i) + "\">" + names[i] + "</option>";
}
ptr += "</select> ";
ptr += "<select size=\"1\" name=\"OPER\">";
ptr += "<option value=0>></option>";
ptr += "<option value=1><</option>";
ptr += "<option value=2>=</option>";
ptr += "</select> ";
ptr += "</select> ";
ptr += "<input type=\"text\" name=\"DATA_P\" value=\"10\">";
ptr += "<select size=\"1\" name=\"ON_OFF\">";
ptr += "<option value=0></option>";
ptr += "<option value=1></option>";
ptr += "</select> ";
//ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "<br><input type=\"button\" value=\"\" onClick=\"document.location = '/rele_auto'\" /><br>";
if (auto_stat) {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/auto_off'\" /><br>";
}
else {
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/auto_on'\" /><br>";
}
ptr += "<br><input type=\"button\" value=\" \" onClick=\"document.location = '/'\" /><br>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
Listing of the program for the module - DHT11 sensor
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTTYPE DHT11
uint8_t DHTPin = 5;
int tempC = 0;
int humC = 0;
int tmi = 10;
DHT dht(DHTPin, DHTTYPE);
const char *ssid_ap = "Datchik_Setup"; //
const char *password_ap = "12345678"; //
const char *ID = "temp_1";
String ssid = ""; // wifi
String password = ""; // wifi
String device_id; // ID
bool setup_mode ; // true - , false -
const char *mqtt_server = "M5.WQTT.RU"; // MQTT
const int mqtt_port = 2602; // MQTT
const char *mqtt_user = "u_RSELYN"; // a
const char *mqtt_pass = "bhbtIJue"; //
ESP8266WebServer server(80);// server
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server, mqtt_port);
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void write_string_EEPROM (int Addr, String Str) {
byte lng = Str.length();
EEPROM.begin (256);
EEPROM.write(Addr , lng);
unsigned char* buf = new unsigned char[15];
Str.getBytes(buf, lng + 1);
Addr++;
for (byte i = 0; i < lng; i++) {
EEPROM.write(Addr + i, buf[i]);
delay(10);
}
EEPROM.commit();
EEPROM.end();
}
char *read_string_EEPROM (int Addr) {
EEPROM.begin(256);
byte lng = EEPROM.read(Addr);
char* buf = new char[15];
Addr++;
for (byte i = 0; i < lng && i < 15; i++) buf[i] = char(EEPROM.read(i + Addr));
buf[lng] = '\x0';
EEPROM.end();
return buf;
}
void setup() {
pinMode(DHTPin, INPUT);
Serial.begin(115200);
EEPROM.begin(256);
setup_mode = EEPROM.read(85);
EEPROM.end();
device_id = read_string_EEPROM(40);
if (!setup_mode) {
dht.begin();
tmi = String(read_string_EEPROM(200)).toInt();
local_ip.fromString(String(read_string_EEPROM(200)));
gateway.fromString(String(read_string_EEPROM(220)));
Serial.println(read_string_EEPROM(0));
Serial.println(read_string_EEPROM(20));
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
ReturnSetup();
}
}
Serial.println("WiFi connected");
tempC = (int)dht.readTemperature();
humC = (int)dht.readHumidity();
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
} else {
Serial.println("Could not connect to MQTT server");
}
client.publish("/datk", device_id+"_t" + "#" + String(tempC) + "##");
client.publish("/datk", device_id+"_hm" + "#" + String(humC) + "##");
delay(100);
ESP.deepSleep(tmi * 1000000);
}
else {
delay(1000);
WiFi.softAP(ssid_ap, password_ap);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/end_setup", handle_EndSetup);
server.on("/action_page", handleForm);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
}
void loop() {
if (setup_mode)
server.handleClient();
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML());
}
void ReturnSetup() {
EEPROM.begin(256);
EEPROM.write(85, true);
setup_mode = true;
EEPROM.commit();
EEPROM.end();
ESP.reset();
}
void handleForm() {
ssid = server.arg("WIFI_NAME");
password = server.arg("WIFI_password");
device_id = server.arg("DEVICE_ID");
tmi = server.arg("TIMING").toInt();
Serial.print("WIFI: ");
Serial.println(ssid);
Serial.print("password: ");
Serial.println(password);
server.send(200, "text/html", SendEndHTML()); //Send web page
}
void handle_EndSetup() {
write_string_EEPROM(0, ssid);
write_string_EEPROM(20, password);
write_string_EEPROM(40, device_id);
write_string_EEPROM(200, String(tmi));
EEPROM.begin(256);
EEPROM.write(85, false);
setup_mode = false;
EEPROM.commit();
EEPROM.end();
WiFi.softAPdisconnect(true);
ESP.reset();
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String SendEndHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<a href='/'><h1> </h1></a>\n";
ptr += "<a href='/end_setup'><h1></h1></a>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> WIFI</h1>\n";
ptr += "<h3> wifi </h3>\n";
ptr += "<form action=\"/action_page\">";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_NAME\" value=\"_\">";
ptr += "<br>";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_password\" value=\"123456789\">";
ptr += "<br>";
ptr += " ( ):<br>";
ptr += "<input type=\"text\" name=\"TIMING\" value=\"1\">";
ptr += "<br>";
ptr += " :<br>";
ptr += "<input type=\"text\" maxlength=\"10\" name=\"DEVICE_ID\" value=\"datc_temp\"><br>";
ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
Listing of the program for the module - sensor ds18b20
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
char temperatureCString[7];
int tempC = 0;
int tmi = 10;
const char *ssid_ap = "Datchik_Setup"; //
const char *password_ap = "12345678"; //
const char *ID = "temp_1";
String ssid = ""; // wifi
String password = ""; // wifi
String device_id; // ID
bool setup_mode ; // true - , false -
const char *mqtt_server = "M5.WQTT.RU"; // MQTT
const int mqtt_port = 2602; // MQTT
const char *mqtt_user = "u_RSELYN"; // a
const char *mqtt_pass = "bhbtIJue"; //
ESP8266WebServer server(80);// server
WiFiClient wclient;
PubSubClient client(wclient, mqtt_server, mqtt_port);
IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
void write_string_EEPROM (int Addr, String Str) {
byte lng = Str.length();
EEPROM.begin (256);
EEPROM.write(Addr , lng);
unsigned char* buf = new unsigned char[15];
Str.getBytes(buf, lng + 1);
Addr++;
for (byte i = 0; i < lng; i++) {
EEPROM.write(Addr + i, buf[i]);
delay(10);
}
EEPROM.commit();
EEPROM.end();
}
char *read_string_EEPROM (int Addr) {
EEPROM.begin(256);
byte lng = EEPROM.read(Addr);
char* buf = new char[15];
Addr++;
for (byte i = 0; i < lng && i < 15; i++) buf[i] = char(EEPROM.read(i + Addr));
buf[lng] = '\x0';
EEPROM.end();
return buf;
}
void getTemperature()
{
do {
DS18B20.requestTemperatures();
tempC = DS18B20.getTempCByIndex(0);
dtostrf(tempC, 2, 2, temperatureCString);
delay(100);
} while (tempC == 85.0 || tempC == (-127.0));
}
void setup() {
Serial.begin(115200);
EEPROM.begin(256);
setup_mode = EEPROM.read(85);
EEPROM.end();
device_id = read_string_EEPROM(40);
if (!setup_mode) {
tmi = String(read_string_EEPROM(200)).toInt();
local_ip.fromString(String(read_string_EEPROM(200)));
gateway.fromString(String(read_string_EEPROM(220)));
DS18B20.begin();
Serial.println(read_string_EEPROM(0));
Serial.println(read_string_EEPROM(20));
WiFi.begin(read_string_EEPROM(0), read_string_EEPROM(20));
int sm = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500); sm++;
Serial.print(".");
if (sm > 120) {
ReturnSetup();
}
}
getTemperature();
Serial.println("WiFi connected");
if (client.connect(MQTT::Connect(ID).set_auth(mqtt_user, mqtt_pass))) {
Serial.println("Connected to MQTT server");
} else {
Serial.println("Could not connect to MQTT server");
}
client.publish("/datk", device_id + "#" + String(tempC) + "##");
delay(100);
ESP.deepSleep(tmi * 1000000);
}
else {
delay(1000);
WiFi.softAP(ssid_ap, password_ap);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
server.on("/", handle_OnConnect);
server.on("/end_setup", handle_EndSetup);
server.on("/action_page", handleForm);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
}
void loop() {
if (setup_mode)
server.handleClient();
}
void handle_OnConnect() {
server.send(200, "text/html", SendHTML());
}
void ReturnSetup() {
EEPROM.begin(256);
EEPROM.write(85, true);
setup_mode = true;
EEPROM.commit();
EEPROM.end();
ESP.reset();
}
void handleForm() {
ssid = server.arg("WIFI_NAME");
password = server.arg("WIFI_password");
device_id = server.arg("DEVICE_ID");
tmi = server.arg("TIMING").toInt();
Serial.print("WIFI: ");
Serial.println(ssid);
Serial.print("password: ");
Serial.println(password);
server.send(200, "text/html", SendEndHTML()); //Send web page
}
void handle_EndSetup() {
write_string_EEPROM(0, ssid);
write_string_EEPROM(20, password);
write_string_EEPROM(40, device_id);
write_string_EEPROM(200, String(tmi));
EEPROM.begin(256);
EEPROM.write(85, false);
setup_mode = false;
EEPROM.commit();
EEPROM.end();
WiFi.softAPdisconnect(true);
ESP.reset();
}
void handle_NotFound() {
server.send(404, "text/plain", "Not found");
}
String SendEndHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/><html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<a href='/'><h1> </h1></a>\n";
ptr += "<a href='/end_setup'><h1></h1></a>\n";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}
String SendHTML() {
String ptr = "<!DOCTYPE html><html lang='ru-RU'><head><meta charset='UTF-8'/> <html>\n";
ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
ptr += "<title>WIFI Control</title>\n";
ptr += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr += ".button {display: block;width: 80px;background-color: #1abc9c;border: none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr += ".button-on {background-color: #1abc9c;}\n";
ptr += ".button-on:active {background-color: #16a085;}\n";
ptr += ".button-off {background-color: #34495e;}\n";
ptr += ".button-off:active {background-color: #2c3e50;}\n";
ptr += "p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr += "</style>\n";
ptr += "</head>\n";
ptr += "<body>\n";
ptr += "<h1> WIFI</h1>\n";
ptr += "<h3> wifi </h3>\n";
ptr += "<form action=\"/action_page\">";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_NAME\" value=\"_\">";
ptr += "<br>";
ptr += ":<br>";
ptr += "<input type=\"text\" name=\"WIFI_password\" value=\"123456789\">";
ptr += "<br>";
ptr += " ( ):<br>";
ptr += "<input type=\"text\" name=\"TIMING\" value=\"1\">";
ptr += "<br>";
ptr += " :<br>";
ptr += "<input type=\"text\" maxlength=\"14\" name=\"DEVICE_ID\" value=\"datc_temp\"><br>";
ptr += "<br><br>";
ptr += "<input type=\"submit\" value=\"\">";
ptr += "</form>";
ptr += "</body>\n";
ptr += "</html>\n";
return ptr;
}