Use the same circuit from the "GET a Web Page" lecture.
For real hardware, here is the code:
#include <ESP8266WiFi.h>
// WiFi config
const char ssid[] = YOUR NETWORK'S SSID";
const char password[] = "YOUR NETWORK'S PASSWORD";
// Server, file, and port
const char hostname[] = "adafruit.com";
const String uri = "/testwifi/index.html";
const int port = 80;
// WiFiClient object
WiFiClient client;
void setup() {
// Initialize Serial
Serial.begin(9600);
delay(100);
// Connect to WiFi
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
}
Serial.println();
// Show that we are connected
Serial.println("Connected!");
}
void loop() {
// Connect to server
Serial.print("Connecting to ");
Serial.println(hostname);
if ( client.connect(hostname, port) == 0 ) {
Serial.println("Connection failed");
}
// Send request for file from server
client.print("GET " + uri + " HTTP/1.1\r\n" +
"Host: " + hostname + "\r\n" +
"Connection: close\r\n\r\n");
delay(500);
// Look for "this," and store whatever comes next
String string = "Not found";
for ( unsigned long i = 0; i < 500; i++ ) {
if ( client.find("this, ") ) {
string = client.readStringUntil('\n');
while ( client.available() ) {
client.read();
}
break;
}
delay(10);
}
// Print the string
Serial.println(string);
// Close TCP connection
client.stop();
Serial.println("Connection closed");
delay(10000);
}
For the simulator, here is the code:
#include "SoftwareSerial.h"
// WiFi config
String ssid = "Simulator Wifi";
String password = "";
// Server, file, and port
const String hostname = "adafruit.com";
const String uri = "/testwifi/index.html";
const int port = 80;
// Software serial object
SoftwareSerial soft(8, 9); // RX, TX
void setup() {
// Initialize serial connections
Serial.begin(115200);
soft.begin(9600);
// Talk to ESP8266
soft.println("Init ESP8266...");
Serial.println("AT");
delay(10);
if ( Serial.find("OK") == 0 ) {
soft.println("ESP8266 not found");
while(1);
}
// Connect to WiFi
soft.println("Connecting to WiFi...");
Serial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
delay(10);
if ( Serial.find("OK") == 0 ) {
soft.println("Could not connect to WiFi");
while(1);
} else {
soft.println("Connected!");
}
}
void loop() {
// Open TCP connection
Serial.println("AT+CIPSTART=\"TCP\",\"" + hostname + "\"," + port);
delay(50);
if ( Serial.find("OK") == 0 ) {
soft.println("Error");
}
// Construct HTTP request
String req = "GET " + uri + " HTTP/1.1\r\n" +
"Host: " + hostname + "\r\n" +
"Connection: close\r\n" +
"\r\n";
int len = req.length();
// Send our request length
Serial.print("AT+CIPSEND=");
Serial.println(len);
delay(10);
if ( Serial.find(">") == 0 ) {
soft.println("Error");
}
// Send our http request
Serial.print(req);
delay(10);
if (!Serial.find("SEND OK\r\n")) {
soft.println("Error");
}
// Wait for a response from the server
while( Serial.available() == 0 ) {
delay(5);
}
// Look for "this," and store whatever comes next
String string = "Not found";
for ( unsigned long i = 0; i < 500; i++ ) {
if ( Serial.find("this, ") ) {
string = Serial.readStringUntil(':');
while ( Serial.available() ) {
Serial.read();
}
break;
}
delay(10);
}
// Print the string
soft.println(string);
// Close TCP connection
Serial.println("AT+CIPCLOSE=0");
delay(50);
if ( Serial.find("OK") == 0 ) {
soft.println("Error");
} else {
soft.println();
soft.println("Connection closed");
}
delay(10000);
}
Simulator: https://tinkercad.com/things/1hO5KOzScgi