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.