ESP32 можно управлять, если пользователь перейдет по ссылке, например пример: у них одинаковый Wi-Fi и фиксированный IP.
Проблема в том, что мое приложение запускает программу одну за другой или имеет медленный ответ.
что-то вроде этого:
- sendHttpRequest(url1)
- sendHttpRequest(url2)
Я пытаюсь добавить задержку() в esp32, но она одновременно не обрабатывается. потому что время, замедляющее его, не фиксировано.
CoroutineScope(Dispatchers.IO).launch {
try {
val requestA = async { sendHttpRequest(urlA) }
val requestB = async { sendHttpRequest(urlB) }
val requestC = async { sendHttpRequest(urlC) }
awaitAll(requestA, requestB, requestC)
} catch (e: Exception) {
Log.e("[FISH] BuildingController", "Error: ${e.message}")
showToast("Error occurred while toggling towers.")
}
< /code>
private fun sendHttpRequest(url: String) {
try {
val request = Request.Builder()
.url(url)
.build()
val response: Response = client.newCall(request).execute()
if (response.isSuccessful) {
// Log success or update UI
Log.d("[FISH] BuildingController", "Request successful: $url")
} else {
Log.e("[FISH] BuildingController", "Request failed: $url")
showToast("Failed ON or OFF")
}
} catch (e: Exception) {
Log.e("[FISH] BuildingController", "Error: ${e.message}")
showToast("Error occurred: ${e.message}")
}
}
< /code>
#include
#include
#include
const char* ssid = "FishEatRice";
const char* password = "123456789";
WiFiServer server(80);
IPAddress local_IP(192, 168, 68, 185); // IP
IPAddress gateway(192, 168, 68, 1); // Network IP
IPAddress subnet(255, 255, 255, 0); // subnet
// Changed from floor to floorPins to avoid conflict with math.h's floor function
int floorPins[41];
void setup() {
Serial.begin(115200);
WiFi.config(local_IP, gateway, subnet);
WiFi.begin(ssid, password);
server.begin();
pinMode(10, OUTPUT);
}
void loop() {
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("GET /on") >= 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
client.println("Connection: close");
client.println();
digitalWrite(10, HIGH);
}
// Tower A
if (request.indexOf("GET /off") >= 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/plain");
client.println("Connection: close");
client.println();
digitalWrite(10, LOW);
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... -same-time