Hardware Reference
In-Depth Information
There are
only a
few changes you need to make to the
server to get it to be a voice server.
The first is in the loop() . When Twilio
makes a POST request, it sends back
the digits that the caller pressed on the
phone keypad using a variable called
Digits. So where you were looking for a
variable called thermostat in the POST
request, you'll now look for Digits. The
new line is shown in blue.
Modify the Server
case 2: //POST
// skip the rest of the header,
// which ends with newline and carriage return:
finder.find("\n\r");
// if the client sends a value for thermostat, take it:
if (finder.find("Digits")) {
int newThermostat = finder.getValue('=');
8
The Twilio gateway that acts as
a client to your server is a bit more
picky about what it expects than most
browsers. It works best when you tell
it in advance the length of the content
you're going to send it. That means
you need to make some changes to
sendFile() and sendHttpHeader() .
First, in sendFile() , you're going to get
the size of the file you're sending, and
identify whether the file you're sending
is HTML or XML. Then you'll send the
file size and type to the sendHttp-
Header() method. The beginning of the
sendFile() changes, as follows, are in
blue.
// send the file that was requested:
void sendFile(Client thisClient, char thisFile[]) {
String outputString = ""; // a String to get each line of the file
// open the file for reading:
File myFile = SD.open(thisFile);
if (myFile) {
// get the file size:
int mySize = myFile.size();
// determine whether the file is XML or HTML
// based on the extension (but assume it's HTML):
int fileType = 1; // 1 = html, 2 = xml
if (String(thisFile).endsWith("xml")) {
fileType = 2;
}
// send an OK header:
sendHttpHeader(thisClient, 200, mySize, fileType);
// read from the file until there's nothing else in it:
while (myFile.available()) {
8
You also need to change the call
to sendHttpHeader() for a 404 error,
which occurs later in sendFile() , as
follows.
else {
// if the file didn't open:
sendHttpHeader(thisClient, 404, 0, 1);
}
NOTE: When developing this project, I benefited greatly by
writing a test server—like the one described in Chapter 4—to
simply read the whole request from Twilio before I wrote my
final code. Twilio's online debugger also helped me find the
problems.
 
Search WWH ::




Custom Search