Last active
September 2, 2015 16:01
-
-
Save t2psyto/941f654ab18679b86b38 to your computer and use it in GitHub Desktop.
esp8266_wifi_httpclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simple HTTP get webclient test | |
*/ | |
#include <ESP8266WiFi.h> | |
const char* ssid = "myssid"; | |
const char* password = "mywifipass"; | |
const char* host = "www.adafruit.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); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("MAC address: "); | |
Serial.println(WiFi.macAddress()); | |
} | |
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)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/testwifi/index.html"; | |
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"); | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment