Skip to content

Instantly share code, notes, and snippets.

@meub
Last active August 30, 2020 22:51
Show Gist options
  • Save meub/29121f0370d2a05fc1a0a8d2513bed08 to your computer and use it in GitHub Desktop.
Save meub/29121f0370d2a05fc1a0a8d2513bed08 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
// WiFi settings
const char* ssid = "SSID_NAME";
const char* password = "SSID_PASSPHRASE";
// Device Settings
// I use the last 6 characters of the WiFi MAC address as device ID
const char* device_id = "123456";
// API Settings
const char* host = "my.domain.com";
const uint16_t port = 80;
WiFiClient client;
void setup_wifi() {
delay(10);
// Announce connection
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Setup WiFi config and connect
WiFi.mode(WIFI_STA);
//WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're connected
while (!client.connected()) {
Serial.print("Attempting connection...");
// Attempt to connect
if (client.connect(host,port)) {
// Success
Serial.println("connected");
} else {
// Fail
Serial.print("failed");
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
// Start WiFi setup
setup_wifi();
}
void loop() {
if (!client.connected()) {
reconnect();
} else {
Serial.println("Making Request to Server");
client.println((String)"GET /?device_id=" + device_id + " HTTP/1.0");
client.println((String)"Host: " + host);
client.println("Connection: keep-alive");
client.println("Cache-Control: max-age=0");
client.println("User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)");
client.println("Accept-Encoding: gzip, deflate");
client.println("Accept-Language: en-US,en;q=0.9");
client.println();
delay(1000);
// Sleep 30 minutes (deepSleep is defined in microseconds)
Serial.print("Going to sleep...");
ESP.deepSleep(30 * 60 * 1000000);
delay(3000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment