#include #include // This code has been borrowed/copied/overwritten from many different sources // This code allows you to control a transistors base which controls a button on a remote // to turn ON an electrical outlet using a PHP server. // Use this code at your own risk // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192,168,1,177 }; // arduino IP address (change according to your network) byte server[] = { 192,168,1,3 }; // php server IP address (change it to yours) char incomingByte = 0; // for incoming serial data char prevByte = 0; // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): Client client(server, 80); void setup() { // start the Ethernet connection: Ethernet.begin(mac, ip); Serial.begin(9600); // opens serial port, sets data rate to 9600 bps // set the 2 pins as output to the transistor pinMode(7, OUTPUT); pinMode(8, OUTPUT); // set them both to low to start digitalWrite(7, LOW); digitalWrite(8, LOW); // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting..."); } void loop() { digitalWrite(7, LOW); digitalWrite(8, LOW); if (client.connect()) { Serial.println("connected"); // Make a HTTP request: get the "1" or "0" from the web server client.println("GET /index.php"); client.println(); } else { // kf you didn't get a connection to the server: Serial.println("connection failed"); } // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { incomingByte = client.read(); Serial.print("I received: "); Serial.println(incomingByte); client.flush(); } if (prevByte != incomingByte){ // if "1" is received turn the transistor pin 7 ON for 1 second if (incomingByte == '1'){ digitalWrite(7, HIGH); delay(1000); digitalWrite(7, LOW); } // if "0" is received turn the transistor pin 8 ON for 1 second else if (incomingByte == '0') { digitalWrite(8, HIGH); delay(1000); digitalWrite(8, LOW); } prevByte = incomingByte; } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); delay(6000); } }