-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagicButton.ino
More file actions
105 lines (88 loc) · 2.52 KB
/
magicButton.ino
File metadata and controls
105 lines (88 loc) · 2.52 KB
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
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <base64.h>
int ledPin = D4;
const char* ssid = "*********";
const char* password = "********";
const char* accountSID = "*****************************";
const char* authToken = "******************************";
const char* fromNumber = "+***********";
const char* toNumber = "+****************";
const char *twilioServer = "api.twilio.com";
const int twilioPort = 443;
WiFiClientSecure client;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
delay(10);
Serial.println();
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Initialize secure client
client.setInsecure();
}
void loop()
{
if (digitalRead(D1) == LOW)
{
makeVoiceCall();
digitalWrite(ledPin, HIGH);
delay(1000); // Debounce delay
}
else
{
digitalWrite(ledPin, LOW);
}
}
void makeVoiceCall()
{
Serial.println("Making voice call...");
if (client.connect(twilioServer, twilioPort))
{
digitalWrite(ledPin, HIGH);
// Prepare Twilio credentials in base64 format
String credentials = accountSID;
credentials += ":";
credentials += authToken;
String base64Credentials = base64::encode(credentials);
// Construct the POST request body
String postBody = "Url=https://demo.twilio.com/docs/voice.xml&To=";
postBody += toNumber;
postBody += "&From=";
postBody += fromNumber;
// HTTP request
client.println("POST /2010-04-01/Accounts/" + String(accountSID) + "/Calls.json HTTP/1.1");
client.println("Host: api.twilio.com");
client.print("Authorization: Basic ");
client.println(base64Credentials);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(String(postBody.length()));
client.println();
client.println(postBody);
// Wait for response
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
}
client.stop();
Serial.println();
Serial.println("Voice call made.");
}
else
{
Serial.println("Connection to Twilio failed");
}
}