Simple Server & Client Error

I get the following error from the client when trying to do the HTTP GET :

Error code: -1

Door Status: --

The client shows it has logged into the server URL. I checked to see if URL malformed, seems fine
according to rules for encode. Server network running. No changes made to router for the
network.....IDE 2.1.0 being used.

Server code -


// Import required libraries
#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"


#include <Wire.h>

// Set your access point network credentials
const char* ssid = "Barn-Sensor-Access-Point";
const char* password = "123456789";

int D0_doorval = 0;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readDoor() {
  return String(digitalRead( D0 ));
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(9600);
  Serial.println();
  
  pinMode( D0, INPUT );
  pinMode( D3, OUTPUT );
  pinMode( D4, OUTPUT );

  digitalWrite( D3, LOW);
  digitalWrite( D4, LOW);  

  // Setting the ESP as an access point
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);
  //WiFi.softAP( ssid );
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

  server.on("/doorstatus", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDoor().c_str());
  });

  
  // Start server
  server.begin();
}
 
void loop() {
  
  // D0_doorval = digitalRead( D0 ); 
  // if ( D0_doorval == 1 ) {

  //   digitalWrite( D3, 1 ) ;
  //   digitalWrite( D4, 0 ) ;
  // } else {
  //   digitalWrite( D3, 0 ) ;
  //   digitalWrite( D4, 1 ) ;
  // }
  // Serial.println( D0_doorval );
  // delay( 5000 );
}

Client Code -


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;

const char* ssid = "Barn-Sensor-Access-Point";
const char* password = "123456789";

//Your IP address or domain name with URL path
const char* serverNameTemp = "http://Barn-Sensor-Access-Point/doorstatus";

String doorstatus;

unsigned long previousMillis = 0;
const long interval = 5000; 

void setup() {

  Serial.begin(9600);
  Serial.println();
  
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  //WiFi.begin( ssid ); 

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected to WiFi");
}

void loop() {

  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {
     // Check WiFi connection status
    if ((WiFiMulti.run() == WL_CONNECTED)) {
      doorstatus = httpGETRequest( serverNameTemp );

      Serial.println("Door Status: " + doorstatus);
      
      // save the last HTTP GET Request
      previousMillis = currentMillis;
    }
    else {
      Serial.println("WiFi Disconnected");
    }
  }
}
String httpGETRequest(const char* serverNameTemp ) {
  WiFiClient client;
  HTTPClient http;
    
  // Your IP address with path or Domain name with URL path 
  http.begin(client, serverNameTemp );
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "--"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

All help greatly appreciated.

Regards, Dana.

Your server does do nothing.

This was example on web, I stripped out his BME280 sensor stuff. So I thought
when server was started if it received a GET it would execute the server.on
POST when the client GET was invoked ? Newbee here so its all an adventure.....

Regards, Dana.

What do you mean by this?

use telnet or your brower and connect to your server. See what it answers. on the servers esp, use serial and send debug information to the arduino ide. debug.

This does not seem to help much.

SDK:2.2.2-dev(38a443e)/Core:3.1.2=30102000/lwIP:STABLE-2_1_3_RELEASE/glue:1.2-65-g06164fb/BearSSL:b024386

Setting AP (Access Point)…fpm close 1

mode : softAP(32:83:98:93:d1:b3)

add if1

dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)

bcn 100

wifi evt: 8

bcn 0

del if1

usl

I looked at codes for debug, and its setup, simply stuck cant see in debug
code any Client GET activity.

Regards, Dana.

The learning process (based on a working example) requires that every step of the example is strictly followed/carried out and marked as checked. Observation is made to see the expected results. After that the example is modified/adjusted to meet one's particular need.

Have you followed every step of the referred example? Surely not! You are saying that you have striped out the codes of the sensor.

The BME280 sensor is now irrelevant. Its not part of the code in any fashion.
There is nothing in the HTTP GET setup asking for it.

However I get the following debug info out of the client :

Door Status: --

[hostByName] request IP for: Barn-Sensor-Access-Point

[hostByName] Host: Barn-Sensor-Access-Point lookup error: Timeout (-3)!

Error code: -1

Ultimately doorstatus is just the state of a GPIO pin. The server is functioning,
as far as I can tell, and the f() call to read and pass to the server is just too simple
for me to screw up, but then I can do that to just about anything depending on
time of day :slight_smile:

Regards, Dana.

well, then get your name resolution working.

The server is running as a SoftAP, so I do not see where there is a DNS naming conflict
which I think you are alluding to ?

But I see the server is assigned to 192.168.4.1 and the Client as follows :slight_smile:

state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt 

connected with BarnSensorAccessPoint, channel 1
dhcp client start...
wifi evt: 0
ip:192.168.4.2,mask:255.255.255.0,gw:192.168.4.1
wifi evt: 3
.
Connected to WiFi
[WIFIM] Connecting saved WiFi
scandone
[WIFIM] Connected:
[WIFIM]   SSID: BarnSensorAccessPoint
[WIFIM]   IP:   192.168.4.2
[WIFIM]   MAC:  32:83:98:93:D1:B3
[WIFIM]   CH:   1
[WIFIM]   RSSI: -26
[hostByName] request IP for: BarnSensorAccessPoint
[hostByName] Host: BarnSensorAccessPoint lookup error: Timeout (-3)!
Error code: -1
pm open,type:2 0
Door Status: --


Regards, Dana.

I am getting correct response from the Server.

PortPinStatusD0: 1
PortPinStatusD0: 0
PortPinStatusD0: 0
PortPinStatusD0: 1
PortPinStatusD0: 1
PortPinStatusD0: 1
PortPinStatusD0: 1

Thanks all for the help. Problem was solved by starting from
scratch.

Regards, Dana.

Then what were the problems/mistakes you introduced in the original sketches of the tutorial during simplification by eliminating the sensor and substituting D0-pin? You are requested to spot them and post them.

I have already detected few mistakes which when corrected, the Server started to respond correctly.

Can you tell what is wrong in the above declaration in the Client Sketch of your post #1?

This is my Client Sketch taken from the same tutorial as yours' and modified just to handle the status of D0-pin of the Server.:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;

const char* ssid = "ESP8266-Access-Point";
const char* password = "123456789";

//Your IP address or domain name with URL path
const char* serverNamePort = "http://192.168.4.1/portPinStatusD0";
String portPinStatusD0;

unsigned long previousMillis = 0;
const long interval = 2000;

void setup()
{
  Serial.begin(115200);
  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("Connected to WiFi");
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // Check WiFi connection status
    if ((WiFiMulti.run() == WL_CONNECTED)) {
      portPinStatusD0 = httpGETRequest(serverNamePort);
      Serial.println("PortPinStatusD0: " + portPinStatusD0);
      // save the last HTTP GET Request
      previousMillis = currentMillis;
    }

    else 
    {
      Serial.println("WiFi Disconnected");
    }
  }
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;

  // Your IP address with path or Domain name with URL path
  http.begin(client, serverName);

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "--";

  if (httpResponseCode > 0) 
  {
    //Serial.print("HTTP Response code: ");
    //Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

@danadak
try make a webrequest by your web browser on your phone/PC
1)connect to AP
2)enter the ip
3) tell us what did you got

This is working code.

Server :slight_smile:


// Import required libraries
#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"

#include <Wire.h>

// Set your access point network credentials
const char* ssid = "Cabin-Sensor-Access-Point";
const char* password = "123456789";

int D0_doorval = 0;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readDoor() {
  return String(digitalRead( D0 ));
  //return String( D0 GPIO Status );
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin( 9600 );
  Serial.println();
  
  pinMode( D0, INPUT );
  pinMode( D3, OUTPUT );
  pinMode( D4, OUTPUT );
  digitalWrite( D3, 0 ) ;
  digitalWrite( D4, 0 ) ;

  // Setting the ESP as an access point
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

  server.on("/door", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readDoor().c_str());
  });
  
  bool status;

  // default settings
  // (you can also pass in a Wire library object like &Wire2)

  
  // Start server
  server.begin();
}
 
void loop(){

  D0_doorval = digitalRead( D0 ); 
  if ( D0_doorval == 1 ) {

    digitalWrite( D3, 1 ) ;
    digitalWrite( D4, 0 ) ;
    Serial.println( "Door Closed" );
  } else {
    digitalWrite( D3, 0 ) ;
    digitalWrite( D4, 1 ) ;
    Serial.println( "Door Open" );
  }



  delay( 1000 );
}



Client -

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>

#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;

const char* ssid = "Cabin-Sensor-Access-Point";
const char* password = "123456789";

//Your IP address or domain name with URL path
const char* serverNameTemp = "http://192.168.4.1/door";

String door;

unsigned long previousMillis = 0;
const long interval = 5000;

int D0_doorval = 0;

void setup() {
Serial.begin(9600);
Serial.println();

pinMode( D0, INPUT );
pinMode( D3, OUTPUT );
pinMode( D4, OUTPUT );
digitalWrite( D3, 0 ) ;
digitalWrite( D4, 0 ) ;

Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Connected to WiFi");
}

void loop() {

if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if ((WiFiMulti.run() == WL_CONNECTED)) {
door = httpGETRequest(serverNameTemp);
//Serial.println("Door: " + door);

  // save the last HTTP GET Request
  previousMillis = currentMillis;
}
else {
  Serial.println("WiFi Disconnected");
}

}
delay( 5000 );

}

String httpGETRequest(const char* ssid) {

WiFiClient client;
HTTPClient http;

// Your IP address with path or Domain name with URL path
http.begin(client, ssid);

// Send HTTP POST request
int httpResponseCode = http.GET();

String payload = "--";

if (httpResponseCode>0) {
//Serial.print("HTTP Response code: ");
//Serial.println(httpResponseCode);
payload = http.getString();

D0_doorval = payload.toInt( ); 
if ( D0_doorval == 1 ) {

  digitalWrite( D3, 1 ) ;
  digitalWrite( D4, 0 ) ;
  Serial.println( "Door Closed" );
} else {
  digitalWrite( D3, 0 ) ;
  digitalWrite( D4, 1 ) ;
  Serial.println( "Door Open" );
} 

} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();

return payload;
}

For some reason the edit is not taking the 2'ond code post tags.....

Do not understand why server name cannot be used in the client definition of name,
client must have IP address typed. Even though, when using server name, client connects to the
server as reported by debug but no longer GETs the server data. That forces one, I gather, to 
force a static IP onto server. Ugh !

I am closing the thread now. Thanks all.


Regards, Dana.

This is the line that was different in your post #1.

The Server has created the WiFi Network. To join into that network, the Client must know the ssid and password of the Network initiator.

Now, you want to interact with the Server for data exchange, you need to use the local/private "IP Address" of the Server. This is how the network works. Here, the Server is not a member of World Wide Web.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.