Forum: Mikrocontroller und Digitale Elektronik ESP8266 Client Probleme


von Andy (Gast)


Lesenswert?

Hallo zusammen,

ich habe einen ESP8266 als Accesspoint mit Server konfiguriert (in 
Arduino) mit dem sich eine LED schalten lässt. Rufe ich über den Browser 
die URL: "http://192.168.4.1/?pin=FUNCTION1ON"; auf geht die LED an.
Ein zweiter ESP8266 soll als Client Befehle an den Server schicken, um 
die LED zu schalten. Der Client verbindet sich auch mit dem WLAN (ich 
bekomme die Rückmeldung "Connected!" und i=1 bzw i=0, siehe Code), 
allerdings reagiert der Server nicht auf den ESP. Ich habe mittlerweile 
so viel rumprobiert, aber leider ohne Erfolg. Ich würde mich freuen wenn 
mir jemand weiterhelfen könnte. Danke schonmal!

Server: vereinfacht, da SPAM Filter auf den Code anschlägt...
1
/*--------------------------------------------------
2
HTTP 1.1 Webserver as AccessPoint for ESP8266 
3
for ESP8266 adapted Arduino IDE
4
5
by Stefan Thesen 08/2015 - free for anyone
6
7
Does HTTP 1.1 with defined connection closing.
8
Handles empty requests in a defined manner.
9
Handle requests for non-exisiting pages correctly.
10
11
This demo allows to switch two functions:
12
Function 1 creates serial output and toggels GPIO2
13
Function 2 just creates serial output.
14
15
Serial output can e.g. be used to steer an attached
16
Arduino, Raspberry etc.
17
--------------------------------------------------*/
18
19
#include <ESP8266WiFi.h>
20
21
const char* ssid = "ESP-Accesspoint";
22
const char* password = "12345678";  // set to XX for open access point w/o passwortd
23
24
unsigned long ulReqcount;
25
26
27
// Create an instance of the server on Port 80
28
WiFiServer server(80);
29
30
void setup() 
31
{
32
  // setup globals
33
  ulReqcount=0; 
34
  
35
  // prepare GPIO2
36
  pinMode(2, OUTPUT);
37
  digitalWrite(2, 0);
38
  
39
  // start serial
40
  Serial.begin(9600);
41
  delay(1);
42
  
43
  // AP mode
44
  WiFi.mode(WIFI_AP);
45
  WiFi.softAP(ssid, password);
46
  server.begin();
47
}
48
49
void loop() 
50
{ 
51
  // Check if a client has connected
52
  WiFiClient client = server.available();
53
  if (!client) 
54
  {
55
    return;
56
  }
57
  
58
  // Wait until the client sends some data
59
  Serial.println("new client");
60
  unsigned long ultimeout = millis()+250;
61
  while(!client.available() && (millis()<ultimeout) )
62
  {
63
    delay(1);
64
  }
65
  if(millis()>ultimeout) 
66
  { 
67
    Serial.println("client connection time-out!");
68
    return; 
69
  }
70
  
71
  // Read the first line of the request
72
  String sRequest = client.readStringUntil('\r');
73
  //Serial.println(sRequest);
74
  client.flush();
75
  
76
  // stop client, if request is empty
77
  if(sRequest==XX)
78
  {
79
    Serial.println("empty request! - stopping client");
80
    client.stop();
81
    return;
82
  }
83
  
84
  // get path; end of path is either space or ?
85
  // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
86
  String sPath=XX,sParam=XX, sCmd=XX;
87
  String sGetstart="GET ";
88
  int iStart,iEndSpace,iEndQuest;
89
  iStart = sRequest.indexOf(sGetstart);
90
  if (iStart>=0)
91
  {
92
    iStart+=+sGetstart.length();
93
    iEndSpace = sRequest.indexOf("X",iStart);
94
    iEndQuest = sRequest.indexOf("?",iStart);
95
    
96
    // are there parameters?
97
    if(iEndSpace>0)
98
    {
99
      if(iEndQuest>0)
100
      {
101
        // there are parameters
102
        sPath  = sRequest.substring(iStart,iEndQuest);
103
        sParam = sRequest.substring(iEndQuest,iEndSpace);
104
      }
105
      else
106
      {
107
        // NO parameters
108
        sPath  = sRequest.substring(iStart,iEndSpace);
109
      }
110
    }
111
  }
112
113
   ///////////////////////////////////////////////////////////////////////////////
114
  // output parameters to serial, you may connect e.g. an Arduino and react on it
115
  ///////////////////////////////////////////////////////////////////////////////
116
  if(sParam.length()>0)
117
  {
118
    int iEqu=sParam.indexOf("=");
119
    if(iEqu>=0)
120
    {
121
      sCmd = sParam.substring(iEqu+1,sParam.length());
122
      Serial.println(sCmd);
123
    }
124
  }
125
  
126
.......
127
128
 //////////////////////
129
    // react on parameters
130
    //////////////////////
131
    if (sCmd.length()>0)
132
    {
133
      // write received command to html page
134
      sResponse += "Kommando:" + sCmd + "<BR>";
135
      
136
      // switch GPIO
137
      if(sCmd.indexOf("FUNCTION1ON")>=0)
138
      {
139
        digitalWrite(2, 1);
140
      }
141
      else if(sCmd.indexOf("FUNCTION1OFF")>=0)
142
      {
143
        digitalWrite(2, 0);
144
      }
145
    }
146
    
147
........

Client:
1
#include <ESP8266WiFi.h>
2
#include <ESP8266HTTPClient.h>
3
4
const char* ssid = "ESP-Accesspoint";
5
const char* password = "12345678";
6
int i=1;
7
8
void setup (){
9
10
Serial.begin(115200);
11
WiFi.begin(ssid, password);
12
13
 while (WiFi.status() != WL_CONNECTED) {
14
15
delay(1000);
16
Serial.println("Connecting..");
17
18
}
19
20
}
21
22
void loop() {
23
24
if(WiFi.status()== WL_CONNECTED){   //Check WiFi connection status
25
Serial.println("Connected!");
26
HTTPClient http;  //Declare an object of class HTTPClient
27
28
if(i==1){
29
  i=0;
30
  http.begin("http://192.168.4.1:80/?pin=FUNCTION1ON");  //Specify request destination
31
  Serial.println("i=1");
32
}
33
else{
34
  i=1;
35
  http.begin("http://192.168.4.1:80/?pin=FUNCTION1OFF");  //Specify request destination
36
  Serial.println("i=0");
37
}
38
int httpCode= http.GET();                                                                   //Send the request
39
40
if(httpCode>0){    //Check the returning code
41
42
String payload = http.getString();   //Get the request response payload
43
Serial.println(payload);                     //Print the response payload
44
}
45
46
http.end();   //Close connection
47
48
}
49
50
delay(5000);    //Send a request every 30 seconds
51
52
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.