-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8285_airwick.ino
129 lines (103 loc) · 3.13 KB
/
esp8285_airwick.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define DEBUG
#ifdef DEBUG
#define debug(x) Serial.print(x)
#define debugln(x) Serial.println(x)
#else
#define debug(x) // define empty, so macro does nothing
#define debugln(x)
#endif
const char* ssid = "xxxx";
const char* password = "xxxxx";
IPAddress ip(192, 168, 1, xx);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 1, xx);
const char* dom_interval = "http://192.168.1.xx:xxxx/json.htm?type=command¶m=getuservariable&idx=1";
const char* dom_device = "http://192.168.1.xx:xxxx/json.htm?type=devices&rid=149";
const char* dom_volt = "http://192.168.1.xx:xxxx/json.htm?type=command¶m=udevice&idx=150&nvalue=0&svalue=3.3";
#define Motor 14
#define Motor_time 1000
StaticJsonBuffer<2200> jsonBuffer;
int httpCode;
String payload;
byte interval;
String dev_status;
byte sleepCount = 0;
void setup() {
Serial.begin(115200);
debug("Connecting to ");
debugln(ssid);
WiFi.config(ip, dns, gateway, subnet); //remove if DHCP is needed
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
debug(".");
}
debugln("");
debugln("WiFi connected");
debugln("IP address: ");
debugln(WiFi.localIP());
pinMode(Motor, OUTPUT);
debugln("I'm awake and getting Domoticz data about Airwick");
getDomoticzData();
EEPROM.begin(1); // initialise EEPROM to store only one byte
sleepCount = EEPROM.read(0);
if(sleepCount > 250) sleepCount = 1;
debugln("Current SleepCount: "+ String(sleepCount));
if( (sleepCount >= interval) && (dev_status == "On")){
debugln("Airwick papurskimas!!");
digitalWrite(Motor, HIGH);
delay(Motor_time);
sleepCount = 0;
}
if( (sleepCount >= interval) && (dev_status == "Off")){
sleepCount = 0;
}
EEPROM.write(0, sleepCount+1);
EEPROM.commit();
debugln("Going into deep sleep for 58 seconds");
ESP.deepSleep(58e6); // 20e6 is 20 microseconds
}
// the loop function runs over and over again forever
void loop() {
}
void getDomoticzData(){
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http; //Object of class HTTPClient
http.begin(dom_interval);
httpCode = http.GET();
payload = http.getString();
// debug("Response Code:"); //200 is OK
// debugln(httpCode); //Print HTTP return code
// debug("Returned data from Server:");
// debugln(payload); //Print request response payload
if (httpCode > 0)
{
JsonObject& root = jsonBuffer.parseObject(payload);
interval = root["result"][0]["Value"];
debug("Idx_interval:");
debugln(interval);
}
jsonBuffer.clear();
http.end(); //Close connection
http.begin(dom_device);
httpCode = http.GET();
payload = http.getString();
if (httpCode > 0)
{
JsonObject& root1 = jsonBuffer.parseObject(payload);
dev_status = root1["result"][0]["Data"].as<String>();
debug("Dev_status:");
debugln(dev_status);
}
jsonBuffer.clear();
http.end(); //Close connection
http.begin(dom_volt);
httpCode = http.GET();
}
}