#include <WiFi.h>
RTC_DATA_ATTR int bootCount = 0;
const char *ssid = "SSID";
const char *pass = "PASSWORD";
const gpio_num_t wakeup = GPIO_NUM_33;
const int led = GPIO_NUM_13;
static int onoff = 1; // -1 always on, 0/1 blink
const char *host_name = "192.168.0.40"; //server address
const int host_port = 80;
WiFiClient client;
TaskHandle_t thp[1];
void Core0a(void *args) {
while (1) {
delay(100);
if (onoff < 0) digitalWrite(led, HIGH);
else {
digitalWrite(led, onoff);
onoff = 1 - onoff;
}
}
}
void setup() {
Serial.begin(115200);
delay(1000);
onoff = 1;
pinMode(led, OUTPUT);
xTaskCreatePinnedToCore(Core0a, "Core0a", 4096, NULL, 3, &thp[0], 0);
++bootCount;
Serial.println("Boot number: " + String(bootCount));
print_wakeup_reason();
esp_sleep_enable_ext0_wakeup(wakeup, 1);
WiFi.begin(ssid, pass);
Serial.print("WiFi connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected.");
onoff = -1;
while (true) {
if (client.connect(host_name, host_port)) {
char sendBuffer[128];
sprintf(sendBuffer, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host_name);
client.print(sendBuffer);
break;
} else {
Serial.println("client.connect error.");
delay(100);
}
}
while(true) {
while(client.available()) {
char c = client.read();
Serial.print(c);
}
if(!client.connected()){
Serial.println("disconnected");
client.stop();
break;
}
}
Serial.println("Go to sleep now");
esp_deep_sleep_start();
}
void loop() {}
void print_wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
Serial.printf("x%02x: ", wakeup_reason);
switch (wakeup_reason) {
case 2: Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 3: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 4: Serial.println("Wakeup caused by timer"); break;
case 5: Serial.println("Wakeup caused by touchpad"); break;
case 6: Serial.println("Wakeup caused by ULP program"); break;
default: Serial.println("Wakeup was not caused by deep sleep"); break;
}
}