wifi setting
This commit is contained in:
parent
ccd064ad3c
commit
edf0b1ceb4
@ -6,9 +6,11 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
|
|
||||||
//Variables
|
//Variables
|
||||||
const char* ssid = "ssid";
|
const char* ssidServer = "ESPFilip";
|
||||||
const char* pasw = "pasw";
|
const char* paswServer = "Sapatr6";
|
||||||
const char* apiToken = "apiToken";
|
String ssid = "";
|
||||||
|
String pasw = "";
|
||||||
|
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";
|
||||||
|
|
||||||
@ -16,6 +18,8 @@ const char* url = "/vasek/home/api.php";
|
|||||||
// IPAddress staticIpAddress = "";
|
// IPAddress staticIpAddress = "";
|
||||||
// IPAddress subnetIpAddress = "";
|
// IPAddress subnetIpAddress = "";
|
||||||
// IPAddress gatewayIpAddress = "";
|
// IPAddress gatewayIpAddress = "";
|
||||||
|
|
||||||
|
String content;
|
||||||
bool conf = false;
|
bool conf = false;
|
||||||
|
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
@ -26,11 +30,10 @@ StaticJsonDocument<250> jsonContent;
|
|||||||
#define SWITCH 5 //0
|
#define SWITCH 5 //0
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
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);
|
||||||
@ -41,37 +44,61 @@ void setup() {
|
|||||||
pinMode(RELAY, OUTPUT);
|
pinMode(RELAY, OUTPUT);
|
||||||
|
|
||||||
//wifi
|
//wifi
|
||||||
if ( ssid.length() > 1 ) {
|
if (ssid != "") {
|
||||||
WiFi.begin(ssid.c_str(), pasw.c_str());
|
|
||||||
conf = !wifiVerify(20);
|
|
||||||
if (conf) {
|
|
||||||
Serial.println("");
|
|
||||||
Serial.println("WiFi connected");
|
|
||||||
Serial.print("Local IP: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
Serial.print("SoftAP IP: ");
|
|
||||||
Serial.println(WiFi.softAPIP());
|
|
||||||
createWeb();
|
|
||||||
// Start the server
|
|
||||||
server.begin();
|
|
||||||
Serial.println("Server started");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
WiFi.persistent(false);
|
WiFi.persistent(false);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(ssid, pasw);
|
|
||||||
#if defined(staticIpAddress) && defined(subnetIpAddress) && defined(gatewayIpAddress)
|
#if defined(staticIpAddress) && defined(subnetIpAddress) && defined(gatewayIpAddress)
|
||||||
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
|
WiFi.config(staticIpAddress, subnetIpAddress, gatewayIpAddress);
|
||||||
#endif
|
#endif
|
||||||
|
WiFi.begin(ssid, pasw);
|
||||||
|
conf = wifiVerify(20);
|
||||||
|
if (conf) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setupAP();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (conf) {
|
if (!conf) {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wifiVerify(int t){
|
||||||
|
int c = 0;
|
||||||
|
Serial.println("Waiting for Wifi to connect to Shelly1");
|
||||||
|
while (c < t) {
|
||||||
|
if (WiFi.status() == WL_CONNECTED) { return true; }
|
||||||
|
delay(500);
|
||||||
|
Serial.print(WiFi.status());
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CleanEeprom(){
|
||||||
|
for (int i = 1; i < 100; ++i) {
|
||||||
|
EEPROM.write(i, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteEeprom (String data, int start = 1) {
|
||||||
|
for (int i = 0; i < data.length(); ++i)
|
||||||
|
{
|
||||||
|
EEPROM.write(start + i, data[i]);
|
||||||
|
}
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
String ReadEeprom(int min, int max){
|
||||||
|
String localString;
|
||||||
|
for(int i = min; i < max; ++i) {
|
||||||
|
localString += char(EEPROM.read(i));
|
||||||
|
}
|
||||||
|
return localString;
|
||||||
|
}
|
||||||
|
|
||||||
void createWeb()
|
void createWeb()
|
||||||
{
|
{
|
||||||
server.on("/", []() {
|
server.on("/", []() {
|
||||||
@ -79,7 +106,7 @@ void createWeb()
|
|||||||
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");
|
||||||
if (ssid.length() > 0 && pasw.length() > 0 && apiToken.length() > 0) {
|
if (ssid != "" && pasw != "" && apiToken != "") {
|
||||||
CleanEeprom();
|
CleanEeprom();
|
||||||
WriteEeprom(ssid);
|
WriteEeprom(ssid);
|
||||||
WriteEeprom(pasw, 33);
|
WriteEeprom(pasw, 33);
|
||||||
@ -97,7 +124,7 @@ void createWeb()
|
|||||||
content += "input {width: 100%;box-sizing: border-box}";
|
content += "input {width: 100%;box-sizing: border-box}";
|
||||||
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 += "<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 += "<input name='wifi-ssid' length=32 type='text'><br>";
|
||||||
content += "<input name='wifi-pasw' length=32 type='password'><br>";
|
content += "<input name='wifi-pasw' length=32 type='password'><br>";
|
||||||
@ -109,35 +136,43 @@ void createWeb()
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wifiVerify(int t){
|
void setupAP(void) {
|
||||||
int c = 0;
|
WiFi.mode(WIFI_STA);
|
||||||
Serial.println("Waiting for Wifi to connect to Shelly1");
|
WiFi.disconnect();
|
||||||
while (c < t) {
|
delay(100);
|
||||||
if (WiFi.status() == WL_CONNECTED) { return true; }
|
int n = WiFi.scanNetworks();
|
||||||
delay(500);
|
Serial.println("scan done");
|
||||||
Serial.print(WiFi.status());
|
if (n == 0)
|
||||||
c++;
|
Serial.println("no networks found");
|
||||||
}
|
else
|
||||||
}
|
|
||||||
|
|
||||||
void CleanEeprom(){
|
|
||||||
for (int i = 1; i < 100; ++i) {
|
|
||||||
EEPROM.write(i, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WriteEeprom (char* data, int start = 0) {
|
|
||||||
for (int i = 0; i < data.length(); ++i)
|
|
||||||
{
|
{
|
||||||
EEPROM.write(start + i, data[i]);
|
Serial.print(n);
|
||||||
|
Serial.println(" networks found");
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
// Print SSID and RSSI for each network found
|
||||||
|
Serial.print(i + 1);
|
||||||
|
Serial.print(": ");
|
||||||
|
Serial.print(WiFi.SSID(i));
|
||||||
|
Serial.print(" (");
|
||||||
|
Serial.print(WiFi.RSSI(i));
|
||||||
|
Serial.print(")");
|
||||||
|
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
EEPROM.commit();
|
|
||||||
}
|
}
|
||||||
|
delay(100);
|
||||||
char* ReadEeprom(int min, int max){
|
WiFi.softAP(ssidServer);
|
||||||
char* localString;
|
Serial.println("softap");
|
||||||
for(int i = min; i < max; ++i) {
|
Serial.println("");
|
||||||
localString += char(EEPROM.read(i));
|
Serial.println("WiFi connected");
|
||||||
}
|
Serial.print("Local IP: ");
|
||||||
return localString;
|
Serial.println(WiFi.localIP());
|
||||||
|
Serial.print("SoftAP IP: ");
|
||||||
|
Serial.println(WiFi.softAPIP());
|
||||||
|
createWeb();
|
||||||
|
// Start the server
|
||||||
|
server.begin();
|
||||||
|
Serial.println("Server started");
|
||||||
|
Serial.println("over");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user