Last active
April 16, 2017 21:51
-
-
Save omiq/ecc41c51ea85340819500a2c81e6673f to your computer and use it in GitHub Desktop.
Connect to a website and shine an LED using ESP32
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
// wifi library | |
#include <WiFi.h> | |
// WiFi network name and password: | |
const char * networkName = "YOUR_WIFI_HERE"; | |
const char * networkPswd = "PASSWORD_HERE"; | |
// The domain to request from: | |
const char * hostDomain = "chrisg.org"; | |
const int hostPort = 80; | |
// whichever pin your LED is attached | |
const int LED_PIN = 5; | |
void setup() | |
{ | |
// set the baud rate: | |
Serial.begin(115200); | |
// set the LED pin to output | |
pinMode(LED_PIN, OUTPUT); | |
// Function for the wifi, see below | |
connectToWiFi(networkName, networkPswd); | |
} | |
// do this forever | |
void loop() | |
{ | |
// LED on | |
digitalWrite(LED_PIN, HIGH); | |
// get the web page | |
requestURL(hostDomain, hostPort); | |
// LED off | |
digitalWrite(LED_PIN, LOW); | |
// wait a second | |
delay(1000); | |
} | |
// function to connect | |
void connectToWiFi(const char * ssid, const char * pwd) | |
{ | |
// init the LED state | |
int ledState = 0; | |
// connecting message | |
Serial.println("Connecting to your network: " + String(ssid)); | |
// start the wifi | |
WiFi.begin(ssid, pwd); | |
// loop while not connected | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
// Blink LED | |
digitalWrite(LED_PIN, ledState); | |
// switch state between on and off | |
ledState = (ledState + 1) % 2; | |
// wait half a second | |
delay(500); | |
// dots | |
Serial.print("."); | |
} | |
// blank line | |
Serial.println(); | |
// yay! | |
Serial.println("WiFi connected!"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
// grab the html | |
void requestURL(const char * host, uint8_t port) | |
{ | |
// status | |
Serial.println("Connecting to domain: " + String(host)); | |
// Use WiFiClient to get TCP connection | |
WiFiClient client; | |
if (!client.connect(host, port)) | |
{ | |
Serial.println("connection failed"); | |
return; | |
} | |
// connected! | |
Serial.println("Connected!"); | |
// send the HTTP GET request | |
client.print((String)"GET / HTTP/1.1\r\n" + | |
"Host: " + String(host) + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
// timeout | |
unsigned long timeout = millis(); | |
while (client.available() == 0) | |
{ | |
if (millis() - timeout > 5000) | |
{ | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
return; | |
} | |
} | |
// Read + print to serial monitor | |
while (client.available()) | |
{ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
// all done | |
Serial.println(); | |
Serial.println("closing connection"); | |
client.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment