-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01-line_notify.ino
219 lines (183 loc) · 6.19 KB
/
01-line_notify.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
/*
Author : Teeraphat Kullanankanjana
Version : 1.0
Date : 02/08/2567
Description : Connects an Arduino MKR WIFI 1010 to a Wi-Fi network and uses the LINE Notify API to send messages, stickers, and images from url.
Copyright (C) 2024 Teeraphat Kullanankanjana. All rights reserved.
*/
#include <WiFiNINA.h>
// Wi-Fi credentials
const char* ssid = "your_ssid";
const char* password = "your_password";
// LINE Notify API details
const char* accessToken = ""; // Replace with your actual token
const char* host = "notify-api.line.me";
const int httpsPort = 443;
WiFiSSLClient client;
void setup() {
Serial.begin(9600);
// Connect to Wi-Fi
connectToWiFi();
// Example usage of notifyMessage function
notifyMessage("Hello from Arduino MKR WIFI1010");
// Example usage of notifySticker function
notifySticker("Hello with sticker!", 1, 1); // Example sticker package ID and sticker ID
// Example usage of notifyImageURL function
notifyImageURL("Hello with image!", "https://static.wikia.nocookie.net/chainsaw-man/images/1/1b/Pochita.PNG"); // Example image URL
}
void loop() {
// Your code goes here
}
/*
* After this comment, there will be a Line Notify API function.
* There is no need to make a change.
*/
// WIFI Connect function
void connectToWiFi() {
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
}
// Text-Only Notification
void notifyMessage(const String &message) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Not connected to Wi-Fi");
return;
}
// Connect to LINE Notify API
if (!client.connect(host, httpsPort)) {
Serial.println("Connection to LINE Notify failed!");
return;
}
// URL encode the message
String encodedMessage = urlEncode(message);
// Prepare the POST request
String url = "/api/notify";
String payload = "message=" + encodedMessage;
String request = String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + accessToken + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + payload.length() + "\r\n" +
"Connection: close\r\n\r\n" +
payload;
// Send the request
client.print(request);
Serial.println("Request sent!");
// Read and print the response
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") break; // Headers ended
Serial.println(line);
}
// Close the connection
client.stop();
}
// Sticker Notification Function
// See sticker package ID and sticker ID here: https://developers.line.biz/en/docs/messaging-api/sticker-list/.
// Every notification message parameter is required.
void notifySticker(const String &message, int stickerPkgID, int stickerID) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Not connected to Wi-Fi");
return;
}
// Connect to LINE Notify API
if (!client.connect(host, httpsPort)) {
Serial.println("Connection to LINE Notify failed!");
return;
}
// URL encode the message
String encodedMessage = urlEncode(message);
// Prepare the POST request
String url = "/api/notify";
String payload = "message=" + encodedMessage + "&stickerPackageId=" + stickerPkgID + "&stickerId=" + stickerID;
String request = String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + accessToken + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + payload.length() + "\r\n" +
"Connection: close\r\n\r\n" +
payload;
// Send the request
client.print(request);
Serial.println("Request sent!");
// Read and print the response
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") break; // Headers ended
Serial.println(line);
}
// Close the connection
client.stop();
}
// Image from the URL Notification Function
// The image URL must be in HTTPS format and end with a.png or.jpeg file.
// Every notification message parameter is required.
void notifyImageURL(const String &message, const String &imageUrl) {
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Not connected to Wi-Fi");
return;
}
// Connect to LINE Notify API
if (!client.connect(host, httpsPort)) {
Serial.println("Connection to LINE Notify failed!");
return;
}
// URL encode the message
String encodedMessage = urlEncode(message);
String encodedImageUrl = urlEncode(imageUrl);
// Prepare the POST request
String url = "/api/notify";
String payload = "message=" + encodedMessage + "&imageThumbnail=" + encodedImageUrl + "&imageFullsize=" + encodedImageUrl;
String request = String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Authorization: Bearer " + accessToken + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: " + payload.length() + "\r\n" +
"Connection: close\r\n\r\n" +
payload;
// Send the request
client.print(request);
Serial.println("Request sent!");
// Read and print the response
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") break; // Headers ended
Serial.println(line);
}
// Close the connection
client.stop();
}
String urlEncode(const String &msg) {
String encodedMsg = "";
char c;
char code0;
char code1;
for (int i = 0; i < msg.length(); i++) {
c = msg.charAt(i);
if (c == ' ') {
encodedMsg += '+';
} else if (isalnum(c)) {
encodedMsg += c;
} else {
code1 = (c & 0xf) + '0';
if ((c & 0xf) > 9) {
code1 = (c & 0xf) - 10 + 'A';
}
c = (c >> 4) & 0xf;
code0 = c + '0';
if (c > 9) {
code0 = c - 10 + 'A';
}
encodedMsg += '%';
encodedMsg += code0;
encodedMsg += code1;
}
}
return encodedMsg;
}