Stap 2: Arduino Sketch
Dit is de schets voor de arduino. Het lijkt sterk op die van mijn vorige instructables. Ik veranderde alleen de parameters van de "Get-verzoek" om te controleren of een "boodschap" was verzenden en als zo, als er een opdracht te met het sturen.
#include <SPI.h> #include <Ethernet.h> //Led for TV on pin 22 #define TV_pin 22 //Led for stereo on pin 24 #define stereo_pin 24 // Set the MAC address byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // Set the IP address IPAddress ip(192, 168, 1, 177); // Start a server at port 80 (http) EthernetServer server(80); void setup() { // Open serial communications Serial.begin(9600); // start the Ethernet connection and the server Ethernet.begin(mac, ip); server.begin(); // Pin 22 + 24 output (leds) pinMode(TV_pin, OUTPUT); pinMode(stereo_pin, OUTPUT); } void loop() { // Check if client connected EthernetClient client = server.available(); if (client) { // If there is a client... boolean currentLineIsBlank = true; String buffer = ""; // A buffer for the GET request while (client.connected()) { if (client.available()) { char c = client.read();// Read the data of the client buffer += c; // Store the data in a buffer if (c == '\n' && currentLineIsBlank) {// if 2x new line ==> Request ended // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); // Blank line ==> end response break; } if (c == '\n') { // if New line currentLineIsBlank = true; buffer = ""; // Clear buffer } else if (c == '\r') { // If cariage return... //Read in the buffer if there was send "GET /?message=..." if(buffer.indexOf("GET /?message=")>=0) { // Read the message you said and see if it is a command if(buffer.indexOf("tv on")>=0) { digitalWrite(TV_pin, HIGH); } if(buffer.indexOf("stereo on")>=0) { digitalWrite(stereo_pin, HIGH); } if(buffer.indexOf("tv off")>=0) { digitalWrite(TV_pin, LOW); } if(buffer.indexOf("stereo off")>=0) { digitalWrite(stereo_pin, LOW); } } } else { currentLineIsBlank = false; } } } delay(1); client.stop(); } }
Als u vragen over dit script hebt, post ze in het commentaar gedeelte.