90 lines
2.5 KiB
C++
90 lines
2.5 KiB
C++
#include <ESP8266WiFi.h>
|
|
|
|
/************************* WiFi Access Point *********************************/
|
|
|
|
#define WLAN_SSID "SSIDforESPTest"
|
|
#define WLAN_PASS "PASSforESPTest"
|
|
#define LED 2
|
|
|
|
WiFiClient client;
|
|
|
|
/*************************** Sketch Code ************************************/
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println();
|
|
Serial.println("Initializing outputs...");
|
|
Serial.println("Starting Wifi connection...");
|
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
|
int failcounter = 300;
|
|
Serial.print("Waiting for a connection");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(100);
|
|
Serial.print(".");
|
|
if (failcounter <= 0) {
|
|
Serial.println();
|
|
Serial.println("Giving up.");
|
|
shutdown();
|
|
}
|
|
failcounter--;
|
|
}
|
|
Serial.println();
|
|
Serial.println("Wifi connected.");
|
|
// connect to host
|
|
// const char* host = "195.160.169.52";
|
|
const char* host = "192.168.1.1";
|
|
int port = 8000;
|
|
if (client.connect(host, port)) {
|
|
Serial.print("Connection to ");
|
|
Serial.print(host);
|
|
Serial.println(" established.");
|
|
// send GET request
|
|
Serial.println("Sending a message to the server:");
|
|
|
|
client.print(String("GET / HTTP/1.1\r\n") + "Host: " + host + "\r\nConnection: close\r\n\r\n");
|
|
Serial.println(String("GET / HTTP/1.1\r\n") + "Host: " + host + "\r\nConnection: close\r\n\r\n");
|
|
delay(500);
|
|
// get response
|
|
int success = 0;
|
|
failcounter = 10000;
|
|
while (client.connected()) {
|
|
if (client.available()) {
|
|
String line = client.readStringUntil('\n');
|
|
Serial.println("Server response: " + line);
|
|
/*
|
|
if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' && line[3] == 'P' && line[4] == '/' && line[5] == '1' && line[6] == '.' && line[7] == '1' && line[8] == ' ' && line[9] == '2' && line[10] == '0' && line[11] == '0' && line[12] == ' ' && line[13] == 'O' && line[14] == 'K' ) {
|
|
success = 1;
|
|
}
|
|
*/
|
|
}
|
|
if (failcounter <= 0) {
|
|
shutdown();
|
|
}
|
|
failcounter--;
|
|
}
|
|
|
|
// close connection
|
|
Serial.println("Successfully closing the connection.");
|
|
client.stop();
|
|
shutdown();
|
|
} else {
|
|
Serial.print("Unable to connect to ");
|
|
Serial.print(host);
|
|
Serial.println(". Sorry.");
|
|
shutdown();
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
// this should never be reached.
|
|
}
|
|
|
|
|
|
void shutdown() {
|
|
Serial.println("Shutting down.");
|
|
Serial.println("Going to sleep.");
|
|
ESP.deepSleep(0);
|
|
Serial.println("Sleep failed.");
|
|
while(1) {
|
|
}
|
|
} |