Files
Arduino/ESP8266_LIGHT_SLEEP/ESP8266_LIGHT_SLEEP.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

82 lines
1.6 KiB
C++
Executable File

// Library
#include <ESP8266WiFi.h>
// WiFi settings
const char* ssid = "Livebox-37cc";
const char* password = "8A6060920A8A86896F770F2C47";
// Time to sleep (in seconds):
const int sleepTimeS = 10;
// Host
const char* host = "192.168.0.10";
void setup()
{
// Serial
Serial.begin(9600);
Serial.println("ESP8266 in normal mode");
}
void initWifi()
{
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
// Logging data to cloud
Serial.print("Connecting to ");
Serial.println(host);
}
void loop()
{
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8080;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET /json.htm?type=command&param=getSunRiseSet") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
WiFi.disconnect();
// Sleep
Serial.println("ESP8266 in sleep mode");
//wifi_set_sleep_type(LIGHT_SLEEP_T);
WiFi.forceSleepBegin();
}// Library