Hey,
mal eine Frage zu der Verbindung zweier ESP8266. Ich würde gerne an dem
einen einen Taster haben, welcher solange er gedrückt ist, über WLAN und
dem angeschlossenen ESP dem zweiten ESP sagt, dass er einen DigitalPin
auf HIGH setzen soll, eben so lange der Taster gedrückt ist.
Aber wie kann ich das in Code umsetzen? Ich würde gerne den Server so
aufsetzen das er einen eigenen AP öffnet damit ich unabhängig von
anderem festen WLAN bin und so der Client direkt auf den 2ten ESP
zugreifen kann.
Ich habe im Internet folgenden Code gefunden, weiß aber noch nicht so
recht ob der bei mir passen könnte und/oder was ich verändern muss. Ich
denke aber das er schon in die Richtung geht.
Wäre nett wenn mir da einer "unter die Arme greifen" könnte. Ich danke
euch :)!
Grüße
Robin
1 | CLIENT:
|
2 |
|
3 | #include <ESP8266WiFi.h>
|
4 | #include <WiFiClient.h>
|
5 |
|
6 | const char* host = "192.168..."; //Ip of the Host(Our Case esp8266-01 as server. Its the ip of the esp8266-01 as Access point)
|
7 |
|
8 | void setup() {
|
9 | Serial.begin(9600); //Baud Rate for Communication
|
10 | delay(10); //Baud rate prper initialization
|
11 | pinMode(14,INPUT); //Pin D7 on NodeMcu Lua. Button to switch on and off the solenoid.
|
12 | WiFi.mode(WIFI_STA); //NodeMcu esp12E in station mode
|
13 | WiFi.begin("ESP_8226_Server_ID_1242421"); //Connect to this SSID. In our case esp-01 SSID.
|
14 |
|
15 | while (WiFi.status() != WL_CONNECTED) { //Wait for getting IP assigned by Access Point/ DHCP.
|
16 | //Our case esp-01 as Access point will assign IP to nodemcu esp12E.
|
17 | delay(500);
|
18 | Serial.print(".");
|
19 | }
|
20 | Serial.println("");
|
21 | Serial.println("WiFi connected");
|
22 | Serial.println("IP address: ");
|
23 | Serial.println(WiFi.localIP()); //Check out the Ip assigned by the esp12E
|
24 | }
|
25 |
|
26 | void loop() {
|
27 |
|
28 | if(digitalRead(14)==1){ //Check out if Pin D7/13 is high. If high activate.
|
29 | //the led connected to esp-01. On next press deactivate it.
|
30 | Serial.print("connecting to ");
|
31 | Serial.println(host);
|
32 | // Use WiFiClient class to create TCP connections
|
33 | WiFiClient client;
|
34 | const int httpPort = 80;
|
35 | if (!client.connect("192.168.4.1", httpPort)) {
|
36 | Serial.println("connection failed");
|
37 | return;
|
38 | }
|
39 | //Request to server to activate the led
|
40 | client.print(String("GET ") +"/Led"+" HTTP/1.1\r\n" +
|
41 | "Host: " + host + "\r\n" +
|
42 | "Connection: close\r\n\r\n");
|
43 | delay(10);
|
44 | // Read all the lines of the reply from server and print them to Serial Monitor etc
|
45 | while(client.available()){
|
46 | String line = client.readStringUntil('\r');
|
47 | Serial.print(line);
|
48 | }
|
49 | //Close the Connection. Automatically
|
50 | Serial.println();
|
51 | Serial.println("closing connection");
|
52 | }//End if
|
53 |
|
54 | } //End Loop
|
1 | SERVER:
|
2 |
|
3 | #include <ESP8266WiFi.h>
|
4 | #include <WiFiClient.h>
|
5 | #include <ESP8266WebServer.h>
|
6 |
|
7 | const char* ssid = "SSIDdesESP"; // SSID of esp8266
|
8 | const char* password = "1234passwort"; //
|
9 | bool toggle=0; //Variable to switch on and off the solenoid
|
10 | ESP8266WebServer server(80); //Specify port for TCP connection
|
11 |
|
12 | void handleRoot() {
|
13 | toggle=!toggle; //Toggling Led Variable
|
14 | digitalWrite(14,toggle); //Toggle Led
|
15 | String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html><h1>Esp8266 Communication</h1> ";
|
16 | s += "<p>Success!!!</html>\r\n\r\n";
|
17 | server.send(200,"text/html",s); //Reply to the client
|
18 | }
|
19 |
|
20 |
|
21 | void setup() {
|
22 | delay(200); //Stable Wifi
|
23 | Serial.begin(9600); //Set Baud Rate
|
24 | pinMode(14, OUTPUT); //Led/Solenoid at pin 2
|
25 | WiFi.softAP(ssid, password); //In Access Point Mode
|
26 |
|
27 | IPAddress myIP = WiFi.softAPIP(); //Check the IP assigned. Put this Ip in the client host.
|
28 | Serial.print("AP IP address: ");
|
29 | Serial.println(myIP); //Print the esp8266-01 IP(Client must also be on the save IP series)
|
30 | server.on("/Led", handleRoot); //Checking client connection
|
31 | server.begin(); // Start the server
|
32 | Serial.println("Server started");
|
33 | }
|
34 |
|
35 | void loop() {
|
36 | // Check if a client has connected. On first connection switch on the Solenoid on next switch off.
|
37 | server.handleClient();
|
38 | }
|