#include
#include
#include
#include
#include
#define BatteryFactor 1
#define Sleep 3600e6
// 0 => Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
// 30e6 => Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
#define SwitchPin 14
#define ActivePin 12
#define EnDebugging
String URL = "https://api.iot.spried.com/connect";
String DeviceId = "%%% DEVICE ID %%%";
String DeviceKey = "%%% DEVICE KEY %%%";
ESP8266WiFiMulti wifiMulti;
void setup()
{
pinMode(ActivePin,INPUT);
pinMode(SwitchPin,INPUT);
WiFi.mode(WIFI_STA);
wifiMulti.addAP("%%% WIFI 1 SSID %%%", "%%% WIFI 1 PASSWORD %%%");
wifiMulti.addAP("%%% WIFI 2 SSID %%%", "%%% WIFI 2 PASSWORD %%%");
delay(2000);
unsigned int count = 0;
while(count<20)
{
if(postData())
break;
delay(2000);
count++;
}
if(!digitalRead(SwitchPin))
{
pinMode(ActivePin,OUTPUT);
digitalWrite(ActivePin,LOW);
}
delay(2000);
ESP.deepSleep(Sleep);
}
void loop()
{
delay(2000);
ESP.deepSleep(Sleep);
delay(2000);
}
bool postData()
{
if (wifiMulti.run() == WL_CONNECTED)
{
digitalWrite(ActivePin,HIGH);
std::unique_ptrclient(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient http;
if(http.begin(*client,URL))
{
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
http.addHeader("HTTP_ACCEPT", "application/json");
http.addHeader("X-API-KEY", DeviceId);
http.addHeader("Authorization", DeviceKey);
long rssi = WiFi.RSSI();
int isopen = digitalRead(SwitchPin);
float batteryLevel = analogRead(0)*BatteryFactor;
int httpCode = http.POST("{\"status\":"+String(isopen)+",\"battery\":"+String(batteryLevel)+",\"rssi\":"+String(rssi)+"}");
http.end();
return true;
}
}
return false;
}