-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESP8266.ino
158 lines (118 loc) 路 4.2 KB
/
ESP8266.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
/**
* SYNTAX:
*
* DefaultEthernetNetwork::DefaultEthernetNetwork(<Firebase_SPI_ETH_Module>);
*
* <Firebase_SPI_ETH_Module> - The Firebase_SPI_ETH_Module class object that keeping the pointer to lwip driver for each
* SPI Ethernet module (e.g. ENC28J60lwIP, W5100lwIP and W5500lwIP).
*
*
* The ENC28J60 Ethernet module and ESP8266 board, SPI port wiring connection.
*
* ESP8266 (Wemos D1 Mini or NodeMCU) ENC28J60
*
* GPIO12 (D6) - MISO SO
* GPIO13 (D7) - MOSI SI
* GPIO14 (D5) - SCK SCK
* GPIO16 (D0) - CS CS
* GND GND
* 3V3 VCC
*
* The complete usage guidelines, please visit https://github.com/mobizt/FirebaseClient
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FirebaseClient.h>
#include <WiFiClientSecure.h>
#include <ENC28J60lwIP.h>
// #include <W5100lwIP.h>
// #include <W5500lwIP.h>
#include <LwipEthernet.h>
/** Don't gorget to define this in src/Config.h or src/UserConfig.h
#define ENABLE_ESP8266_ENC28J60_ETH
*/
/** For PlatformIO IDE, please set the lib_ldf_mode in platformio.ini as the following.
lib_ldf_mode = chain+
*/
// The API key can be obtained from Firebase console > Project Overview > Project settings.
#define API_KEY "Web_API_KEY"
// User Email and password that already registerd or added in your project.
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
void printResult(AsyncResult &aResult);
#define ETH_CS_PIN 16 // D0
ENC28J60lwIP eth(ETH_CS_PIN);
// Wiznet5100lwIP eth(ETH_CS_PIN);
// Wiznet5500lwIP eth(ETH_CS_PIN);
Firebase_SPI_ETH_Module spi_eth;
DefaultEthernetNetwork default_network(spi_eth);
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3000 /* expire period in seconds (<= 3600) */);
FirebaseApp app;
WiFiClientSecure ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client, getNetwork(default_network));
AsyncResult aResult_no_callback;
void setup()
{
Serial.begin(115200);
// https://github.com/esp8266/Arduino/blob/master/libraries/lwIP_Ethernet/examples/EthClient/EthClient.ino
Serial.print("Connecting to Ethernet... ");
if (!ethInitDHCP(eth))
{
Serial.println("no hardware found!");
while (1)
{
delay(1000);
}
}
while (!eth.connected())
{
Serial.printf(".");
delay(500);
}
Firebase.printf("\nEthernet: IP Address: %s\n", eth.localIP().toString().c_str());
/* Assign the pointer to Ethernet module lwip interface */
#if defined(ENABLE_ESP8266_ENC28J60_ETH)
spi_eth.enc28j60 = ð
#endif
#if defined(ENABLE_ESP8266_W5100_ETH)
spi_eth.w5100 = ð
#endif
#if defined(ENABLE_ESP8266_W5500_ETH)
spi_eth.w5500 = ð
#endif
Serial.printf("Firebase Client v%s\n", FIREBASE_CLIENT_VERSION);
Serial.println("Initializing app...");
ssl_client.setInsecure();
ssl_client.setBufferSizes(4096, 1024);
initializeApp(aClient, app, getAuth(user_auth), aResult_no_callback);
}
void loop()
{
// The async task handler should run inside the main loop
// without blocking delay or bypassing with millis code blocks.
app.loop();
// To get the authentication time to live in seconds before expired.
// app.ttl();
printResult(aResult_no_callback);
}
void printResult(AsyncResult &aResult)
{
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.appEvent().message().c_str(), aResult.appEvent().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
}
}