-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHumidityControllerFirmware.ino
133 lines (107 loc) · 2.76 KB
/
HumidityControllerFirmware.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
130
131
132
133
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>
#include <ArduinoJson.h>
// sensor config
#define SENSOR_PIN D7
#define SENSOR_TYPE DHT11
// WiFi config
#define NETWORK_NAME ""
#define NETWORK_PASS ""
#define HUMIDITY_UPDATED_ENDPOINT ""
#define AWS_FINGERPRINT ""
// initialize dht sensor & http client
DHT dht(SENSOR_PIN, SENSOR_TYPE);
HTTPClient http;
// global variables
int humidifierState = 0;
void setup()
{
Serial.begin(115200); // TODO: replace with AWS logs
// set-up sensor
pinMode(SENSOR_PIN, INPUT);
dht.begin();
// set-up humidifier
pinMode(15, OUTPUT); // живлення резонатора D8
pinMode(12, OUTPUT); // вкл. резонатора D6
pinMode(2, OUTPUT); // flashlight
digitalWrite(15, HIGH);
delay(4000);
digitalWrite(12, HIGH);
// set-up WiFi
Serial.print("Establishing network connection");
WiFi.begin(NETWORK_NAME, NETWORK_PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("\nWorking!");
}
void loop()
{
delay(3000);
Serial.println("Getting humidity information...");
float currentHumidityLevel = dht.readHumidity();
String currentHumidifierState = Get_Current_Humidifier_State();
Serial.println("Sending request...");
String requestBody = Create_Humidity_Updated_Request_Body(currentHumidityLevel, currentHumidifierState);
int responseCode = Submit_Humidity_Updated_Request(requestBody);
if (responseCode == 202)
{
Toggle_Humidifier();
}
Serial.println(responseCode);
}
int Submit_Humidity_Updated_Request(String requestBody)
{
int responseCode;
http.begin(HUMIDITY_UPDATED_ENDPOINT, AWS_FINGERPRINT);
http.addHeader("Content-Type", "application/json");
responseCode = http.POST(requestBody);
http.end();
return responseCode;
}
String Create_Humidity_Updated_Request_Body(float currentHumidityLevel, String currentHumidifierState)
{
String response;
StaticJsonDocument<100> json;
json["current_humidity_level"] = currentHumidityLevel/100;
json["current_humidifier_state"] = currentHumidifierState;
serializeJson(json, response);
return response;
}
String Get_Current_Humidifier_State()
{
if (humidifierState == 1)
{
return "enabled";
}
else if (humidifierState == 0)
{
return "disabled";
}
}
void Toggle_Humidifier()
{
if (humidifierState == 0)
{
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
humidifierState = 1;
digitalWrite(2, LOW); // flashlight off
}
else if (humidifierState == 1)
{
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(100);
digitalWrite(12, HIGH);
humidifierState = 0;
digitalWrite(2, HIGH); // flashlight on
}
}