Skip to content

Instantly share code, notes, and snippets.

@morgulbrut
Last active June 18, 2018 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgulbrut/45c5c00f7e545bdc911099e0e9e52b60 to your computer and use it in GitHub Desktop.
Save morgulbrut/45c5c00f7e545bdc911099e0e9e52b60 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <HTTPClient.h>
RTC_DATA_ATTR int bootCount = 0; // RTC_DATA_ATTR saves stuff in the RTC memory
const char* ssid = "*******";
const char* password = "********";
const int uS_TO_S_FACTOR = 1000000; /* Conversion factor for micro seconds to seconds */
const int S_TO_M_FACTOR = 60;
const int TIME_TO_SLEEP = 3;
const byte BTN1 = D19;
const uint64_t BTN1_mask = 1ULL << BTN1;
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case 1 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case 2 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case 3 : Serial.println("Wakeup caused by timer"); break;
case 4 : Serial.println("Wakeup caused by touchpad"); break;
case 5 : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.println("Wakeup was not caused by deep sleep"); break;
}
}
void setup(){
Serial.begin(115200);
delay(1000); //Take some time to open up the Serial Monitor
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//Increment boot number and print it every reboot
++bootCount;
Serial.println("Boot number: " + String(bootCount));
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin("http://jsonplaceholder.typicode.com/comments?id=10"); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);
}
else {
Serial.println("Error on HTTP request");
}
http.end(); //Free the resources
}
delay(10000);
//Print the wakeup reason for ESP32
print_wakeup_reason();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR * S_TO_M_FACTOR);
esp_sleep_enable_ext1_wakeup(BTN1_mask, ESP_EXT1_WAKEUP_ALL_LOW);
//Go to sleep now
Serial.println("Going to sleep now");
esp_deep_sleep_start();
}
void loop(){} // needed because of arduino
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment