· 4 years ago · Sep 10, 2021, 06:46 PM
1#include "lcdConfig.h"
2#include "config.h"
3#include "utilities.h"
4#include "libs.h"
5
6
7#include <FirebaseESP32.h>
8//Provide the token generation process info.
9#include "addons/TokenHelper.h"
10#include "addons/RTDBHelper.h"
11
12#include <U8g2lib.h>
13#ifdef U8X8_HAVE_HW_SPI
14#include <SPI.h>
15#endif
16#ifdef U8X8_HAVE_HW_I2C
17#include <Wire.h>
18#endif
19
20
21#define LCD_CS 33
22#define LCD_SCLK 14 //25
23#define LCD_MISO 12 //27
24#define LCD_MOSI 13 //26
25
26// SPIClass SPI1(HSPI);
27
28U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ LCD_SCLK, /* data=*/ LCD_MOSI, /* CS=*/ LCD_CS, /* reset=*/ 22);
29
30
31
32
33#define SMS_TARGET "+8801611696567"
34#define CALL_TARGET "+380xxxxxxxxx"
35
36
37
38
39/* 1. Define the WiFi credentials */
40#define WIFI_SSID "Cloud9"
41#define WIFI_PASSWORD "---"
42
43/* 2. Define the API Key */
44#define API_KEY "---"
45
46/* 3. Define the project ID */
47#define FIREBASE_PROJECT_ID "---"
48#define DATABASE_URL "---"
49/* 4. Define the user Email and password that alreadey registerd or added in your project */
50#define USER_EMAIL "user@ireverb.com"
51#define USER_PASSWORD "admin"
52
53//Define Firebase Data object
54FirebaseData stream;
55FirebaseData fbdo;
56FirebaseAuth auth;
57FirebaseConfig config;
58
59unsigned long dataMillis = 0;
60int count = 0;
61
62void connectWiFi()
63{
64 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
65 SerialMon.print("Connecting to Wi-Fi");
66 while (WiFi.status() != WL_CONNECTED)
67 {
68 SerialMon.print(".");
69 delay(300);
70 }
71 SerialMon.println();
72 SerialMon.print("Connected with IP: ");
73 SerialMon.println(WiFi.localIP());
74 SerialMon.println();
75
76 SerialMon.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
77}
78
79
80void send_sms(String name, String text, String number)
81{
82 bool res = modem.sendSMS(number,"Hey "+ name + ",\n\n" + text + String("\n\n - Open Knot app to view it!"));
83
84 DBG("SMS:", res ? "OK" : "fail");
85}
86
87
88// Stream Firebase Data
89void streamCallback(StreamData data)
90{
91
92 Serial.printf("sream path, %s\nevent path, %s\ndata type, %s\nevent type, %s\n\n",
93 data.streamPath().c_str(),
94 data.dataPath().c_str(),
95 data.dataType().c_str(),
96 data.eventType().c_str());
97
98
99 printResult(data); //see addons/RTDBHelper.h
100 Serial.println();
101
102
103 if (data.dataPath().c_str()!="/")
104 {
105 String username_filter = data.dataPath().c_str();
106
107 String online_user = username_filter + String(" is online.");
108
109 send_sms("corrupted-genius",online_user,"+8801521105285");
110 }
111
112
113
114
115}
116
117void streamTimeoutCallback(bool timeout)
118{
119 if (timeout)
120 Serial.println("stream timeout, resuming...\n");
121
122 if (!stream.httpConnected())
123 Serial.printf("error code: %d, reason: %s\n\n", stream.httpCode(), stream.errorReason().c_str());
124}
125
126
127
128
129void initFireBase()
130{
131 /* Assign the project host and api key (required) */
132 config.api_key = API_KEY;
133
134 /* Assign the user sign in credentials */
135 auth.user.email = USER_EMAIL;
136 auth.user.password = USER_PASSWORD;
137
138 /* Assign the callback function for the long running token generation task */
139 // config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
140
141 config.database_url = DATABASE_URL;
142 config.signer.tokens.legacy_token = "---";
143
144 Firebase.begin(&config, &auth);
145
146 Firebase.reconnectWiFi(true);
147
148 //Recommend for ESP8266 stream, adjust the buffer size to match your stream data size
149 #if defined(ESP8266)
150 stream.setBSSLBufferSize(2048 /* Rx in bytes, 512 - 16384 */, 512 /* Tx in bytes, 512 - 16384 */);
151 #endif
152
153 if (!Firebase.beginStream(stream, "/presence"))
154 {
155 Serial.printf("sream begin error, %s\n\n", stream.errorReason().c_str());
156 }
157 Firebase.setStreamCallback(stream, streamCallback, streamTimeoutCallback);
158
159
160
161}
162
163
164
165void clearLCD()
166{
167 u8g2.clearBuffer();
168}
169
170void lcdText(int x, int y,String text)
171{
172 // char *str;
173
174 // char* str = text.c_str();
175
176 u8g2.setFont(u8g2_font_profont12_tr); // choose a suitable font
177 u8g2.drawStr(x,y,text.c_str()); // write something to the internal memory
178 u8g2.sendBuffer();
179}
180
181void setup()
182{
183 // // Update Wire.h for 128x64 LCD
184 // Wire.begin(LCD_SDA, LCD_SCL);
185 // SPI1.begin(LCD_SCLK, LCD_MISO, LCD_MOSI, LCD_CS);
186
187
188 u8g2.begin();
189 u8g2.enableUTF8Print();
190 u8g2.clearBuffer();
191
192 clearLCD();
193 // lcdText(10,30,"Booting Up...");
194
195
196
197 // Set console baud rate
198 SerialMon.begin(115200);
199 connectWiFi();
200 delay(1000);
201 initFireBase();
202
203 delay(10);
204
205 // Start power management
206 if (setupPMU() == false)
207 {
208 SerialMon.println("Setting power error");
209 // clearLCD();
210 lcdText(5,30,"Powering up.");
211
212 }
213 clearLCD();
214 // Some start operations
215 setupModem();
216
217 // Set GSM module baud rate and UART pins
218 SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
219
220 delay(6000);
221
222
223 SerialMon.println("Initializing modem...");
224 lcdText(10,30,"Connecting...");
225 modem.init();
226 delay(10000);
227
228
229
230
231}
232
233bool is_sim_data_fetched = false;
234
235
236
237void loop()
238{
239 // clearLCD();
240
241 String name = "[CELESTE]";
242
243 String uid = auth.token.uid.c_str();
244
245 if (!is_sim_data_fetched)
246 {
247 clearLCD();
248 String cop = modem.getOperator();
249 String ussd_phone_num = modem.sendUSSD("*2#");
250 lcdText(30,10,cop);
251 lcdText(5,22,ussd_phone_num);
252 // lcdText(5,46,"UID: " + uid);
253
254
255 // // u8g2.updateDisplayArea(5, 34, 30, 12);
256 // int csq = modem.getSignalQuality();
257 // lcdText(5,34,"Strength: " + String(csq));
258
259
260 is_sim_data_fetched = true;
261 }
262 else
263 {
264
265 // u8g2.updateDisplayArea(5, 34, 12, 8);
266 int csq = modem.getSignalQuality();
267 lcdText(5,34,"Strength: " + String(csq));
268 delay(5000);
269 }
270
271
272
273
274
275 // Do nothing forevermore
276 while (true)
277 {
278 modem.maintain();
279 }
280}
281