Skip to content

Instantly share code, notes, and snippets.

@johnosullivan
Last active February 1, 2019 15:33
Show Gist options
  • Save johnosullivan/4f3c2a4314401318502bde45f9b3b2f5 to your computer and use it in GitHub Desktop.
Save johnosullivan/4f3c2a4314401318502bde45f9b3b2f5 to your computer and use it in GitHub Desktop.
Simple TCPClient HTTP GET Request
TCPClient client;
char buffer[1024];
char * http_get(char const * hostname, int port, String path) {
// Send Request
if (client.connect(hostname, port)) {
client.print("GET ");
client.print(path);
client.print(" HTTP/1.1\r\n");
client.print("HOST: ");
client.println(hostname);
client.print("\r\n\r\n\r\n");
} else {
client.stop();
return NULL;
}
int i,j,k = 0;
uint32_t lastRead = millis();
while ((millis() - lastRead) < 1000) {
while (client.available()) {
char c = client.read();
Serial.print(c);
lastRead = millis();
}
}
client.flush();
client.stop();
return buffer;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
char * response = http_get("192.168.1.51", 3000, "/ping");
Serial.println(response);
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment