create shelly V2
This commit is contained in:
parent
1e973d2d8f
commit
4b1a26cc6f
@ -6,94 +6,206 @@
|
|||||||
#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
|
||||||
#define SWITCH 5 //0
|
#define SWITCH 5 //0
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
EEPROM.begin(100);
|
EEPROM.begin(100);
|
||||||
while (!Serial) continue;
|
while (!Serial) continue;
|
||||||
delay(10);
|
delay(10);
|
||||||
//read saved data
|
//read saved data
|
||||||
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);
|
||||||
//wifi
|
realState = state;
|
||||||
if (ssid != "") {
|
attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE);
|
||||||
WiFi.persistent(false);
|
//wifi
|
||||||
WiFi.mode(WIFI_STA);
|
if (ssid != "") {
|
||||||
#if defined(staticIpAddress) && defined(subnetIpAddress) && defined(gatewayIpAddress)
|
WiFi.persistent(false);
|
||||||
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
|
WiFi.mode(WIFI_STA);
|
||||||
#endif
|
//Serial.println(WiFi.localIP());
|
||||||
WiFi.begin(ssid, pasw);
|
//Serial.println("IP nastaveny z hodnot.");
|
||||||
conf = wifiVerify(20);
|
//WiFi.config(staticIpAddress, gatewayIpAddress, subnetIpAddress);
|
||||||
if (conf) {
|
WiFi.begin(ssid, pasw);
|
||||||
return;
|
conf = wifiVerify(20);
|
||||||
}
|
if (conf) {
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
jsonContent = {};
|
||||||
|
jsonContent["token"] = apiToken;
|
||||||
|
jsonContent["values"]["on/off"]["value"] = (String)realState;
|
||||||
|
sendDataToWeb();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
setupAP();
|
}
|
||||||
|
setupAP();
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wifiVerify(int t){
|
void handleInterrupt() {
|
||||||
|
buttonActive = true;
|
||||||
|
realState = !state;
|
||||||
|
digitalWrite(RELAY, realState);
|
||||||
|
}
|
||||||
|
|
||||||
|
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++;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CleanEeprom(){
|
void loadDataFromWeb() {
|
||||||
for (int i = 1; i < 100; ++i) {
|
jsonContent = {};
|
||||||
EEPROM.write(i, 0);
|
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() {
|
||||||
|
for (int i = 1; i < 100; ++i) {
|
||||||
|
EEPROM.write(i, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteEeprom (String data, int start = 1) {
|
void WriteEeprom (String data, int start = 1) {
|
||||||
for (int i = 0; i < data.length(); ++i)
|
for (int i = 0; i < data.length(); ++i)
|
||||||
{
|
{
|
||||||
EEPROM.write(start + i, data[i]);
|
EEPROM.write(start + i, data[i]);
|
||||||
}
|
}
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
String ReadEeprom(int min, int max){
|
String ReadEeprom(int min, int max) {
|
||||||
String localString;
|
String localString;
|
||||||
for(int i = min; i < max; ++i) {
|
for (int i = min; i < max; ++i) {
|
||||||
localString += char(EEPROM.read(i));
|
localString += char(EEPROM.read(i));
|
||||||
}
|
}
|
||||||
return localString;
|
return localString;
|
||||||
@ -102,7 +214,7 @@ String ReadEeprom(int min, int max){
|
|||||||
void createWeb()
|
void createWeb()
|
||||||
{
|
{
|
||||||
server.on("/", []() {
|
server.on("/", []() {
|
||||||
if (server.args() == 3){
|
if (server.args() == 3) {
|
||||||
ssid = server.arg("wifi-ssid");
|
ssid = server.arg("wifi-ssid");
|
||||||
pasw = server.arg("wifi-pasw");
|
pasw = server.arg("wifi-pasw");
|
||||||
apiToken = server.arg("apiToken");
|
apiToken = server.arg("apiToken");
|
||||||
@ -125,20 +237,38 @@ 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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
@ -149,7 +279,7 @@ void setupAP(void) {
|
|||||||
Serial.print(n);
|
Serial.print(n);
|
||||||
Serial.println(" networks found");
|
Serial.println(" networks found");
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i)
|
||||||
{
|
{
|
||||||
// Print SSID and RSSI for each network found
|
// Print SSID and RSSI for each network found
|
||||||
Serial.print(i + 1);
|
Serial.print(i + 1);
|
||||||
Serial.print(": ");
|
Serial.print(": ");
|
||||||
@ -157,12 +287,12 @@ void setupAP(void) {
|
|||||||
Serial.print(" (");
|
Serial.print(" (");
|
||||||
Serial.print(WiFi.RSSI(i));
|
Serial.print(WiFi.RSSI(i));
|
||||||
Serial.print(")");
|
Serial.print(")");
|
||||||
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
|
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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