ethernet shield module w5100

Hi,

I just bought an Ethernet shield module W5100 and am using it with Arduino Uno R3(Atmega 16U2)

the Ethernet shield didn’t cam with any MAC Add, so I give it one.

After setting the IP add, am able to ping the Ethernet shield by I get get it in browser.

What’s the use of the server port?

here’s the code :

/*

Home Automation Created By Bn4al 01/06/2016

*/

#include <SPI.h>

#include <Ethernet.h>

EthernetServer server(80);// Server port

const byte lightPin = 5;// Select pin for Light

const byte FanPin = 6;// Select pin for Fan

const byte LampPin = 7;// Select pin for Lamp

const byte AlarmPin = 8;// Select pin for Alarm

const byte WaterPumpPin = 9;// Select pin for WaterPump

const byte ChandellierPin = 10;// Select pin for Chandellier

const byte CeilingPin = 11;// Select pin for Ceiling

const byte AuxillaryPin = 12;// Select pin for Auxillary

byte mac = { 0x00, 0x08, 0xDC, 0xAB, 0xCD, 0xEF };// Physical MAC address

byte ip = { 192, 168, 100, 25 };// Fixed IP address

byte gateway = { 192, 168, 100, 1 };// Router Gateway Internet access

byte subnet = { 255, 255, 255, 0 };// Subnet mask

String readString;

void setup()

{

delay(300);// Delay for Ethernet shield initialization (Arduino has 65mS Power Up delay and W5100 reset logic has 280mS)

pinMode(lightPin, OUTPUT);// Define pin for Light as output

pinMode(FanPin, OUTPUT);// Define pin for Fan as Output

pinMode(LampPin, OUTPUT);// Define pin for Lamp as Output

pinMode(AlarmPin, OUTPUT);// Define pin for Alarm

pinMode(WaterPumpPin, OUTPUT);// Define pin for WaterPump

pinMode(ChandellierPin, OUTPUT);// Define pin for Chandellier

pinMode(CeilingPin, OUTPUT);// Define pin for Ceiling

pinMode(AuxillaryPin, OUTPUT);// Define pin for Auxillary

Serial.begin(9600);// Initialize serial communications at 9600 bps

Serial.println(F(“Home Automation”));// Display Arduino title

Ethernet.begin(mac, ip, gateway, subnet);// Start Ethernet

server.begin();

Serial.print(F(“System initialized. Local IP address is:”));

Serial.println(Ethernet.localIP());// Print Server IP address

}

void loop()

{

EthernetClient client = server.available();// Create a client connection

if (client == true)

{

while (client.connected())

{

if (client.available())

{

char c = client.read();// Read char by char HTTP request

if (readString.length() < 100)

{

readString = readString + c;// Store characters to string

}

if (c == ‘\n’)

{

Serial.println(readString);

client.println(F(“http/1.1 200 ok”));// Send standard http headers

client.println(F(“content-type: text/html”));

client.println();

client.println(F("<!doctype html>"));

client.println(F(""));

client.println(F(“Home Automation”));

client.println(F(“

Home Automation

”));

client.println(F("Bn4al

"));

client.println(F("LOGO"));

client.println(F("


Click the Buttons to turn On and OFF


"));

client.print(F("<input type=button value=‘Light ON’ style=‘width:120px;’ onmousedown=location.href=’/?ML_on’>"));

client.println(F("<input type=button value=‘Light OFF’ style=‘width:120px;’ onmousedown=location.href=’/?ML_off’>

"));

client.print(F("<input type=button value=‘Fan ON’ style=‘width:120px;’ onmousedown=location.href=’/?FN_on’>"));

client.println(F("<input type=button value=‘Fan OFF’ style=‘width:120px;’ onmousedown=location.href=’/?FN_off’>

"));

client.print(F("<input type=button value=‘Lamp ON’ style=‘width:120px;’ onmousedown=location.href=’/?LM_on’>"));

client.println(F("<input type=button value=‘Lamp OFF’ style=‘width:120px;’ onmousedown=location.href=’/?LM_off’>

"));

client.print(F("<input type=button value=‘Alarm ON’ style=‘width:120px;’ onmousedown=location.href=’/?AL_on’>"));

client.println(F("<input type=button value=‘Alarm OFF’ style=‘width:120px;’ onmousedown=location.href=’/?AL_off’>


"));

client.print(F("<input type=button value=‘WaterPump ON’ style=‘width:120px;’ onmousedown=location.href=’/?WP_on’>"));

client.println(F("<input type=button value=‘WaterPump OFF’ style=‘width:120px;’ onmousedown=location.href=’/?WP_off’>


"));

client.print(F("<input type=button value=‘Chandellier ON’ style=‘width:120px;’ onmousedown=location.href=’/?CH_on’>"));

client.println(F("<input type=button value=‘Chandellier OFF’ style=‘width:120px;’ onmousedown=location.href=’/?CH_off’>


"));

client.print(F("<input type=button value=‘Ceiling ON’ style=‘width:120px;’ onmousedown=location.href=’/?CL_on’>"));

client.println(F("<input type=button value=‘Ceiling OFF’ style=‘width:120px;’ onmousedown=location.href=’/?CL_off’>


"));

client.print(F("<input type=button value=‘Auxillary ON’ style=‘width:120px;’ onmousedown=location.href=’/?AU_on’>"));

client.println(F("<input type=button value=‘Auxillary OFF’ style=‘width:120px;’ onmousedown=location.href=’/?AU_off’>


"));

client.println(F(""));

delay(1);// Page loading delay

client.stop();// Stopping client

if(readString.indexOf("/?ML_on") > 0) digitalWrite(lightPin, HIGH);// Switch on Light

if(readString.indexOf("/?ML_off") > 0) digitalWrite(lightPin, LOW);// Switch off Light

if(readString.indexOf("/?FN_on") > 0) digitalWrite(FanPin, HIGH);// Switch on Fan

if(readString.indexOf("/?FN_off") > 0) digitalWrite(FanPin, LOW);// Switch off Fan

if(readString.indexOf("/?LM_on") > 0) digitalWrite(LampPin, HIGH);// Switch on Lamp

if(readString.indexOf("/?LM_off") > 0) digitalWrite(LampPin, LOW);// Switch off Lamp

if(readString.indexOf("/?AL_on") > 0) digitalWrite(AlarmPin, HIGH);// Switch on Alarm

if(readString.indexOf("/?AL_off") > 0) digitalWrite(AlarmPin, LOW);// Switch off Alarm

if(readString.indexOf("/?WP_on") > 0) digitalWrite(WaterPumpPin, HIGH);// Switch on WaterPump

if(readString.indexOf("/?WP_off") > 0) digitalWrite(WaterPumpPin, LOW);// Switch off WaterPump

if(readString.indexOf("/?CH_on") > 0) digitalWrite(ChandellierPin, HIGH);// Switch on Chandellier

if(readString.indexOf("/?CH_off") > 0) digitalWrite(ChandellierPin, LOW);// Switch off Chandellier

if(readString.indexOf("/?CL_on") > 0) digitalWrite(CeilingPin, HIGH);// Switch on Ceiling

if(readString.indexOf("/?CL_off") > 0) digitalWrite(CeilingPin, LOW);// Switch off Ceiling

if(readString.indexOf("/?AU_on") > 0) digitalWrite(AuxillaryPin, HIGH);// Switch on Auxilary

if(readString.indexOf("/?AU_off") > 0) digitalWrite(AuxillaryPin, LOW);// Switch off Auxilary

readString = “”;// Clearing string for next read

}// End of line reached

}// End of client available

}// End of client connected

}// End of client connection

}// End of loop

thanks,

Bn4al

First off I recommend you use code tags to post code cos somebody gonna get a little p1ssy about it LOL

Second you can find the MAC by looking at the items connected to your router. Go to your router setup page. (usually under DHCP) or use some other software to look for the MAC. They tend to be fixed but there are methods to spoof them. Try the "real" MAC in place of your made up one.

Server ports have different functions depending on what you want to do. There are also many ways of talking to servers. If its just serving up a simple web page for you to browse values sent from the Arduino then all you need to do is browse to the IP address. If its a little more interactive then you may also be able to enter and change values. Sometimes via the address bar or maybe even using a mouse.

There are COMMON ports depending on what you want to actually do. "listed here"

General rule is don't mix up what you want to do with a port that has a function your network may depend on. EG dont use a timekeeping port for a web browsing port. Randomly defining a server port could take other things on your network offline or unable to function properly.

First off, this is incorrect. There is a parameter missing. I'll use the gateway for the DHCP server.

// change this
  Ethernet.begin(mac, ip, gateway, subnet);// Start Ethernet
// to this
  Ethernet.begin(mac, ip, gateway, gateway, subnet);// Start Ethernet

There is no "real MAC" for the ethernet shield. If the ethernet shield came with a MAC address, use it. If not, use DEAD BEEF FEED. The router's DHCP or ARP table will show whatever you use.

Ballscrewbob covered the port thing.

Hi, I finally have been able to make it work with the following codes :

/*
Created by Bn4al
*/

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int led1 = 5;
int led2 = 6;
int led3 = 8;
int led4 = 9;
int led5 = 4;
int led6 = 3;
int led7 = 2;
Servo microservo;
int pos = 0;
byte mac = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip = { 192, 168, 100, 25 }; // ip in lan
byte gateway = { 192, 168, 100, 1 }; // internet access via router
byte subnet = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
microservo.attach(7);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == ‘\n’) {
Serial.println(readString); //print to serial monitor for debuging

client.println(“HTTP/1.1 200 OK”); //send new page
client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("");
client.println("");
client.println("");
client.println("");
client.println(“Genesis Home Automation”);
client.println("");
client.println("");
client.println(“

Home Automation

”);
client.println(“

Home Control System

”);
int sensorReading = analogRead(A4);
float temperature = (sensorReading5100) / 1024;
client.print("

");
client.print(“Room Temperature: “);
client.print(temperature);
client.print(” C”);
client.println("

“);
client.println(”<a href="/?button1on"">Light ON");
client.println("<a href="/?button1off"">Light OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button3on"">FAN ON");
client.println("<a href="/?button3off"">FAN OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button4on"">lamp ON");
client.println("<a href="/?button4off"">Lamp OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button6on"">Pump ON");
client.println("<a href="/?button6off"">Pump OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button7on"">Alarm ON");
client.println("<a href="/?button7off"">Alarm OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button8on"">lamp3 ON");
client.println("<a href="/?button8off"">Lamp3 OFF
“);
client.println(”
“);
client.println(”
“);
client.println(”<a href="/?button9on"">lamp4 ON");
client.println("<a href="/?button9off"">Lamp4 OFF
“);
client.println(”
“);
client.println(”
“);
client.println(“

Created by Bn4al

”);
client.println(”
“);
client.println(”");
client.println("");

delay(1);
//stopping client
client.stop();
//controls the Arduino if you press the buttons
if (readString.indexOf("?button1on") >0){
digitalWrite(led1, HIGH);
}
if (readString.indexOf("?button1off") >0){
digitalWrite(led1, LOW);
}

if (readString.indexOf("?button3on") >0){
digitalWrite(led2, HIGH);
}
if (readString.indexOf("?button3off") >0){
digitalWrite(led2, LOW);
}
if (readString.indexOf("?button4on") >0){
digitalWrite(led3, HIGH);
}
if (readString.indexOf("?button4off") >0){
digitalWrite(led3, LOW);
}
if (readString.indexOf("?button6on") >0){
digitalWrite(led4, HIGH);
}
if (readString.indexOf("?button6off") >0){
digitalWrite(led4, LOW);
}
if (readString.indexOf("?button7on") >0){
digitalWrite(led5, HIGH);
}
if (readString.indexOf("?button7off") >0){
digitalWrite(led5, LOW);
}
if (readString.indexOf("?button8on") >0){
digitalWrite(led6, HIGH);
}
if (readString.indexOf("?button8off") >0){
digitalWrite(led6, LOW);
}
if (readString.indexOf("?button9on") >0){
digitalWrite(led7, HIGH);
}
if (readString.indexOf("?button9off") >0){
digitalWrite(led7, LOW);
}

if (readString.indexOf("?button2on") >0){
for(pos = 0; pos < 180; pos += 3) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
microservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
if (readString.indexOf("?button2off") >0){
for(pos = 180; pos>=1; pos-=3) // goes from 180 degrees to 0 degrees
{
microservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
//clearing string for next read
readString="";

}
}
}
}
}

OMG…please use CODE TAGS.

its the one that looks like “</>” on the small toolbar just above the smileys.

Maybe go practice that one please
Or even use the function in the IDE in the EDIT section called “Copy for Forum”

Once is an easy mistake second time would indicate a distinct lack of attention on your part.

OK so a new set of code works.

Great so whats the question ?