1

i am using a esp8266(esp-01s) with arduino uno and a screen. i am trying to get esp-01s to read an echo from a HTTPS website. but the esp connects to the domain(i think) but cant return the echo on serial monitor.. first tried using code for http and port 80 but then learned that it is different for https websites. my current code is from a github repository with minor changes. my php file only has an echo like echo "hello world"; the website is https://asuiot12.000webhostapp.com/esp8266test.php i don't think the serial speed is the issue cause i tried with 9600 baud rate also. with AT commands i only got till connecting with wifi but this code help me connect to domain. i have no sure way of knowing if its connected with domain. but the serial monitor doesn't respond with connection failed.

#include <ESP8266WiFi.h>

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

const char* host = "asuiot12.000webhostapp.com"; // only google.com not https://google.com

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

  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());
}

void loop() {
  delay(5000);

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

  // Use WiFiClient class to create TCP connections
  WiFiClientSecure client;
  const int httpPort = 443; // 80 is for HTTP / 443 is for HTTPS!
  
  client.setInsecure(); // this is the magical line that makes everything work
  
  if (!client.connect(host, httpPort)) { //works!
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/esp8266test.php";
  url += "?data=";
  url += "aaaa";


  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");

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

above code is programmed into esp-01s...you might see that i am using the most insecure way of getting the echo there are other ways like certificate validation etc but that echo is only going to converted to a qr-code on screen, so no issues. also if the code for sending that recieved echo to arduino uno could be shared would be tremendous help. and where it need to be programmed... if there is some better way of doing this then please advise i have zero experience with arduino,esp..

4
  • 1
    You appear to be writing the request header. Where do you read the response? – timemage Jun 15 at 13:10
  • i don't know much about arduino.... can you write the changes that might be needed. as this is the complete code. also am i writing the url correctly? – Khizar Jun 15 at 14:09
  • 1
    the Arduino IDE includes example code that you can start with ... your question is a general programming question, not really Arduino specific – jsotola Jun 15 at 15:02
  • 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.