This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAM2315_W5100.ino
212 lines (173 loc) · 5.12 KB
/
AM2315_W5100.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
/****************************************************************************************************************************
AM2315_W5100.ino
For STM32 running W5x00 Ethernet shields
BlynkSTM32Ethernet_WM is a library for the STM32 running built-in Ethernet, ENC28J60 or W5x00 Ethernet shields
to enable easy configuration/reconfiguration and autoconnect/autoreconnect to Blynk
Forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
Built by Khoi Hoang https://github.com/khoih-prog/BlynkGSM_ESPManager
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include "Credentials.h"
#include "dynamicParams.h"
#define W5100_CS 10
#define SDCARD_CS 4
#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 (SCL) - on '168/'328 Arduino Uno/Duemilanove/etc that's Analog 5
// Connect YELLOW to i2c data (SDA) - on '168/'328 Arduino Uno/Duemilanove/etc that's Analog 4
Adafruit_AM2315 AM2315;
#define AM2315_DEBUG false
BlynkTimer timer;
#define READ_INTERVAL 30000L //read AM2315 interval 30s
void ReadData()
{
static float temperature, humidity;
if (!AM2315.readTemperatureAndHumidity(&temperature, &humidity))
{
#if AM2315_DEBUG
Serial.println(F("Failed to read data from AM2315"));
#endif
return;
}
#if AM2315_DEBUG
Serial.print(F("Temp *C: "));
Serial.println(String(temperature));
Serial.print(F("Humid %: "));
Serial.println(String(humidity));
#endif
//V1 and V2 are Blynk Display widgets' VPIN
Blynk.virtualWrite(V1, temperature);
Blynk.virtualWrite(V2, humidity);
}
#define BLYNK_PIN_FORCED_CONFIG V10
#define BLYNK_PIN_FORCED_PERS_CONFIG V20
// Use button V10 (BLYNK_PIN_FORCED_CONFIG) to forced Config Portal
BLYNK_WRITE(BLYNK_PIN_FORCED_CONFIG)
{
if (param.asInt())
{
Serial.println( F("\nCP Button Hit. Rebooting") );
// This will keep CP once, clear after reset, even you didn't enter CP at all.
Blynk.resetAndEnterConfigPortal();
}
}
// Use button V20 (BLYNK_PIN_FORCED_PERS_CONFIG) to forced Persistent Config Portal
BLYNK_WRITE(BLYNK_PIN_FORCED_PERS_CONFIG)
{
if (param.asInt())
{
Serial.println( F("\nPersistent CP Button Hit. Rebooting") );
// This will keep CP forever, until you successfully enter CP, and Save data to clear the flag.
Blynk.resetAndEnterConfigPortalPersistent();
}
}
void heartBeatPrint()
{
static int num = 1;
if (Blynk.connected())
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))
{
heartBeatPrint();
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
}
}
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart AM2315_W5100 on ")); Serial.print(BOARD_NAME);
Serial.print(F(" using ")); Serial.println(SHIELD_TYPE);
Serial.println(BLYNK_ETHERNET_STM32_WM_VERSION);
#if !(USE_BUILTIN_ETHERNET || USE_UIP_ETHERNET)
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
if (!AM2315.begin())
{
Serial.println(F("Sensor not found, check wiring & pullups!"));
}
#if USE_BLYNK_WM
Blynk.begin();
#else
#if USE_LOCAL_SERVER
Blynk.begin(auth, server, BLYNK_HARDWARE_PORT);
#else
Blynk.begin(auth);
// You can also specify server:
//Blynk.begin(auth, server, BLYNK_HARDWARE_PORT);
#endif
#endif
if (Blynk.connected())
{
#if USE_BLYNK_WM
Serial.print(F("Conn2Blynk: server = "));
Serial.print(Blynk.getServerName());
Serial.print(F(", port = "));
Serial.println(Blynk.getHWPort());
Serial.print(F("Token = "));
Serial.println(Blynk.getToken());
#endif
Serial.print(F("IP = "));
Serial.println(Ethernet.localIP());
}
timer.setInterval(READ_INTERVAL, ReadData);
}
#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);
}
}
#endif
void loop()
{
Blynk.run();
timer.run();
check_status();
#if (USE_BLYNK_WM && USE_DYNAMIC_PARAMETERS)
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
}