create shelly V2
This commit is contained in:
parent
1e973d2d8f
commit
4b1a26cc6f
@ -6,24 +6,29 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
//Variables
|
//Variables
|
||||||
const char* ssidServer = "ESPFilip";
|
const char* ssidServer = "";
|
||||||
const char* paswServer = "Sapatr6";
|
const char* paswServer = "";
|
||||||
String ssid = "";
|
String ssid = "";
|
||||||
String pasw = "";
|
String pasw = "";
|
||||||
String apiToken = "";
|
String apiToken = "";
|
||||||
const char* host = "http://dev.steelants.cz";
|
const char* host = "http://dev.steelants.cz";
|
||||||
const char* url = "/vasek/home/api.php";
|
const char* url = "/vasek/home/api.php";
|
||||||
|
|
||||||
//NetworkData
|
IPAddress staticIpAddress;
|
||||||
// IPAddress staticIpAddress = "";
|
IPAddress gatewayIpAddress;
|
||||||
// IPAddress subnetIpAddress = "";
|
IPAddress subnetIpAddress;
|
||||||
// IPAddress gatewayIpAddress = "";
|
|
||||||
|
|
||||||
String content;
|
String content;
|
||||||
bool conf = false;
|
bool conf = false;
|
||||||
|
bool buttonActive = false;
|
||||||
|
int state = 0;
|
||||||
|
int realState = 0;
|
||||||
|
String requestJson = "";
|
||||||
|
int unsuccessfulRounds = 0; //Unsucesful atmpt counter
|
||||||
|
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
StaticJsonDocument<250> jsonContent;
|
StaticJsonDocument<250> jsonContent;
|
||||||
|
DeserializationError error;
|
||||||
|
|
||||||
//Pins
|
//Pins
|
||||||
#define RELAY 4 //12
|
#define RELAY 4 //12
|
||||||
@ -38,21 +43,28 @@ void setup() {
|
|||||||
ssid = ReadEeprom(1, 33);
|
ssid = ReadEeprom(1, 33);
|
||||||
pasw = ReadEeprom(33, 65);
|
pasw = ReadEeprom(33, 65);
|
||||||
apiToken = ReadEeprom(65, 97);
|
apiToken = ReadEeprom(65, 97);
|
||||||
|
|
||||||
//set pins
|
//set pins
|
||||||
pinMode(SWITCH, INPUT);
|
pinMode(SWITCH, INPUT);
|
||||||
pinMode(RELAY, OUTPUT);
|
pinMode(RELAY, OUTPUT);
|
||||||
|
state = EEPROM.read(0);
|
||||||
|
digitalWrite(RELAY, state);
|
||||||
|
realState = state;
|
||||||
|
attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE);
|
||||||
//wifi
|
//wifi
|
||||||
if (ssid != "") {
|
if (ssid != "") {
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
#if defined(staticIpAddress) && defined(subnetIpAddress) && defined(gatewayIpAddress)
|
//Serial.println(WiFi.localIP());
|
||||||
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
|
//Serial.println("IP nastaveny z hodnot.");
|
||||||
#endif
|
//WiFi.config(staticIpAddress, gatewayIpAddress, subnetIpAddress);
|
||||||
WiFi.begin(ssid, pasw);
|
WiFi.begin(ssid, pasw);
|
||||||
conf = wifiVerify(20);
|
conf = wifiVerify(20);
|
||||||
if (conf) {
|
if (conf) {
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
jsonContent = {};
|
||||||
|
jsonContent["token"] = apiToken;
|
||||||
|
jsonContent["values"]["on/off"]["value"] = (String)realState;
|
||||||
|
sendDataToWeb();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,16 +72,43 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!conf) {
|
if (conf) {
|
||||||
|
if (unsuccessfulRounds >= 5) {
|
||||||
|
Serial.println("RESTARTING ESP");
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
if (buttonActive) {
|
||||||
|
delay (500);
|
||||||
|
jsonContent = {};
|
||||||
|
jsonContent["token"] = apiToken;
|
||||||
|
requestJson = "";
|
||||||
|
jsonContent["values"]["on/off"]["value"] = (String)realState;
|
||||||
|
digitalWrite(RELAY, realState);
|
||||||
|
EEPROM.write(0, realState);
|
||||||
|
EEPROM.commit();
|
||||||
|
sendDataToWeb();
|
||||||
|
buttonActive = false;
|
||||||
|
}
|
||||||
|
loadDataFromWeb();
|
||||||
|
} else {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleInterrupt() {
|
||||||
|
buttonActive = true;
|
||||||
|
realState = !state;
|
||||||
|
digitalWrite(RELAY, realState);
|
||||||
|
}
|
||||||
|
|
||||||
bool wifiVerify(int t) {
|
bool wifiVerify(int t) {
|
||||||
int c = 0;
|
int c = 0;
|
||||||
Serial.println("Waiting for Wifi to connect to Shelly1");
|
Serial.println("Waiting for Wifi to connect to Shelly1");
|
||||||
while (c < t) {
|
while (c < t) {
|
||||||
if (WiFi.status() == WL_CONNECTED) { return true; }
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
|
c = t;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(WiFi.status());
|
Serial.print(WiFi.status());
|
||||||
c++;
|
c++;
|
||||||
@ -77,6 +116,79 @@ bool wifiVerify(int t){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loadDataFromWeb() {
|
||||||
|
jsonContent = {};
|
||||||
|
jsonContent["token"] = apiToken;
|
||||||
|
requestJson = "";
|
||||||
|
sendDataToWeb();
|
||||||
|
|
||||||
|
if (error.code() != DeserializationError::Ok) {
|
||||||
|
Serial.println(error.c_str());
|
||||||
|
unsuccessfulRounds++;
|
||||||
|
Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5");
|
||||||
|
error = DeserializationError::Ok;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//configuration setup
|
||||||
|
String hostName = jsonContent["device"]["hostname"];
|
||||||
|
String requestState = jsonContent["state"];
|
||||||
|
JsonObject object = jsonContent.as<JsonObject>();
|
||||||
|
if (!object["value"].isNull()) {
|
||||||
|
state = (int)jsonContent["value"];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requestState != "succes") {
|
||||||
|
unsuccessfulRounds++;
|
||||||
|
Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5");
|
||||||
|
} else if (requestState == "succes") {
|
||||||
|
unsuccessfulRounds = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFi.hostname(hostName);
|
||||||
|
Serial.println("state: " + (String)state + ", realState: " + (String)realState);
|
||||||
|
if (state != realState && !buttonActive) {
|
||||||
|
if (state == 1 && realState == 0) {
|
||||||
|
Serial.println("ON state: " + (String)state + ", realState: " + (String)realState);
|
||||||
|
realState = 1;
|
||||||
|
} else {
|
||||||
|
Serial.println("OFF");
|
||||||
|
realState = 0;
|
||||||
|
}
|
||||||
|
digitalWrite(RELAY, realState);
|
||||||
|
EEPROM.write(0, realState);
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendDataToWeb() {
|
||||||
|
serializeJson(jsonContent, requestJson);
|
||||||
|
Serial.println("JSON: " + requestJson);
|
||||||
|
error = deserializeJson(jsonContent, sendHttpRequest());
|
||||||
|
}
|
||||||
|
|
||||||
|
String sendHttpRequest () {
|
||||||
|
HTTPClient http;
|
||||||
|
http.setReuse(true);
|
||||||
|
Serial.println("HTTP url: " + String(host) + String(url) + ""); //Print HTTP return code
|
||||||
|
http.begin(String(host) + String(url));
|
||||||
|
http.addHeader("Content-Type", "text/plain"); //Specify content-type header
|
||||||
|
Serial.println("HTTP request: " + String(requestJson) + ""); //Print HTTP return code
|
||||||
|
int httpCode = http.POST(requestJson);
|
||||||
|
String payload = http.getString(); //Get the response payload
|
||||||
|
http.end();
|
||||||
|
|
||||||
|
Serial.println("HTTP CODE: " + String(httpCode) + ""); //Print HTTP return code
|
||||||
|
Serial.println("HTTP BODY: " + String(payload) + ""); //Print request response payload
|
||||||
|
|
||||||
|
if (httpCode == -1) {
|
||||||
|
unsuccessfulRounds++;
|
||||||
|
Serial.println("UNSUCCESSFUL ROUND NUMBER " + String(unsuccessfulRounds) + "FROM 5");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
void CleanEeprom() {
|
void CleanEeprom() {
|
||||||
for (int i = 1; i < 100; ++i) {
|
for (int i = 1; i < 100; ++i) {
|
||||||
EEPROM.write(i, 0);
|
EEPROM.write(i, 0);
|
||||||
@ -125,12 +237,29 @@ void createWeb()
|
|||||||
content += "</style></head>";
|
content += "</style></head>";
|
||||||
content += "<h2>WIFI Configuration</h2>";
|
content += "<h2>WIFI Configuration</h2>";
|
||||||
content += "<a href='#'>Refresh</a>";
|
content += "<a href='#'>Refresh</a>";
|
||||||
|
content += "<div class=\"wifi-list\">";
|
||||||
|
int n = WiFi.scanNetworks();
|
||||||
|
if (n == 0)
|
||||||
|
content += "<label>No networks found...</label>";
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
content += "<a href=\"#\" onclick=\"fillSSID(this.innerHTML)\">" + WiFi.SSID(i) + "</a><br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content += "</div>";
|
||||||
content += "<form method='get' action=''><div class='wifi-form'>";
|
content += "<form method='get' action=''><div class='wifi-form'>";
|
||||||
content += "<input name='wifi-ssid' length=32 type='text'><br>";
|
content += "<label>SSID: </label><input name='wifi-ssid' id='wifi-ssid' length=32 type='text'><br>";
|
||||||
content += "<input name='wifi-pasw' length=32 type='password'><br>";
|
content += "<label>Heslo: </label><input name='wifi-pasw' length=32 type='password'><br>";
|
||||||
content += "<input name='apiToken' length=32 type='password'><br>";
|
content += "<label>Api token: </label><input name='apiToken' length=32 type='password'><br>";
|
||||||
content += "<input type='submit' value='Connect'>";
|
content += "<input type='submit' value='Connect'>";
|
||||||
content += "</div></form>";
|
content += "</div></form>";
|
||||||
|
content += "<script>";
|
||||||
|
content += "function fillSSID(value) {\r\n";
|
||||||
|
content += "document.getElementById(\"wifi-ssid\").value = value;\r\n";
|
||||||
|
content += "}";
|
||||||
|
content += "</script>";
|
||||||
content += "</body>";
|
content += "</body>";
|
||||||
server.send(200, "text/html", content);
|
server.send(200, "text/html", content);
|
||||||
});
|
});
|
||||||
@ -139,6 +268,7 @@ void createWeb()
|
|||||||
void setupAP(void) {
|
void setupAP(void) {
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
|
WiFi.softAPdisconnect(true);
|
||||||
delay(100);
|
delay(100);
|
||||||
int n = WiFi.scanNetworks();
|
int n = WiFi.scanNetworks();
|
||||||
Serial.println("scan done");
|
Serial.println("scan done");
|
||||||
@ -162,7 +292,7 @@ void setupAP(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
delay(100);
|
delay(100);
|
||||||
WiFi.softAP(ssidServer);
|
WiFi.softAP(ssidServer, paswServer);
|
||||||
Serial.println("softap");
|
Serial.println("softap");
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
|
Loading…
Reference in New Issue
Block a user