-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.ino
179 lines (145 loc) · 4.98 KB
/
setup.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
/*----------- Included Libraries ------------*/
#include <bits/stdc++.h>
#include <LiquidCrystal.h>
#include "CTBot.h"
using namespace std;
CTBot fiona;
/*------------ All about Connections ------------*/
// Set Your Wi-Fi SSID Name and Password
String SSID = "";
String PASSWORD = "";
// Secret Code (Telegram API Key)
String KEY = "";
// Set FionaID
char *FionaID0 = "";
char *FionaID1 = "";
char *FionaID2 = "";
/*------------ Global Variables and Functions ------------*/
String newNotice = "";
// Set 16x2 LCD Pin
const int rs = 14, en = 2, d4 = 0, d5 = 4, d6 = 5, d7 = 16;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int LED1 = 12;
int LED2 = 15;
int PIEZO = 13;
// Emergency Checker Function
int IsEmergency(String text) {
String splitedText = text.substring(0, 10);
for (int i = 0; i < splitedText.length(); i++) {
splitedText[i] = tolower(splitedText[i]);
}
if (splitedText == "/emergency") {
return 1;
}
return 0;
}
// Show notice on LCD Function
void showOnLCD(String txt) {
lcd.setCursor(0, 0);
lcd.print(txt);
lcd.setCursor(0, 1);
lcd.print("{fiona:} by Team Disconnection");
for (int scrollCounter = 0; scrollCounter < txt.length(); scrollCounter++) {
lcd.scrollDisplayLeft();
delay(400);
}
}
/*------------ Setup Section ------------*/
void setup() {
// Start LCD
lcd.begin(16, 2);
// Declare LED Pins Mode
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(PIEZO, OUTPUT);
// Connect with Internet
fiona.wifiConnect(SSID, PASSWORD);
// Connect with Telegram
fiona.setTelegramToken(KEY);
// Check the Internet Connection
if (fiona.testConnection()) {
// If Internet Connection is Ok
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
lcd.clear();
newNotice = "No new messages";
}
else{
// If Internet Connection is not Ok
digitalWrite(LED1, HIGH);
delay(500);
digitalWrite(LED2, HIGH);
delay(5000);
digitalWrite(LED2, LOW);
digitalWrite(LED1, LOW);
lcd.clear();
newNotice = "No Internet Connection";
}
}
/*------------ Loop Section ------------*/
void loop() {
TBMessage notice;
// Check Incoming Message
if (CTBotMessageText == fiona.getNewMessage(notice)) {
char *ID = (char *)notice.sender.id;
if (notice.text.equalsIgnoreCase("/start")) {
// Send welcome message to sender
String welcome = (String) "Hello! Your FionaID is " + notice.sender.id + ". You can use this ID to make your system unique.\n\n After completing the setup, Type '/help' to learn more.\n\n Thanks.";
fiona.sendMessage(notice.sender.id, welcome);
}
// Check ID
if (strcmp(ID, FionaID0) || strcmp(ID, FionaID1) || strcmp(ID, FionaID2)) {
String text = (String)notice.text;
/*------------ Response & Message ------------*/
if (notice.text.equalsIgnoreCase("/help")) {
// Send help page to sender
String help = (String) "/emergency text - Send notice with emergency priority.\n\text - Send notice without any priority.\n\nnotice_Text must be longer than 10 characters.\n\nTo learn more, visit https://github.com/aratheunseen/fiona/blob/main/DOCS.md";
fiona.sendMessage(notice.sender.id, help);
} else {
if (text.length() < 10) {
String report = (String) "Unable to publish. It is too short or an invalid notice.";
fiona.sendMessage(notice.sender.id, report);
} else if (IsEmergency(text) == 1 && text.length() < 12) {
// Send notice as False Emergency
String report = (String) "Invalid notice. Type '/emergency text' to send emergency notice.";
fiona.sendMessage(notice.sender.id, report);
}
// Emergency Notice
else if (IsEmergency(text) == 1 && text.length() > 11) {
// LED
digitalWrite(LED2, HIGH);
digitalWrite(LED1, HIGH);
// LCD
String emergencyText = (String)notice.text;
int last = emergencyText.length();
lcd.clear();
newNotice = emergencyText.substring(11, last);
// Toggle Piezo for 5s
digitalWrite(PIEZO, HIGH);
delay(5000);
digitalWrite(PIEZO, LOW);
// Send notice as emergency report
String report = (String) "Notice has been published as emergency.";
fiona.sendMessage(notice.sender.id, report);
}
// General Notice
else {
// LED
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
// LCD
lcd.clear();
newNotice = (String)notice.text;
// Send notice delivered report
String report = (String) "Notice has been published.";
fiona.sendMessage(notice.sender.id, report);
}
}
}
}
showOnLCD(newNotice);
}