This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
AM2315_ESP32_SSL.ino
260 lines (209 loc) · 6.58 KB
/
AM2315_ESP32_SSL.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/****************************************************************************************************************************
AM2315_ESP32_SSL.ino
For ESP32 boards
Blynk_WM is a library for the ESP8266/ESP32 Arduino platform (https://github.com/esp8266/Arduino) to enable easy
configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk
Forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
Built by Khoi Hoang https://github.com/khoih-prog/Blynk_WM
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include <Ticker.h>
#include <Wire.h>
#include <Adafruit_AM2315.h> // To install Adafruit AM2315 library
// Connect RED of the AM2315 sensor to 5.0V
// Connect BLACK to Ground
// Connect WHITE to i2c clock (PIN_D22 / SCL)
// Connect YELLOW to i2c data (PIN_D21 / SDA)
Adafruit_AM2315 AM2315;
#define AM2315_DEBUG true
BlynkTimer timer;
Ticker led_ticker;
#define READ_INTERVAL 60000L //read AM2315 interval 30s
void readAndSendData()
{
static float temperature, humidity;
temperature = AM2315.readTemperature();
humidity = AM2315.readHumidity();
if (!isnan(temperature) && !isnan(humidity))
{
Blynk.virtualWrite(V1, String(temperature, 1));
Blynk.virtualWrite(V2, String(humidity, 1));
#if AM2315_DEBUG
Serial.println("Temp *C: " + String(temperature));
Serial.println("Humid %: " + String(humidity));
#endif
}
else
{
Blynk.virtualWrite(V1, "NAN");
Blynk.virtualWrite(V2, "NAN");
}
}
void set_led(byte status)
{
digitalWrite(LED_BUILTIN, status);
}
void heartBeatPrint()
{
static int num = 1;
if (Blynk.connected())
{
set_led(HIGH);
led_ticker.once_ms(111, set_led, (byte) LOW);
Serial.print(F("B"));
}
else
{
Serial.print(F("F"));
}
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
#define STATUS_CHECK_INTERVAL 60000L
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
// report status to Blynk
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
#if USING_CUSTOMS_STYLE
const char NewCustomsStyle[] /*PROGMEM*/ = "<style>div,input{padding:5px;font-size:1em;}input{width:95%;}body{text-align: center;}\
button{background-color:blue;color:white;line-height:2.4rem;font-size:1.2rem;width:100%;}fieldset{border-radius:0.3rem;margin:0px;}</style>";
#endif
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
// Debug console
Serial.begin(115200);
while (!Serial);
delay(200);
#if (USE_LITTLEFS)
Serial.print(F("\nStarting AM2315_ESP32_SSL using LITTLEFS"));
#elif (USE_SPIFFS)
Serial.print(F("\nStarting AM2315_ESP32_SSL using SPIFFS"));
#else
Serial.print(F("\nStarting AM2315_ESP32_SSL using EEPROM"));
#endif
#if USE_SSL
Serial.print(F(" with SSL on ")); Serial.println(ARDUINO_BOARD);
#else
Serial.print(F(" without SSL on ")); Serial.println(ARDUINO_BOARD);
#endif
#if USE_BLYNK_WM
Serial.println(BLYNK_WM_VERSION);
Serial.println(ESP_DOUBLE_RESET_DETECTOR_VERSION);
#endif
if (!AM2315.begin())
{
Serial.println(F("Sensor not found, check wiring & pullups!"));
}
#if USE_BLYNK_WM
// From v1.0.5
// Set config portal SSID and Password
Blynk.setConfigPortal("TestPortal", "TestPortalPass");
// Set config portal IP address
Blynk.setConfigPortalIP(IPAddress(192, 168, 200, 1));
// Set config portal channel, defalut = 1. Use 0 => random channel from 1-13
Blynk.setConfigPortalChannel(0);
// From v1.0.5, select either one of these to set static IP + DNS
Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
//Blynk.setSTAStaticIPConfig(IPAddress(192, 168, 2, 220), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
// IPAddress(4, 4, 4, 4), IPAddress(8, 8, 8, 8));
//////////////////////////////////////////////
#if USING_CUSTOMS_STYLE
Blynk.setCustomsStyle(NewCustomsStyle);
#endif
#if USING_CUSTOMS_HEAD_ELEMENT
Blynk.setCustomsHeadElement("<style>html{filter: invert(10%);}</style>");
#endif
#if USING_CORS_FEATURE
Blynk.setCORSHeader("Your Access-Control-Allow-Origin");
#endif
//////////////////////////////////////////////
// Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
//Blynk.begin();
// Use this to personalize DHCP hostname (RFC952 conformed)
// 24 chars max,- only a..z A..Z 0..9 '-' and no '-' as last char
//Blynk.begin("AM2315_ESP32_SSL");
Blynk.begin(HOST_NAME);
#else
WiFi.begin(ssid, pass);
#if USE_LOCAL_SERVER
Blynk.config(auth, blynk_server, BLYNK_HARDWARE_PORT);
#else
Blynk.config(auth);
#endif
Blynk.connect();
#endif
timer.setInterval(READ_INTERVAL, readAndSendData);
if (Blynk.connected())
{
#if (USE_LITTLEFS)
Serial.println(F("\nBlynk ESP32 using LittleFS connected"));
#elif (USE_SPIFFS)
Serial.println(F("\nBlynk ESP32 using SPIFFS connected."));
#else
Serial.println(F("\nBlynk ESP32 using EEPROM connected."));
Serial.printf("EEPROM size = %d bytes, EEPROM start address = %d / 0x%X\n", EEPROM_SIZE, EEPROM_START, EEPROM_START);
#endif
#if USE_BLYNK_WM
Serial.print(F("Board Name : ")); Serial.println(Blynk.getBoardName());
#endif
}
}
#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
void displayCredentials()
{
Serial.println(F("\nYour stored Credentials :"));
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
Serial.print(myMenuItems[i].displayName);
Serial.print(F(" = "));
Serial.println(myMenuItems[i].pdata);
}
}
void displayCredentialsInLoop()
{
static bool displayedCredentials = false;
if (!displayedCredentials)
{
for (uint16_t i = 0; i < NUM_MENU_ITEMS; i++)
{
if (!strlen(myMenuItems[i].pdata))
{
break;
}
if ( i == (NUM_MENU_ITEMS - 1) )
{
displayedCredentials = true;
displayCredentials();
}
}
}
}
#endif
void loop()
{
Blynk.run();
timer.run();
check_status();
#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
displayCredentialsInLoop();
#endif
}