Serial communication with ESP01

Greetings!

I'm trying to give some inputs to my ESP01 through serial monitor of arduino IDE. Following is the code I'm using

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>

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

 const char* host = "ec2-13-126-239-35.ap-south-1.compute.amazonaws.com";//:8080/put/value-to-be-inserted

String path = "/put/";
String sensorData;

void setup()
{
 
Serial.begin(115200);
delay(100);

 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.println("IP address: ");
 Serial.println(WiFi.localIP());
 }

 int value = 0;
 
void loop() 
{
  if(Serial.available())
  {
   sensorData = Serial.readStringUntil('\n');
   httpRequest(sensorData);
  }
}

void httpRequest(String Data)
{
  Serial.print("connecting to ");
  Serial.println(host);
  WiFiClient client;
  const int httpPort = 8080;

  if (!client.connect(host, httpPort))
  {
    Serial.println("connection failed");
    return;
  }
  client.println("GET /put/" +
String(Data) + " HTTP/1.1");
 client.println("Host: ec2-13-126-239-35.ap-south-1.compute.amazonaws.com");
 client.println("User-Agent: ArduinoESP/1.1");
 client.println("Connection: close");
 client.println();
 /* client.print(String("GET ") + path + data + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: keep‐alive\r\n\r\n");*/
  delay(500); // wait for server to respond
  // read response
  int dir = 0; 
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
    /////////////////////////////////////////////////////////////////////
    // NOTE: we’ll replace the line above with something to parse the 
    // JSON response here!
    /////////////////////////////////////////////////////////////////////
  }
  Serial.print("closing connection. ");
}

The response I get is

connecting to ec2-13-126-239-35.ap-south-1.compute.amazonaws.com
HTTP/1.1 400 
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 435
Date: Mon, 25 Jan 2021 14:44:56 GMT
Connection: close

<!doctype html><html lang="en"><head><title>HTTP Status 400 – Bad Request</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 400 – Bad Request</h1></body></html>closing connection.

What mistake I'm doing? or what do I need to change in my code?

I would expect amazon to require https not http
don't they?

have you tested your URL "manually" in a browser ?

There is no way to see what the mistake is because half the information is missing. You are entering part of the request manually through the serial monitor but we can't see what you typed.

I suggest (unless it is proprietary information) you put a Serial.println following

client.println("GET /put/" +
String(Data) + " HTTP/1.1");

with the exact same information so the serial monitor actually shows you (and us) the EXACT line being sent.

It can be as easy as a space too many or too few.

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