The problem using client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");

Good afternoon all
hopefully this is a very simple problem, I’m using a ESP 32 what I’m trying to achieve is send a command (192.168.1.210/slider1?value=29) to my fan controlle also another (ESP32)
if I use a web browser and just type the command in this works fine.
But if I try and use the program nothing appears to happen it doesn’t appear to be sending anything?
Here is the loop part of the code that is sending the data.

void loop() {
	ts.execute();
	watchdog();
   // Read Joystick values and map to values of 0 - 255
  joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 4095, 0, 64);
  joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 4095, 0, 64);
  joystick[2] = digitalRead(Button_Switch) ;
 
  //Display the joystick values in the serial monitor.
/*  Serial.println("-----------");
  Serial.print("x:");
  Serial.print(joystick[0]);
  Serial.print(" : y:");
  Serial.print(joystick[1]);
	Serial.print(" : s:");
  Serial.println(joystick[2]);
*/
	delay(500);
	if(x<=10){
		x++;
	}
	else {
		x=0;
		/*UploadData="";
		UploadData= String("/slider1?value=");
		UploadData+=String(joystick[0]); 
		SendHttpPOST();
		*/
		UploadData="";
		UploadData= String("192.168.1.210");
		UploadData+= String("/slider1?value=");
		UploadData+=String(joystick[0]);
		Serial.println(UploadData);
		if (client.connect(ServerHost1,80)){
			client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
      client.println("Host: "+String("192.168.1.210"));
      delay(500); // Essential delay for ESP32 
      client.println F("Connection: close");
			Serial.println F("Connection: close"); 
      client.stop();
		}  
	}
}

Any suggestions would be much appreciated
if you would like to look at the full code please see below
Thank you

#include <Arduino.h>
#include "soc/timer_group_struct.h" 
#include "soc/timer_group_reg.h"
#define _TASK_INLINE
#include <TaskScheduler.h> 
#include <WiFi.h>
#include "WebServer.h"
#include <Preferences.h>
#include <esp_task_wdt.h>

// Connect to the server with http://fanserver.local/ e.g. name = "myserver" then use address http://myserver.local/

IPAddress local_IP(192, 168, 1, 205);
IPAddress gateway(192, 168, 1, 254);    // Set your network Gateway usually your Router base address
IPAddress subnet(255, 255, 255, 0); 
IPAddress dns1(192,168,1,254);           // Set your network DNS usually your Router base address
IPAddress dns2(8,8,8,8);
WebServer server(80);
WiFiClient client;
Scheduler ts;
Preferences preferences;

const char*  hostname  = "Joystick";
const char*  ServerHost1  = "192.168.1.210";
const char*  ServerHost2  = "192.168.1.118";
#define WDT_TIMEOUT 4
// Define Joystick Connections
#define JoyStick_X_PIN     36 
#define JoyStick_Y_PIN     39
#define Button_Switch 17
// Declare unsigned 8-bit joystick array
uint16_t joystick[3]; 

const char* ssid = "BTHub6-******";
const char* password = "JL488888888888";

int Rssi=0;
String UploadData;
unsigned int x=0;
//################# Voids  
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void motor_print();
uint8_t StartWiFi();
void watchdog_setup();
void watchdog();
void Web_Listen();
void Rssi_Update();
void SendHttpPOST();
void SendHttpPOST();

//#####################
Task Web_Listen1(305,TASK_FOREVER , &Web_Listen,&ts,true);
Task watchdog1(220, TASK_FOREVER, &watchdog,&ts,true);
//#############################################################################################
void setup() {
  Serial.begin(115200);
  Serial.println();
	TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; 
	TIMERG0.wdt_feed=1; TIMERG0.wdt_wprotect=0;
	preferences.begin("credentials", false);
	pinMode(Button_Switch,INPUT_PULLUP );
	if (StartWiFi() == WL_CONNECTED) {         // Start the WiFi service
    Serial.println("WiFi Started...");
  }
  watchdog_setup();	//Setup watchdog
}
//#############################################################################################
void loop() {
	ts.execute();
	watchdog();
   // Read Joystick values and map to values of 0 - 255
  joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 4095, 0, 64);
  joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 4095, 0, 64);
  joystick[2] = digitalRead(Button_Switch) ;
 
  //Display the joystick values in the serial monitor.
/*  Serial.println("-----------");
  Serial.print("x:");
  Serial.print(joystick[0]);
  Serial.print(" : y:");
  Serial.print(joystick[1]);
	Serial.print(" : s:");
  Serial.println(joystick[2]);
*/
	delay(500);
	if(x<=10){
		x++;
	}
	else {
		x=0;
		/*UploadData="";
		UploadData= String("/slider1?value=");
		UploadData+=String(joystick[0]); 
		SendHttpPOST();
		*/
		UploadData="";
		UploadData= String("192.168.1.210");
		UploadData+= String("/slider1?value=");
		UploadData+=String(joystick[0]);
		Serial.println(UploadData);
		if (client.connect(ServerHost1,80)){
			client.println("GET /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
      client.println("Host: "+String("192.168.1.210"));
      delay(500); // Essential delay for ESP32 
      client.println F("Connection: close");
			Serial.println F("Connection: close"); 
      client.stop();
		}  
	}
}
//#############################################################################################
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
//#############################################################################################
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Disconnected from WiFi access point");
  Serial.print("WiFi lost connection. Reason: ");
  Serial.println(info.disconnected.reason);
  Serial.println("Trying to Reconnect");
  WiFi.begin(ssid, password);
}	
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Connected to AP successfully!");
}
//#############################################################################################
uint8_t StartWiFi() {
  Serial.print("\r\nConnecting to: "); Serial.println(String(ssid));
  
  WiFi.disconnect();
  delay(1200);
  WiFi.mode(WIFI_STA); // switch off AP
  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
  WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
	WiFi.config(local_IP, gateway, subnet, dns1 ,dns2);
  WiFi.setHostname(hostname); //define hostname
	WiFi.begin(ssid, password);
  unsigned long start = millis();
  uint8_t connectionStatus;
  bool   AttemptConnection = true;
  while (AttemptConnection) {
    connectionStatus = WiFi.status();
    if (millis() > start + 15000) { // Wait 15-secs maximum
      AttemptConnection = false;
    }
    if (connectionStatus == WL_CONNECTED || connectionStatus == WL_CONNECT_FAILED) {
      AttemptConnection = false;
    }
    delay(50);
  }
  if (connectionStatus == WL_CONNECTED) {
    Serial.println("WiFi connected at: " + WiFi.localIP().toString());
		WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
  	WiFi.onEvent(WiFiGotIP, SYSTEM_EVENT_STA_GOT_IP);
  	WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);
  }
	  server.on("/test", []()  {server.send(200, "text/plain", "Server status is OK"); }); // Simple server test by providing a status response
	server.begin();
  return connectionStatus;
}
//#############################################################################################
void Web_Listen(){
server.handleClient();
}
//###################################### Watchdog ###########################################
void watchdog(){
  esp_task_wdt_reset();  //Feed the watchdog
}
//#############################################################################################
void watchdog_setup(){
	Serial.print("Configuring Watchdog Timeout for ");
	Serial.print(WDT_TIMEOUT);
	Serial.println(" Seconds");
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch	
}
//#############################################################################################
void Rssi_Update(){
	int xx=0;
  Rssi = WiFi.RSSI();
	Rssi = 90 / 40.0 * Rssi + 212.5;
	if (Rssi > 100) Rssi = 100;
	//RssiS= Rssi + String(Rssi, 0) + " %";
  Serial.print("RSSI:");
  Serial.println(Rssi);
  //ThingSpeak.setField(2,Rssi );
  //xx=ThingSpeak.writeFields(myChannelNumber1, myWriteAPIKey1);
  if(xx == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(xx));
    //Rssi_Fail();
  }
}
//#############################################################################################
/*
void CollectUploadData() {
  Serial.println("...Submitting Upload request");
  UploadData  = "sensor?Sensor="+String(SensorNumber);
  UploadData += "&temperature="+String(Temperature);
  UploadData += "&humidity="+String(Humidity);
  UploadData += "&BMP-temperature="+String(Temperature);
  UploadData += "&pressure="+String(Humidity);
  UploadData += "&spare="+String(Lightv);
  UploadData += "&sensortype="+String("SHTC3");
  Serial.println("...Information to be uploaded: "+UploadData);
	SendHttpPOST();
	//Thingspeak_Send();
}
*/
//############################################################################################ 
void SendHttpPOST() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print F("Attempting connection...");
    if (client.connect(ServerHost1,80)){
      Serial.println F("connected");
      client.println("PUT /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
      client.println("Host: "+String("192.168.1.210"));
      delay(500); // Essential delay for ESP32 
      client.println F("Connection: close");
			Serial.println F("Connection: close"); 
      client.stop();
      return;
    } 
    else {
      Serial.println F("try again in 2 Seconds");
			//Thingspeak_Send_Fail();
    }
	}
}

This is not in the correct format for a HTTP request.

  1. The GET URL does not contain the the host.
  2. After you do a Conection: clone\r\n\r\n you try to send more HTTP headers (Host:), but with the double \r\n you’ve already closed ended the HTTP request
  3. After that there’s invalid C++ code client.println F("Connection: close"); should not compile since there are missing ( ) around the argument.

The request should look like e.g.

GET /slider1?value=100 HTTP/1.1
Host: 192.168.1.210
Connection: close

Try and adapt the code for that.

maxrerhardt
thank youd for your suggestion unfortunately it is still not working correctly.
I will update the code, I’m also getting this message in the terminal screen

/button?output=0&state=0
[E][WiFiClient.cpp:288] setSocketOption(): 1006 : 9
Connected!
Connection: close

if you have any further ideas these will be much appreciated.
Thanking you in advance.

#include <Arduino.h>
#include "soc/timer_group_struct.h" 
#include "soc/timer_group_reg.h"
#define _TASK_INLINE
#include <TaskScheduler.h> 
#include <WiFi.h>
#include "WebServer.h"
#include <Preferences.h>
#include <esp_task_wdt.h>

// Connect to the server with http://fanserver.local/ e.g. name = "myserver" then use address http://myserver.local/

IPAddress local_IP(192, 168, 1, 205);
IPAddress gateway(192, 168, 1, 254);    // Set your network Gateway usually your Router base address
IPAddress subnet(255, 255, 255, 0); 
IPAddress dns1(192,168,1,254);           // Set your network DNS usually your Router base address
IPAddress dns2(8,8,8,8);
WebServer server(80);
WiFiClient client;
Scheduler ts;
Preferences preferences;

const char*  hostname  = "Joystick";
const char*  ServerHost1  = "192.168.1.210";
const char*  ServerHost2  = "192.168.1.118";
#define WDT_TIMEOUT 4
// Define Joystick Connections
#define JoyStick_X_PIN     36 
#define JoyStick_Y_PIN     39
#define Button_Switch 17
// Declare unsigned 8-bit joystick array
uint16_t joystick[3]; 

const char* ssid = "BTHub6******";
const char* password = "************";

int Rssi=0;
String UploadData;
unsigned int x=0;
//################# Voids  
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void motor_print();
uint8_t StartWiFi();
void watchdog_setup();
void watchdog();
void Web_Listen();
void Rssi_Update();
void SendHttpPOST();
void SendHttpPOST();
void WiFi_ON();

//#####################
Task Web_Listen1(305,TASK_FOREVER , &Web_Listen,&ts,true);
Task watchdog1(220, TASK_FOREVER, &watchdog,&ts,true);
//#############################################################################################
void setup() {
  Serial.begin(115200);
  Serial.println();
	TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; 
	TIMERG0.wdt_feed=1; TIMERG0.wdt_wprotect=0;
	preferences.begin("credentials", false);
	pinMode(Button_Switch,INPUT_PULLUP );
/*	if (StartWiFi() == WL_CONNECTED) {         // Start the WiFi service
    Serial.println("WiFi Started...");
  }*/
	WiFi_ON();
  watchdog_setup();	//Setup watchdog
}
//#############################################################################################
void loop() {
	ts.execute();
	watchdog();
   // Read Joystick values and map to values of 0 - 255
  joystick[0] = map(analogRead(JoyStick_X_PIN), 0, 4095, 0, 64);
  joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 4095, 0, 64);
  joystick[2] = digitalRead(Button_Switch) ;
 
  //Display the joystick values in the serial monitor.
/*  Serial.println("-----------");
  Serial.print("x:");
  Serial.print(joystick[0]);
  Serial.print(" : y:");
  Serial.print(joystick[1]);
	Serial.print(" : s:");
  Serial.println(joystick[2]);
*/
	delay(500);
	if(x<=10){
		x++;
	}
	else {
		x=0;

		UploadData="";
		//UploadData= String("/slider1?value=");
		UploadData= String("/button?output=0&state=0");
		//UploadData+=String(joystick[0]);
		Serial.println(UploadData);
		client.setTimeout(10000);

		if (!client.connect(ServerHost1,80)){
			Serial.println(F("Connection failed"));
    	return;
  	}
		Serial.println(F("Connected!"));

		//client.println("GET /UploadData HTTP/1.1");
		client.println("GET /"+UploadData+" HTTP/1.1 ");

		client.println(F("Host: ServerHost1"));

    delay(500); // Essential delay for ESP32 
    client.println (F("Connection: close"));
		Serial.println (F("Connection: close")); 

		if (client.println() == 0) {
    	Serial.println(F("Failed to send request"));
    	client.stop();
    	return;
  	}

	  // Check HTTP status
  	char status[32] = {0};
  	client.readBytesUntil('\r', status, sizeof(status));
  	if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
    	Serial.print(F("Unexpected response: "));
    	Serial.println(status);
    	client.stop();
    	return;
  	}
	  // Skip HTTP headers
  	char endOfHeaders[] = "\r\n\r\n";
  	if (!client.find(endOfHeaders)) {
    	Serial.println(F("Invalid response"));
    	client.stop();
    	return;
  	}

  	client.stop();  	
	}
}
//#############################################################################################
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
//#############################################################################################
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Disconnected from WiFi access point");
  Serial.print("WiFi lost connection. Reason: ");
  Serial.println(info.disconnected.reason);
  Serial.println("Trying to Reconnect");
  WiFi.begin(ssid, password);
}	
//#############################################################################################
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Connected to AP successfully!");
}
//#############################################################################################
void WiFi_ON(){
	uint8_t connectionStatus; 
   if (!WiFi.config(local_IP, gateway, subnet, dns1, dns2)) { 
    Serial.println(F("WiFi STATION Failed to configure Correctly")); 
  }

  Serial.print(F("Connecting to "));
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(F("Trying to connect to Wifi Network"));
  }
  Serial.println(F(""));
  Serial.println(F("Successfully connected to WiFi network"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());
	connectionStatus = WiFi.status();
  if (connectionStatus == WL_CONNECTED) {
    Serial.println("WiFi connected at: " + WiFi.localIP().toString());
		WiFi.onEvent(WiFiStationConnected, SYSTEM_EVENT_STA_CONNECTED);
  	WiFi.onEvent(WiFiGotIP, SYSTEM_EVENT_STA_GOT_IP);
  	WiFi.onEvent(WiFiStationDisconnected, SYSTEM_EVENT_STA_DISCONNECTED);
	}	
  ////////////////////  My setup onwards   /////////////////////////////////
  server.on("/test", []()  {server.send(200, "text/plain", "Server status is OK"); }); // Simple server test by providing a status response

  server.begin();
  Serial.println(F("HTTP server started"));
}

//#############################################################################################
void Web_Listen(){
server.handleClient();
}
//###################################### Watchdog ###########################################
void watchdog(){
  esp_task_wdt_reset();  //Feed the watchdog
}
//#############################################################################################
void watchdog_setup(){
	Serial.print("Configuring Watchdog Timeout for ");
	Serial.print(WDT_TIMEOUT);
	Serial.println(" Seconds");
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch	
}
//#############################################################################################
void Rssi_Update(){
	int xx=0;
  Rssi = WiFi.RSSI();
	Rssi = 90 / 40.0 * Rssi + 212.5;
	if (Rssi > 100) Rssi = 100;
	//RssiS= Rssi + String(Rssi, 0) + " %";
  Serial.print("RSSI:");
  Serial.println(Rssi);
  //ThingSpeak.setField(2,Rssi );
  //xx=ThingSpeak.writeFields(myChannelNumber1, myWriteAPIKey1);
  if(xx == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(xx));
    //Rssi_Fail();
  }
}
//#############################################################################################
/*
void CollectUploadData() {
  Serial.println("...Submitting Upload request");
  UploadData  = "sensor?Sensor="+String(SensorNumber);
  UploadData += "&temperature="+String(Temperature);
  UploadData += "&humidity="+String(Humidity);
  UploadData += "&BMP-temperature="+String(Temperature);
  UploadData += "&pressure="+String(Humidity);
  UploadData += "&spare="+String(Lightv);
  UploadData += "&sensortype="+String("SHTC3");
  Serial.println("...Information to be uploaded: "+UploadData);
	SendHttpPOST();
	//Thingspeak_Send();
}
*/
//############################################################################################ 
void SendHttpPOST() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print F("Attempting connection...");
    if (client.connect(ServerHost1,80)){
      Serial.println F("connected");
      client.println("PUT /"+UploadData+" HTTP/1.1\r\nConnection: close\r\n\r\n");
      client.println("Host: "+String("192.168.1.210"));
      delay(500); // Essential delay for ESP32 
      client.println F("Connection: close");
			Serial.println F("Connection: close"); 
      client.stop();
      return;
    } 
    else {
      Serial.println F("try again in 2 Seconds");
			//Thingspeak_Send_Fail();
    }
	}
}

There’s gonna be a double / at the start (GET //button?), there must only be one. Also the space at the end of the string might be wrong.

Delaying in the middle of request data seems weird. Should be be at the end maybe.

There also isn’t a double \r\n at the end of the request anymore. After sending Connection: close, do another client.println() to print that.

Thank you again for your help
this code appears to be working now but if you see anything obviously wrong please don’t hesitate to call back

		if (!client.connect(ServerHost1,80)){
			Serial.println(F("Connection failed"));
    	return;
  	}
		Serial.println(F("Connected!")); 

    if (client.connect(ServerHost1,80)){
    	Serial.println (F("connected"));
     	client.println("GET /"+UploadData+" HTTP/1.1 ");
     	client.println("Host: "+String(ServerHost1)); 
     	client.println (F("Connection: close")); 
			 client.println();
     	return;

Except for bad indentation / formatting of source code, looks alright :slight_smile: (Ctrl+Shift+P → Format document is a handy shortcut)