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

130 lines
3.2 KiB
C++
Executable File

/**
An example showing how to put ESP8266 into Deep-sleep mode
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
//#include <Losant.h>
// WiFi credentials.
const char* WIFI_SSID = "Livebox-37cc";
const char* WIFI_PASS = "8A6060920A8A86896F770F2C47";
WiFiClientSecure wifiClient;
void connect() {
// Connect to Wifi.
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// WiFi fix: https://github.com/esp8266/Arduino/issues/2186
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
unsigned long wifiConnectStart = millis();
while (WiFi.status() != WL_CONNECTED) {
// Check to see if
if (WiFi.status() == WL_CONNECT_FAILED) {
Serial.println("Failed to connect to WiFi. Please verify credentials: ");
delay(10000);
}
delay(500);
Serial.println("...");
// Only try for 5 seconds.
if (millis() - wifiConnectStart > 15000) {
Serial.println("Failed to connect to WiFi");
return;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("Connecting to Losant...");
Serial.print("Authenticating Device...");
HTTPClient http;
http.begin("http://api.losant.com/auth/device");
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
/* Create JSON payload to sent to Losant
{
"deviceId": "575ecf887ae143cd83dc4aa2",
"key": "this_would_be_the_key",
"secret": "this_would_be_the_secret"
}
*/
String buffer;
int httpCode = http.POST(buffer);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println("This device is authorized!");
} else {
Serial.println("Failed to authorize device to Losant.");
if (httpCode == 400) {
Serial.println("Validation error: The device ID, access key, or access secret is not in the proper format.");
} else if (httpCode == 401) {
Serial.println("Invalid credentials to Losant: Please double-check the device ID, access key, and access secret.");
} else {
Serial.println("Unknown response from API");
}
return;
}
} else {
Serial.println("Failed to connect to Losant API.");
return;
}
http.end();
Serial.println("Connected!");
Serial.println("This device is now ready for use!");
}
void setup() {
Serial.begin(9600);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while (!Serial) { }
Serial.println("Device Started");
Serial.println("-------------------------------------");
Serial.println("Running Deep Sleep Firmware!");
Serial.println("-------------------------------------");
connect();
int temp = analogRead(A0);
double degreesC = (((temp / 1024.0) * 3.2) - 0.5) * 100.0;
double degreesF = degreesC * 1.8 + 32;
Serial.println();
Serial.print("Temperature C: ");
Serial.println(degreesC);
Serial.print("Temperature F: ");
Serial.println(degreesF);
Serial.println();
Serial.println("Going into deep sleep for 20 seconds");
ESP.deepSleep(20e6); // 20e6 is 20 microseconds
}
void loop() {
}