|
|
|
@ -15,8 +15,11 @@
|
|
|
|
|
const char *APssid = APSSID; |
|
|
|
|
const char *APpassword = APPSK; |
|
|
|
|
|
|
|
|
|
const char *ssid = "Apto202"; |
|
|
|
|
const char *password = "gatocafe"; |
|
|
|
|
const char *ssid = "QueirozX"; |
|
|
|
|
const char *password = "queirozlindo"; |
|
|
|
|
|
|
|
|
|
bool is_discovering_tuya_ip = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ESP8266WebServer web_server(80); |
|
|
|
|
WiFiClient wifiClient; |
|
|
|
@ -52,6 +55,41 @@ void handleSetHost()
|
|
|
|
|
HOST_ADDRESS = host; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void try_to_connect_to_wifi_network_if_not_connected() |
|
|
|
|
{ |
|
|
|
|
WiFi.begin(ssid, password); |
|
|
|
|
while(WiFi.status() != WL_CONNECTED) |
|
|
|
|
{ |
|
|
|
|
Serial.print("."); |
|
|
|
|
delay(500); |
|
|
|
|
} |
|
|
|
|
Serial.println(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String return_first_client_ip() |
|
|
|
|
{ |
|
|
|
|
unsigned char softap_stations_cnt; |
|
|
|
|
struct station_info *stat_info; |
|
|
|
|
uint32 uintaddress; |
|
|
|
|
|
|
|
|
|
softap_stations_cnt = wifi_softap_get_station_num(); |
|
|
|
|
stat_info = wifi_softap_get_station_info(); |
|
|
|
|
String string_ip; |
|
|
|
|
|
|
|
|
|
if (stat_info != NULL) { |
|
|
|
|
auto IPaddress = &stat_info->ip; |
|
|
|
|
uintaddress = IPaddress->addr; |
|
|
|
|
IPAddress myIp(uintaddress); |
|
|
|
|
string_ip = myIp.toString(); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
return String("👎"); |
|
|
|
|
} |
|
|
|
|
return string_ip; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void setup() |
|
|
|
|
{ |
|
|
|
|
pinMode(BUTTON, INPUT_PULLUP); |
|
|
|
@ -61,40 +99,54 @@ void setup()
|
|
|
|
|
Serial.print("Configuring access point..."); |
|
|
|
|
/* You can remove the password parameter if you want the AP to be open. */ |
|
|
|
|
|
|
|
|
|
// WiFi.mode(WIFI_AP_STA);
|
|
|
|
|
// WiFi.softAP(APssid, APpassword);
|
|
|
|
|
WiFi.begin(ssid, password); |
|
|
|
|
while (WiFi.status() != WL_CONNECTED) |
|
|
|
|
{ |
|
|
|
|
Serial.print('.'); |
|
|
|
|
delay(1000); |
|
|
|
|
} |
|
|
|
|
// Serial.println(WiFi.localIP());
|
|
|
|
|
// Serial.println(WiFi.softAPIP());
|
|
|
|
|
WiFi.mode(WIFI_AP_STA); |
|
|
|
|
WiFi.softAP(APssid, APpassword); |
|
|
|
|
|
|
|
|
|
Serial.print("Server AP ip: "); |
|
|
|
|
Serial.println(WiFi.softAPIP()); |
|
|
|
|
|
|
|
|
|
Serial.print("AP IP address: "); |
|
|
|
|
Serial.println(WiFi.localIP()); |
|
|
|
|
web_server.on("/", handleRoot); |
|
|
|
|
web_server.on("/sethost", handleSetHost); |
|
|
|
|
web_server.begin(); |
|
|
|
|
Serial.println("HTTP server started"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (is_discovering_tuya_ip) |
|
|
|
|
{ |
|
|
|
|
Serial.println('Collecting ip'); |
|
|
|
|
String client_ip = return_first_client_ip(); |
|
|
|
|
if (client_ip != String("👎")) |
|
|
|
|
{ |
|
|
|
|
is_discovering_tuya_ip = false; |
|
|
|
|
HOST_ADDRESS = client_ip; |
|
|
|
|
Serial.println("New host address: " + HOST_ADDRESS); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Serial.println("Leaving setup"); |
|
|
|
|
|
|
|
|
|
void loop() |
|
|
|
|
try_to_connect_to_wifi_network_if_not_connected(); |
|
|
|
|
|
|
|
|
|
Serial.print("Server AP ip: "); |
|
|
|
|
Serial.println(WiFi.localIP()); |
|
|
|
|
|
|
|
|
|
Serial.print("Server external ip: "); |
|
|
|
|
Serial.println(WiFi.softAPIP()); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void handle_button_click() |
|
|
|
|
{ |
|
|
|
|
if(digitalRead(BUTTON) == LOW) { |
|
|
|
|
//digitalWrite(LED_BUILTIN, HIGH);
|
|
|
|
|
//int v = digitalRead(BUTTON);
|
|
|
|
|
//Serial.print("READ: ");
|
|
|
|
|
//Serial.println(v);
|
|
|
|
|
// Wait for the button to be released
|
|
|
|
|
//while(digitalRead(BUTTON) == LOW) {
|
|
|
|
|
// delay(100);
|
|
|
|
|
//}
|
|
|
|
|
//digitalWrite(LED_BUILTIN, LOW);
|
|
|
|
|
//Serial.println("BOO");
|
|
|
|
|
make_toggle_request(); |
|
|
|
|
delay(500); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void loop() |
|
|
|
|
{ |
|
|
|
|
handle_button_click(); |
|
|
|
|
web_server.handleClient(); |
|
|
|
|
} |
|
|
|
|