Last active
November 2, 2017 09:32
-
-
Save yutashi/1313444a3173e835c33602b7c8c25f24 to your computer and use it in GitHub Desktop.
ESP8266 HTTP GET via WiFi
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
#include <ESP8266HTTPClient.h> | |
#include <ESP8266WiFi.h> | |
char ssid[] = "<YOUR SSID>"; | |
char password[] = "<YOUR PSK>"; | |
void setup() { | |
Serial.begin(115200); | |
while(!Serial) {} | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(100); | |
} | |
Serial.println("Connected!"); | |
String result = fetchMessage("<ENDPOINT URL>"); | |
Serial.println(result); | |
WiFi.disconnect(); | |
Serial.println("Disconnected!"); | |
} | |
String fetchMessage(char host[]) { | |
HTTPClient http; | |
http.begin(host); | |
int httpCode = http.GET(); | |
String payload = ""; | |
if (httpCode > 0) { | |
Serial.printf("HTTP GET ... code: %d\n", httpCode); | |
Serial.println(HTTP_CODE_OK); | |
if (httpCode == HTTP_CODE_OK) { | |
payload = http.getString(); | |
} | |
} else { | |
Serial.printf("HTTP GET failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
return payload; | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment