0

I am trying to upload information to my appspot created with Google App Engine. I am able to do it when I in my browser type:
http://myname.appspot.com/query?city=Copenhagen&temp=20&id=website
But I cannot get it to work on the ESP8266. Here is my code:

const char* MY_URL = "http://myname.appspot.com";

void uploadHTTP() {
    // Define the WiFi Client
    WiFiClient client;
    // Set the http Port
    const int httpPort = 80;

    // Make sure we can connect
    if (!client.connect(MY_URL, httpPort)) {
        return;
    }
    else {
        Serial.println("Connected to MY_URL");
    }

    String url = "/query?city=Copenhagen&temp=20&id=ESP8266";

    // Post to appspot
    if (client.connect(MY_URL, httpPort)) {

        // Sent HTTP POST Request
        client.println("POST " + url + " HTTP/1.1");
        Serial.println("POST " + url + " HTTP/1.1");
        client.println("Host: " + String(MY_URL));
        Serial.println("Host: " + String(MY_URL));
        client.println("User-Agent: Arduino/1.0");
        Serial.println("User-Agent: Arduino/1.0");
        client.print("Content-Length: ");
        Serial.print("Content-Length: ");
        client.println(0);
        Serial.println(0);
        client.println("Content-Type: application/x-www-form-urlencoded");
        Serial.println("Content-Type: application/x-www-form-urlencoded");
        client.println("Connection: close");
        Serial.println("Connection: close");
        }
    //Done
    Serial.println("");
    Serial.println("my HTTP done");
    Serial.println("");
}
  • Are you programming the ESP8266 directly, or using it connected to an Arduino? – Majenko Oct 9 '18 at 10:24
2

Your biggest problem is that you are trying to use a website URL as a FQDN. That's not going to work.

When you connect to a remote machine you connect to the name (myname.appspot.com) which gets translated into an IP address for you to open a connection to.

When you use a web browser it parses the URL to extract the protocol (http) and the FQDN (myname.appspot.com) and uses the protocol to determine the port (and what language to speak) to connect to, then look up the IP address from the FQDN.

You need to drop the http:// from your FQDN which is causing it to break (there is no host called http://myname.appspot.com). The fact that you are connecting to port 80 and speaking the HTTP language implies that you are using HTTP.

To put it into more layman's terms: if you "need to call Fred to talk about going to the pub" you first look up fred's number in the phone book then call that number. Then only once he has answered do you talk about going to the pub. What you don't do is try and find the number to call by looking in the phone book under "talk about going to the pub with Fred". It won't be there.

If you are programming your ESP8266 directly you should consider using the much simpler bundled ESP8266HTTPClient.h library.

  • Thank you for the quick answer and the explanation - I did not know that! However I tried to change it to "myname.appspot.com" but it did not work? I have also been trying to get it to work with ESP8266HTTPClient.h library but I could not get that to work either... – T.Krebs Oct 9 '18 at 11:11
  • Is my 'Content-type' correct? Here is my code for the ESP8266HTTPClient.h: ['code'] (HTTPClient http;) http.begin("myname.appspot.com"); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.POST("/query?city=Copenhagen&temp=20&id=ESP8266"); Sorry for the bad layout - I am new to this forum and cant get the comment to work.... – T.Krebs Oct 9 '18 at 11:19
  • You have no content, so content type is meaningless. You should really be doing a GET request not a POST request if there is no content to POST. – Majenko Oct 9 '18 at 11:21
  • I have a content? I am setting the 'city' to 'Copenhagen', 'temp' to '20' and 'id' to 'ESP8266' – T.Krebs Oct 9 '18 at 11:24
  • No, that's not content. Content goes in the body. You have URL parameters. URL parameters go on the end of the URL. You have no body, and zero content length, and your parameters are on the end of the URL. That is not "content". – Majenko Oct 9 '18 at 11:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.