Compare commits

...

2 Commits

Author SHA1 Message Date
gabriel becker
9bf1bb1871 Implement WiFi dual mode functionality by adding station mode. 2022-08-22 22:48:46 -03:00
gabriel becker
87fb00bd26 Make a test with fixed ip 2022-08-22 18:47:17 -03:00
2 changed files with 73 additions and 24 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/*

View File

@ -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,50 @@ 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 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");
try_to_connect_to_wifi_network_if_not_connected();
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
}
void loop()
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();
}