forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_LineNotify.ino
121 lines (107 loc) · 3.11 KB
/
ESP32_LineNotify.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
/*
NodeMCU (ESP32)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-10-09 21:00
https://www.facebook.com/francefu
*/
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = ""; // your network SSID
const char* password = ""; // your network password
String token = ""; // LineNotify Token
void setup() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
delay(1000);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
long int StartTime=millis();
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if ((StartTime+10000) < millis()) break;
}
if (WiFi.status() == WL_CONNECTED)
{
pinMode(2, OUTPUT);
for (int i=0;i<5;i++)
{
digitalWrite(2,HIGH);
delay(100);
digitalWrite(2,LOW);
delay(100);
}
Serial.println();
Serial.println("STAIP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
else
Serial.println("Unable to connect!");
//Push a message to LineNotify
if (WiFi.status() == WL_CONNECTED) {
/*
request = message=xxxxx
request = message=xxxxx&stickerPackageId=xxxxx&stickerId=xxxxx
*/
String request = "message=Taiwan\nI'm a \"Maker\"";
request.replace(" ","%20");
request.replace("&","%20");
request.replace("#","%20");
//request.replace("\'","%27");
request.replace("\"","%22");
request.replace("\n","%0D%0A");
request += "&stickerPackageId=1&stickerId=2";
String Response = LineNotify(request, 1);
Serial.println(Response);
}
}
void loop()
{
}
String LineNotify(String request, byte wait)
{
WiFiClientSecure client_tcp;
if (client_tcp.connect("notify-api.line.me", 443))
{
client_tcp.println("POST /api/notify HTTP/1.1");
client_tcp.println("Connection: close");
client_tcp.println("Host: notify-api.line.me");
client_tcp.println("User-Agent: ESP8266/1.0");
client_tcp.println("Authorization: Bearer " + token);
client_tcp.println("Content-Type: application/x-www-form-urlencoded");
client_tcp.println("Content-Length: " + String(request.length()));
client_tcp.println();
client_tcp.println(request);
client_tcp.println();
String getResponse="",Feedback="";
boolean state = false;
int waitTime = 3000; // timeout 3 seconds
long startTime = millis();
while ((startTime + waitTime) > millis())
{
while (client_tcp.available())
{
char c = client_tcp.read();
if (c == '\n')
{
if (getResponse.length()==0) state=true;
getResponse = "";
}
else if (c != '\r')
getResponse += String(c);
if (state==true) Feedback += String(c);
if (wait==1)
startTime = millis();
}
if (wait==0)
if ((state==true)&&(Feedback.length()!= 0)) break;
}
client_tcp.stop();
return Feedback;
}
else
return "Connection failed";
}