uz dlhsie sa trapim s komunikaciou medzi ESP8266 v AP mode. Konkretne pouzivam modul ESP01s a navrhol som si kod, kde je 1 receiver a 3 transmittery. V tejto konfiguracii (1+3) vsetko funguje. pustil som sa do upgradu a nijako sa mi nedari napisat kody pre 2 receivery (chcel by som dosiahnut 2 receivery + 1 transmitter). Vacsinou som mal problem, ze ked boli oba receivery online, tak reagoval iba jeden, no ked boli online separatne (jeden za druhym) tak reagoval kazdy z nich. Skusal som kazdemu zariadeniu nastavovat fixnu IP, davat kazde zariadenie na iny port ale bez uspechu. Naozaj som sa zasekol na jednom mieste a neviem sa pohnut. Preto by som vas poprosil, ci by ste mi s tym pomohli. Vopred dakujem
Transmitter:
Kód: Vybrať všetko
//::::::::::::::::::TRANSMITTER::::::::::::::::::
#include <ESP8266WiFi.h>
const char* receiverIP = "192.168.4.1"; // IP address of the receiver
const int pirSensorPin = 2; // PIR sensor pin connected to GPIO2
WiFiClient client; // Initialize WiFi client
bool messageSent = false; // Flag to track message sent status
void setup() {
Serial.begin(115200);
// Connect to the Receiver's Access Point
WiFi.begin("ESP-alarm", "123456789");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Receiver's AP...");
}
Serial.println("Connected to Receiver's AP");
pinMode(pirSensorPin, INPUT);
}
void loop() {
int motionDetected = digitalRead(pirSensorPin);
if (motionDetected == HIGH && !messageSent) {
Serial.println("Motion detected from Transmitter 1!");
// Send a message to the receiver
if (client.connect(receiverIP, 80)) {
Serial.println("Connected to Receiver");
client.println("MotionDetected1");
client.stop();
messageSent = true; // Set the flag to indicate message sent
}
} else if (motionDetected == LOW && messageSent) {
// Reset the flag when the PIR sensor is not detecting motion
messageSent = false;
}
delay(100);
}
Kód: Vybrať všetko
//:::::::::::::::::::RECEIVER 1:::::::::::::::::::::::
#include <ESP8266WiFi.h>
const char* receiverSSID = "ESP-alarm"; // Receiver's SSID
const char* receiverPassword = "123456789"; // Receiver's Wi-Fi Password
const int ledPin1 = 0; // GPIO0 pin for LED
const int ledPin2 = 2; // GPIO2 pin for LED
WiFiServer server(80); // Create a server on port 80
bool isTriggered1 = false;
bool isTriggered2 = false;
bool isTriggered3 = false;
void setup() {
Serial.begin(115200);
// Set up the receiver as an Access Point
WiFi.softAP(receiverSSID, receiverPassword);
Serial.println("Receiver Access Point started");
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
server.begin(); // Start the server
server.setNoDelay(true); // Set no delay for faster response
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
WiFiClient client = server.available(); // Check for client connection
if (client) {
String request = client.readStringUntil('\r\n'); // Read client data
Serial.println(request);
if (request.indexOf("MotionDetected1") != -1) {
if (!isTriggered1) {
Serial.println("PIR 1 Trigger received - Motion detected!");
digitalWrite(ledPin1, HIGH);
isTriggered1 = true;
delay(2000);
digitalWrite(ledPin1, LOW);
isTriggered1 = false;
}
}
delay(10);
client.stop(); // Close the connection
}
}
snazil som sa komentovat najviac kodu, ako to len slo, tak snad je to zrozumitelne. Mojim zamerom je, aby ked transmitter je triggernuty, tak odosle spravu do oboch receiverov a tie potom rozsvietia ledku. Kazde zariadenie ma svoju esp01s dosku a vyuzivaju svoju vlastnu wifi siet. nie je k dospozicii externa wifi.
Naozaj vopred dakujem za pomoc.