I have made significant progress in utilizing the Arduino Uno to give me a status of my garage door using a push button. The concept is when the garage door is open it presses a normally open push button. The Arduino reads the change and then using an Ethernet shield and a AJAX web page it displays the garage door status.
The problem that I am having is I want the Arduino to run a php script that exists on a different web server to text message me based on the status of the garage door. I do not know what code to use to run this remote php script. I don't believe I need to send any variables from the Arduino to the PHP script because the if statement I use would run the correct script based on the status. I also need to make sure that the text message is only sent once until the status changes again.
I have found multiple examples of using a GET and POST statement but all this seems to do is post output to the Arduino AJAX web page. I have spent 14+ hours trying to do this. So, I have put in my time researching and I believe because I am a newbie I may just not understand what if anything I am doing wrong in the code.
I can go to my remote web server, run the php script and receive a text. So, I know the PHP script works. I just don't know how to make the Arduino run this remote PHP script. I have included my code below. Any help would be greatly appreciated.
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 5); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
}
void loop() // this piece of code will run over and over again
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// AJAX request for switch state
if (HTTP_req.indexOf("ajax_switch") > -1) {
// read switch state and analog input
GetAjaxData(client);
}
else { // HTTP request for web page
// send web page - contains JavaScript with AJAX calls
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Garage Door Status</title>");
client.println("<script>");
client.println("function GetSwitchAnalogData() {");
client.println(
"nocache = \"&nocache=\" + Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"sw_an_data\")\
.innerHTML = this.responseText;");
client.println("}}}}");
client.println(
"request.open(\"GET\", \"ajax_switch\" + nocache, true);");
client.println("request.send(null);");
client.println("setTimeout('GetSwitchAnalogData()', 1000);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body onload=\"GetSwitchAnalogData()\">");
client.println("<h1>Arduino Garage Door Status</h1>");
client.println("<div id=\"sw_an_data\">");
client.println("</div>");
client.println("</body>");
client.println("</html>");
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// send the state of the switch to the web browser
void GetAjaxData(EthernetClient cl)
{
int analog_val;
const int analogPin0 = A0; // pin that the sensor is attached to
const int thresholdOpen = 0; // an arbitrary threshold level that's in the range of the analog input
const int thresholdClosed = 1; // an arbitrary threshold level that's in the range of the analog input
// read analog pin A0
analog_val = analogRead(analogPin0);
cl.print("<p>Analog PIN0 (A0): "); // this text will display on the html web page
cl.print(analog_val); // if this value is anything but 0 the garage door is closed
cl.println("</p>"); // this will close the paragraph on the html web page
if (analog_val > thresholdClosed) {
cl.print("<p><font size='3' color='green'>GARAGE DOOR CLOSED</font>"); // this text will display on the html web page if the Analog 2 value is greater than 1
cl.print("<p>
");
cl.print("<img src='http://arduino.website.com/images/green_closed_sign.jpg'>");
cl.println("</p>");
}
else if (analog_val == thresholdOpen) {
cl.print("<p><font size='3' color='red'>GARAGE DOOR OPEN</font>"); // this text will display on the html web page if the Analog 2 value is equal to 0
cl.print("<p>
");
cl.print("<img src='http://arduino.website.com/images/red_open_sign.jpg'>");
cl.print("GET /http://arduino.website.com/php/email_open.php");
//cl.print(analog_val); //values commented out because I wasn't sure if I needed them
//cl.println(" HTTP/1.1"); //values commented out because I wasn't sure if I needed them
//cl.println("Host: http://arduino.website.com"); //values commented out because I wasn't sure if I needed them
//cl.println("User-Agent: Arduino"); //values commented out because I wasn't sure if I needed them
//cl.println("Accept: text/html"); //values commented out because I wasn't sure if I needed them
//cl.println(); //values commented out because I wasn't sure if I needed them
cl.print("<p>
");
cl.println("</p>");
}
}
Remote PHP script (email_open.php)
<?php
$to = "5555555555@txt.att.net";
$subject = "GARAGE OPEN";
$message = "GARAGE DOOR OPEN";
$from = "user@email.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Remote PHP script (email_closed.php)
<?php
$to = "5555555555@txt.att.net";
$subject = "GARAGE CLOSED";
$message = "GARAGE DOOR CLOSED";
$from = "user@email.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>