ESPWakeupTest/ESP8266-WakeupTest.ino

83 lines
2.1 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.");
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 (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) {
}
}