Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By WereCatf
#38809
Dan Kiefer wrote:Now I tried to connect to it from another ESP8266 using the WifiClient example. Here I am stuck. In the example, a sparkfun address is used as the host. If I replace the host address by the IP of my webserver (192.168.4.1), my WifiClient connects to the network, but the connection to the server fails (client.connect("192.168.4.1", 80) == false). My intention was to send the same string to the webserver that is also sent by the browser. Sorry if that sounds silly.

How can I 'customize the script to talk to other http servers' (as it is said in the WifiClient example)? Do I have to change the server part in order that it accepts such requests?


First of all, if your server works from a browser then it works from another ESP, too -- there is no change needed on the server. It's your client-sketch that is wrong, so I do have to ask: are you trying to run your client as an AP or a STA? It obviously can't connect to the server if the client is in a separate WIFI-network! So, make your client connect to the "WifiTest" AP first and see if your sketch works then.

If you are still having trouble then share the client's sketch.
User avatar
By Dan Kiefer
#38851 Thanks for your help, martinayotte and WereCatf.

As a newbie, I used the WiFiClient example, which doesn't specify the mode (AP or STA). So I guess that STA mode is the one that is by default? Meanwhile I can confirm that even without specifying the mode, it does work as STA. Probably it would be good practice not to depend on default values. But than the example code should be changed too.

Please find here the code of my client which connects correctly to the AP. Unfortunately the request with which I try to send information from client to server is still wrong. Can anybody help me with that?

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "WiFiTest";
const char* password = "";
const char* host = "192.168.4.1";
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}
 
void loop() {
  delay(5000);

  Serial.print("connecting to ");
  Serial.println(host);

  WiFiClient myClient;
  if (!myClient.connect("192.168.4.1", 80)) {
    Serial.println("connection failed");
    return;
  }

  String myRequestUrl;
  myRequestUrl = "http://192.168.4.1/?pin=BlinkON";

  Serial.print("Request URL: ");
  Serial.println(myRequestUrl);
  Serial.println();

  myClient.print(String("GET ") + myRequestUrl+ " HTTP/1.1\r\n" +
                  "Host: " + host + "\r\n" +
                  "Connection: close\r\n\r\n");
 
  delay(10);
  while(myClient.available()){
    String line= myClient.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}


Here is the Serial Monitor output:

Connecting to WiFiTest
......
WiFi connected
IP address: 192.168.4.3
connecting to 192.168.4.1
Request URL: http://192.168.4.1/?pin=BlinkON

HTTP/1.1 404 Not found
Content-Length: 139
Content-Type: text/html
Connection: close

<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>
closing connection



Thanks for your time!
Regards,
Dan