This repository was archived by the owner on Feb 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMega_ESP8266Shield.ino
236 lines (186 loc) · 6.85 KB
/
Mega_ESP8266Shield.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
/****************************************************************************************************************************
Mega_ESP8266Shield.ino
For AVR Mega using ESP8266 WiFi Shield
Blynk_Esp8266AT_WM is a library for the Mega, Teensy, SAM DUE and SAMD boards (https://github.com/khoih-prog/Blynk_Esp8266AT_WM)
to enable easy configuration/reconfiguration and autoconnect/autoreconnect of WiFi/Blynk
Based on and Modified from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
Built by Khoi Hoang https://github.com/khoih-prog/Blynk_Esp8266AT_WM
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
Important notes:
1) Tested OK with SAMD, Mega, nRF52, SAM DUE
a) ESP8266-AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
b) ESP8266-AT version:1.7.4.0(May 11 2020 19:13:04)
SDK version:3.0.4(9532ceb)
compile time:May 27 2020 10:12:22
Bin version(Wroom 02):1.7.4
2) These versions don't work on SAMD, but work on Mega (hanging when connect to Blynk, WiFi OK)
To find bug and fix in Blynk ESP8266_Lib or Blynk library
*****************************************************************************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
// Debug level, 0-3
#define BLYNK_WM_DEBUG 1
#define USE_NEW_WEBSERVER_VERSION true //false
#define _ESP_AT_LOGLEVEL_ 0
#define _ESP_AT_LIB_LOGLEVEL_ 0
/* Comment this out to disable prints and save space */
#define ESP_AT_DEBUG_OUTPUT Serial
//#define ESP_AT_LIB_DEBUG_OUTPUT Serial
#define ESP_AT_DEBUG true
#define ESP_AT_LIB_DEBUG true
/* Comment this out to disable prints and save space */
#define DRD_GENERIC_DEBUG true
// Uncomment to use ESP32-AT commands
//#define USE_ESP32_AT true
// USE_ESP_AT_LIB == true to use new ESP_AT_Lib, instead of ESP8266_Lib
// For ESP32-AT, must use ESP_AT_Lib
#if (defined(USE_ESP32_AT) && USE_ESP32_AT )
#define USE_ESP_AT_LIB true
#else
#define USE_ESP_AT_LIB true
//#define USE_ESP_AT_LIB false
#endif
//#if !( defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_ADK) )
#if !( defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || \
defined(__AVR_ATmega640__) || defined(__AVR_ATmega641__) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA) )
#error This code is intended to run only on the Arduino Mega 1280/2560/ADK boards ! Please check your Tools->Board setting.
#endif
// For Mega, use Serial1 or Serial3
#define EspSerial Serial3
#if ( defined(ARDUINO_AVR_MEGA2560)|| defined(__AVR_ATmega2561__) )
#define BOARD_TYPE "AVR Mega2560"
#elif ( defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) )
#define BOARD_TYPE "AVR Mega1280"
#elif defined(ARDUINO_AVR_ADK)
#define BOARD_TYPE "AVR MegaADK"
#else
#define BOARD_TYPE "AVR Mega"
#endif
#ifndef BOARD_NAME
#define BOARD_NAME BOARD_TYPE
#endif
// Start location in EEPROM to store config data. Default 0
// Config data Size currently is 128 bytes)
#define EEPROM_START 0
#define USE_BLYNK_WM true
//#define USE_BLYNK_WM false
#if USE_BLYNK_WM
#include <BlynkSimpleShieldEsp8266_WM.h>
#else
#include <BlynkSimpleShieldEsp8266.h>
#define USE_LOCAL_SERVER true
#if USE_LOCAL_SERVER
char auth[] = "****";
char BlynkServer[] = "account.duckdns.org";
//char BlynkServer[] = "192.168.2.112";
#else
char auth[] = "****";
char BlynkServer[] = "blynk-cloud.com";
#endif
#define BLYNK_SERVER_HARDWARE_PORT 8080
// Your WiFi credentials.
char ssid[] = "****";
char pass[] = "****";
#endif
// SSID and PW for Config Portal
char portal_ssid[] = "CfgPrtl-SSID";
char portal_password[] = "CfgPrtl-PW";
// Your Mega <-> ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
#if USE_BLYNK_WM
#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();
}
}
#endif
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 15000L
// 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;
}
}
void setup()
{
// Debug console
Serial.begin(115200);
while (!Serial);
delay(500);
Serial.print(F("\nStart Mega_ESP8266Shield on ")); Serial.println(BOARD_NAME);
Serial.println(BLYNK_ESP8266AT_WM_VERSION);
Serial.println(ESP_AT_LIB_VERSION);
// initialize serial for ESP module
EspSerial.begin(ESP8266_BAUD);
#if USE_BLYNK_WM
Serial.println(DOUBLERESETDETECTOR_GENERIC_VERSION);
Serial.println(F("Start Blynk_ESP8266AT_WM"));
// Optional to change default AP IP(192.168.4.1) and channel(10)
//Blynk.setConfigPortalIP(IPAddress(192, 168, 120, 1));
// Personalized portal_ssid and password
Blynk.setConfigPortal(portal_ssid, portal_password);
//Blynk.setConfigPortal("Mega_WM", "MyMega_PW");
Blynk.setConfigPortalChannel(0);
Blynk.begin(wifi);
#else
Serial.print(F("Start Blynk no WM with BlynkServer = "));
Serial.print(BlynkServer);
Serial.print(F(" and Token = "));
Serial.println(auth);
Blynk.begin(auth, wifi, ssid, pass, BlynkServer, BLYNK_SERVER_HARDWARE_PORT);
#endif
}
void loop()
{
Blynk.run();
check_status();
}