forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP12E_linebot.ino
139 lines (118 loc) · 3.63 KB
/
ESP12E_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
NodeMCU (ESP12E)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-10-16 20:00
https://www.facebook.com/francefu
*/
#include <ESP8266WiFi.h> //ESP12E
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 (this: Webhooks, that: Webhooks) -> Google Apps Script > Line Bot
// Google Apps Script: https://github.com/fustyles/webduino/blob/gs/linebot_push_message.gs
String domain="maker.ifttt.com";
String key="dwGDpfZMocgp179E5zpZwuxxx";
String event="",value1="",value2="",value3="";
String request="";
// text
event="linebot_text";
value1=WiFi.localIP().toString();
request = "/trigger/"+event+"/with/key/"+key+"?value1="+value1;
Serial.println(tcp(domain,request,80,0));
Serial.println();
delay(5000);
// sticker
event="linebot_sticker";
value1="1";
value2="2";
request = "/trigger/"+event+"/with/key/"+key+"?value1="+value1+"&value2="+value2;
Serial.println(tcp(domain,request,80,0));
Serial.println();
delay(5000);
// image
event="linebot_image";
value1 = "https://video.nextmag.com.tw/photo/2016/04/25/1461565395_f66f-tile_1461575684432_555961_ver1.0.jpg";
value2 = "https://video.nextmag.com.tw/photo/2016/01/26/B766ENT02-01_1453804306593_487920_ver1.0.jpg";
request = "/trigger/linebot_image/with/key/"+key+"?value1="+value1+"&value2="+value2;
Serial.println(tcp(domain,request,80,0));
Serial.println();
delay(5000);
// vedio
// location
}
}
void loop()
{
}
String tcp(String domain,String request,int port,byte wait)
{
WiFiClient client_tcp;
if (client_tcp.connect(domain, 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 (state==true) Feedback += String(c);
if (c == '\n')
{
if (getResponse.length()==0) state=true;
getResponse = "";
}
else if (c != '\r')
getResponse += 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";
}