forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_linebot.ino
121 lines (104 loc) · 3.26 KB
/
ESP32_linebot.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-16 20:00
https://www.facebook.com/francefu
*/
#include <WiFi.h> //ESP32
const char* ssid = "xxxxx"; //your network SSID
const char* password = "xxxxx"; //your network password
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,LOW);
delay(100);
digitalWrite(2,HIGH);
delay(100);
}
Serial.println();
Serial.println("STAIP address: ");
Serial.println(WiFi.localIP());
Serial.println();
}
else
Serial.println("Unable to connect!");
if (WiFi.status() == WL_CONNECTED)
{
// IFTTT (webhooks+webhooks) + Google Apps Script + Line Bot
String domain="maker.ifttt.com";
String key="dwGDpfZMocgp179E5zpZwuxxx";
String request="";
String text = WiFi.localIP().toString();
request = "/trigger/linebot_text/with/key/"+key+"?value1="+text;
Serial.println(tcp(domain,request,80,0));
Serial.println();
delay(5000);
request = "/trigger/linebot_sticker/with/key/"+key+"?value1=1&value2=2";
Serial.println(tcp(domain,request,80,0));
Serial.println();
delay(5000);
String originalContentUrl = "https://video.nextmag.com.tw/photo/2016/04/25/1461565395_f66f-tile_1461575684432_555961_ver1.0.jpg";
String previewImageUrl = "https://video.nextmag.com.tw/photo/2016/01/26/B766ENT02-01_1453804306593_487920_ver1.0.jpg";
request = "/trigger/linebot_image/with/key/"+key+"?value1="+originalContentUrl+"&value2="+previewImageUrl;
Serial.println(tcp(domain,request,80,0));
}
}
void loop()
{
}
String tcp(String domain,String request,int port,byte wait)
{
WiFiClient client_tcp;
if (client_tcp.connect(domain.c_str(), port))
{
Serial.println("GET " + request);
client_tcp.println("GET " + request + " HTTP/1.1");
client_tcp.println("Host: " + domain);
//client_tcp.println("Connection: close");
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";
}