2

I'm trying to send a standard http GET request to my website using the NodeMCU example client code however for some reason it is not working.

If I type into my browser: snackrefill.com/GetData.php?username=aaaa&pin=bbbb&cost=cccc then it will successfully connect with the php script and save a data base entry.

The problem is that when I try to do the same using the NodeMCU module it does not work. it seems to be connecting to WiFi and the server just fine but when it sends the request nothing seems to happen.

Am I structuring my request wrong?

#include <ESP8266WiFi.h>

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

const char* host = "ns8451.hostgator.com";

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

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password); //works!

  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() {
  delay(5000);
  ++value;

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

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) { //works!
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/GetData.php";
  url += "?username=";
  url += "aaaa";
  url += "&pin=";
  url += "bbbb";
  url += "&cost=";
  url += "cccc";

  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");
}
4
  • This is definitively not C. Is it C++? In any case, I don't think your problem has to do with the programming language, so I just remove the tag. Commented Aug 18, 2017 at 20:12
  • okay no problem, i just put that so the code would be colored Commented Aug 18, 2017 at 20:13
  • 2
    You can do that by adding the following line <!-- language: c++ --> before your code. I have added it already for now. Commented Aug 18, 2017 at 20:16
  • 1
    what happens when you put your "snackrefill.com/…" instead of your url in client.print ???? let me know Commented Nov 5, 2017 at 1:14

1 Answer 1

3

The reason it was not sending was because the host was wrong, i was pointing to my website nameserver when i should have pointed it to the regular website name. I changed const char* host = "ns8451.hostgator.com"; to const char* host = "snackrefill.com"; and now it works!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.