This sketch enables all those pins for output and then turns them on and off for half a second (500 milliseconds) in sequence.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x50, 0xA0 }; //physical mac address
byte ip[] = { 192, 168, 1, 177 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
byte sampledata=50; //some sample data - outputs 2 (ascii = 50 DEC)
String readString = String(30); //string for fetching data from address
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pins 2 through 9 to output
for (int i = 2; i <= 9; i++){
pinMode(i, OUTPUT);
}
//enable serial data print
Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 30) {
//store characters to string
readString.concat(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
if(readString.indexOf("test=all") > -1) {
httpReply(client);
// cycle through pins 2 to 9
for (int i = 2; i <= 9; i++){
Serial.print("Writing pin ");
Serial.print(i);
Serial.println(" HIGH");
digitalWrite(i, HIGH); // set the LED on
delay(500);
Serial.print("Writing pin ");
Serial.print(i);
Serial.println(" LOW");
digitalWrite(i, LOW);
delay(500);
}
}
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
void httpReply(Client client){
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("{\"status\" : \"executing\" , \"cmd\" : \"test=all\"}");
client.stop();
}
This code is loosely derived from code by Mareika Giacobbi of nerdydog.it for his Domotic Home project.
No comments:
Post a Comment