-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExternalClient.ino
166 lines (138 loc) · 4.68 KB
/
ExternalClient.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
/**
* Created by K. Suwatchai (Mobizt)
*
* Email: [email protected]
*
* Github: https://github.com/ESP-Line-Notify
*
* Copyright (c) 2022 mobizt
*
*/
/**
* This example showed how to send the notified message via the Line Notify agent
* with external Client.
* This example used SAMD21 device and WiFiNINA as the client.
*
*/
#if defined(ARDUINO_ARCH_SAMD)
#include <WiFiNINA.h>
#endif
#include <ESP_Line_Notify.h>
/* Set your WiFI AP credential */
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
/* Define the LineNotifyClient object */
LineNotifyClient line;
/* Function to print the sending result via Serial (optional) */
void printRessult(LineNotifySendingResult result);
/* The sending callback function (optional) */
void sendingCallback(LineNotifySendingResult result);
WiFiSSLClient client;
void networkConnection()
{
// Reset the network connection
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
unsigned long ms = millis();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
if (millis() - ms >= 5000)
{
Serial.println(" failed!");
return;
}
}
Serial.println();
Serial_Printf("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
}
// Define the callback function to handle server status acknowledgement
void networkStatusRequestCallback()
{
// Set the network status
line.setNetworkStatus(WiFi.status() == WL_CONNECTED);
}
// Define the callback function to handle server connection
void tcpConnectionRequestCallback(const char *host, int port)
{
Serial.print("Connecting to server via external Client... ");
if (!client.connect(host, port))
{
Serial.println("failed.");
return;
}
Serial.println("success.");
}
void setup()
{
Serial.begin(115200);
#if defined(ARDUINO_ARCH_SAMD)
while (!Serial)
;
#endif
networkConnection();
/* line.setExternalClient and line.setExternalClientCallbacks must be called before Firebase.begin */
/* Assign the pointer to global defined WiFiClient object */
line.setExternalClient(&client);
/* Assign the required callback functions */
line.setExternalClientCallbacks(tcpConnectionRequestCallback, networkConnection, networkStatusRequestCallback);
Serial.println("Sending Line Notify message...");
line.token = "Your Line Notify Access Token";
line.message = "Hello world";
LineNotifySendingResult result = LineNotify.send(line);
// Print the sending result
printRessult(result);
}
void loop()
{
}
/* Function to print the sending result via Serial */
void printRessult(LineNotifySendingResult result)
{
if (result.status == LineNotify_Sending_Success)
{
Serial.printf("Status: %s\n", "success");
Serial.printf("Text limit: %d\n", result.quota.text.limit);
Serial.printf("Text remaining: %d\n", result.quota.text.remaining);
Serial.printf("Image limit: %d\n", result.quota.image.limit);
Serial.printf("Image remaining: %d\n", result.quota.image.remaining);
Serial.printf("Reset: %d\n", result.quota.reset);
}
else if (result.status == LineNotify_Sending_Error)
{
Serial.printf("Status: %s\n", "error");
Serial.printf("error code: %d\n", result.error.code);
Serial.printf("error msg: %s\n", result.error.message.c_str());
}
}
/* The sending callback function (optional) */
void sendingCallback(LineNotifySendingResult result)
{
if (result.status == LineNotify_Sending_Begin)
{
Serial.println("Sending begin");
}
else if (result.status == LineNotify_Sending_Upload)
{
Serial.printf("Uploaded %s, %d%s\n", result.file_name.c_str(), (int)result.progress, "%");
}
else if (result.status == LineNotify_Sending_Success)
{
Serial.println("Sending success\n\n");
Serial.printf("Text limit: %d\n", result.quota.text.limit);
Serial.printf("Text remaining: %d\n", result.quota.text.remaining);
Serial.printf("Image limit: %d\n", result.quota.image.limit);
Serial.printf("Image remaining: %d\n", result.quota.image.remaining);
Serial.printf("Reset: %d\n", result.quota.reset);
}
else if (result.status == LineNotify_Sending_Error)
{
Serial.println("Sending failed\n\n");
Serial.printf("error code: %d\n", result.error.code);
Serial.printf("error msg: %s\n", result.error.message.c_str());
}
}