Skip to content

Instantly share code, notes, and snippets.

@sontqq
Created December 15, 2019 05:42
Show Gist options
  • Save sontqq/c8fd6e861f3866524394a0ffe17d0cab to your computer and use it in GitHub Desktop.
Save sontqq/c8fd6e861f3866524394a0ffe17d0cab to your computer and use it in GitHub Desktop.
esp8266 relay code for smart plug with eveng logging to php/mysql
const char MAIN_page[] PROGMEM = R"=====(
<html>
<head>
<title>SONT RELAY CONTROL PAGE</title>
<style>
@media only screen and (max-device-width: 480px) {
//.zoom { zoom: 150%; position: fixed; }
}
small{
font-size: 16px;
}
.kozep{
top: 50%;
left: 50%;
}
input{
zoom: 200%;
}
</style>
</head>
<body>
<div id="kozep">
<center>
<a style="font-weight: bold; font-size: 48px;" href="http://sont.sytes.net:81/">HOME</a>
<hr>
Relay status: <b>%d</b>
<br>
<form action="/webcommand" method=get>
<input class="" type="submit" value="TOGGLE"/>
</form>
<div id="since">
Since boot: %ch %ym %xs
</div><br>
Nearby WiFi: %a (%g ms)<i>(disabled)</i>
<hr>
<i>
<a href="http://sont.sytes.net:81/?relay=on">http://sont.sytes.net:81/?relay=on</a>
<br>
<a href="http://sont.sytes.net:81/?relay=off">http://sont.sytes.net:81/?relay=off</a>
<br>
<a href="http://sont.sytes.net:81/?relay=toggle">http://sont.sytes.net:81/?relay=toggle</a>
</i>
<hr>
<form action="/drop" method=get>
<input class="" type="submit" value="DROP TABLE"/>
</form>
<form action="/" method=get>
<input class="" type="submit" value="RESTART"/>
</form>
<h3>Events:</h3>
<br>
<div id="small">
<div style="text-align: center;">
<div style="display: inline-block; text-align: left;">
%h
</div>
</div>
</div>
</center>
</div>
</body>
</html>
)=====";
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <QueueList.h>
#include <ESP8266HTTPClient.h>
#include "index.h"
int COM = 5;
int VCC = 3;
int LED = 2;
int BTN = 0;
ESP8266WebServer server(81);
IPAddress apIP(10, 10, 10, 1);
WiFiEventHandler WifiDcHandler;
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
void setup() {
Serial.begin(9600);
pinMode(COM, OUTPUT);
pinMode(VCC, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT);
attachInterrupt(0, flashbutton, FALLING);
WiFi.mode(WIFI_AP_STA);
delay(100);
WiFi.disconnect();
wifi_station_set_hostname("sontrelay");
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP("SONTRELAY", "ezajelszo");
digitalWrite(VCC,HIGH);
int trycount = 0;
while (WiFi.status() != WL_CONNECTED) {
trycount++;
int m = millis();
String prefix = "["+(String)m+"]";
prefix+="["+(String)trycount+"]";
Serial.print(prefix);
Serial.println("Connecting to WiFi");
digitalWrite(LED, !digitalRead(LED));
delay(1000);
if(trycount == 3600){
Serial.print("["+(String)m+"]");
Serial.println("Max try count reached (3600). Resetting.");
pinMode(D0, OUTPUT);
digitalWrite(D0, LOW);
delay(100);
ESP.reset();
ESP.restart();
}
}
WifiDcHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);
stationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);
Serial.println();
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("AP IP: ");
Serial.println(WiFi.softAPIP());
server.on("/", indexReq);
server.on("/drop", dropReq);
server.on("/index.htm", indexReq);
server.on("/index.html", indexReq);
server.on("/index.php", indexReq);
server.on("/webcommand", webcommand);
server.begin();
server.onNotFound([]() {
server.send(200, "text/html", "404");
});
Serial.println("SETUP DONE");
digitalWrite(COM,LOW);
if (!MDNS.begin("sontrelay")){
Serial.println("DNS Error");
}
uploaddb("BOOTED");
}
void loop() {
server.handleClient();
if(digitalRead(COM)==0){
digitalWrite(LED,0);
}
}
void uploaddb(String event){
HTTPClient http;
String path = "http://sont.sytes.net/relay.php?event=";
String url = path + event;
http.begin(url);
http.setReuse(true);
http.addHeader("Content-type", "application/x-www-form-urlencoded");
int httpCode = http.GET();
String response = "tempdata";
if(httpCode >= 200){
response = http.getString();
}
http.end();
}
String getdb(){
HTTPClient http;
String response = "tempdata";
String path = "http://sont.sytes.net/relay_show.php";
http.begin(path);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
response = http.getString();
}
else{
Serial.print("Error. Return code: ");
Serial.println(httpCode);
}
http.end();
return response;
}
void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {
uploaddb("APCONNECTED");
}
void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {
uploaddb("APDISCONNECTED");
}
String macToString(const unsigned char* mac) {
char buf[20];
snprintf(buf, sizeof(buf), "%02x-%02x-%02x-%02x-%02x-%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buf);
}
void indexReq(){
String database = getdb();
String addy = server.client().remoteIP().toString();
String stamp;
int m = millis();
if(server.arg("relay") == "toggle"){
digitalWrite(COM,!digitalRead(COM));
digitalWrite(LED,!digitalRead(LED));
stamp = "["+addy+"]" + "STATUS CHANGED: " + (String)digitalRead(COM);
uploaddb(stamp);
}
if(server.arg("relay") == "on"){
digitalWrite(COM,LOW);
digitalWrite(LED,LOW);
stamp = "["+addy+"]" + "STATUS CHANGED: " + (String)digitalRead(COM);
uploaddb(stamp);
}
if(server.arg("relay") == "off"){
digitalWrite(COM,HIGH);
digitalWrite(LED,HIGH);
stamp = "["+addy+"]" + "STATUS CHANGED: " + (String)digitalRead(COM);
uploaddb(stamp);
}
if(server.arg("relay") == "restart"){
stamp = "["+addy+"]" + "Rebooted";
uploaddb(stamp);
delay(1000);
ESP.restart();
}
if(server.arg("relay") == "drop"){
stamp = "["+addy+"]" + "DATABASE DROPPED";
uploaddb(stamp);
}
String page = MAIN_page;
int sec = (int) (millis() / 1000) % 60 ;
int minu = (int) ((millis() / (1000*60)) % 60);
int hr = (int) ((millis() / (1000*60*60)) % 24);
String relay_status = (String)digitalRead(COM);
relay_status.replace("0","<span style='color:#aaffc3;'>On</span>");
relay_status.replace("1","<span style='color:red;'>Off</span>");
page.replace("%d",(String)relay_status);
page.replace("%x",(String)sec);
page.replace("%y",(String)minu);
page.replace("%c",(String)hr);
page.replace("%a",(String)"");
page.replace("%g",(String)"");
page.replace("%h",(String)database);
server.send(200, "text/html", page);
}
void webcommand() {
String addy = server.client().remoteIP().toString();
server.send(200, "text/html","<script>window.location.href='../';</script>");
digitalWrite(COM,!digitalRead(COM));
digitalWrite(LED,!digitalRead(LED));
String was_status = (String)digitalRead(COM);
String stamp;
was_status.replace("0","On");
was_status.replace("1","Off");
stamp = "["+addy+"]";
stamp += "STATUS CHANGED: " + was_status;
uploaddb(stamp);
}
void dropReq(){
String stamp;
String addy = server.client().remoteIP().toString();
stamp = "["+addy+"]";
stamp += "DATABASE DROPPED";
HTTPClient http;
String response;
String path = "http://sont.sytes.net/relay_drop.php";
http.begin(path);
int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK) {
response = http.getString();
}
http.end();
server.send(200, "text/html","<script>window.location.href='/';</script>");
uploaddb(stamp);
}
void flashbutton() {
digitalWrite(COM,!digitalRead(COM));
digitalWrite(LED,!digitalRead(LED));
String stamp;
int m = millis();
stamp = "[BUTTON]";
stamp += "STATUS CHANGED: " + (String)digitalRead(COM);
uploaddb(stamp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment