This sketch connects with the Arduino through a DHCP assigned IP address. Serial monitor prints out the unique IP. The IP address gets put into Terminal using the telnet function. Using some of the preprogramed functions “ledon” “ledoff” “photo” and “I don’t Understand”. They are pretty self explanatory – but ledon will send a command through the internet that will send a voltage to pin 8, which activates the LED. This test worked.
/*
DHCP Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device’s IP address and type.
You can see the client’s input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
THis version attempts to get an IP address using DHCP
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
modified 9 Apr 2012
by Tom Igoe
Based on ChatServer example by David A. Mellis
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0xFB };
IPAddress ip(149,31,195, 76);
IPAddress gateway(149,31,195, 1);
IPAddress subnet(255, 255, 255, 0);
// telnet defaults to port 23
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected
int ledPinB = 8;
String commandString;
void setup() {
//set digital pin as output
pinMode(ledPinB,OUTPUT);
Ethernet.begin(mac,ip,gateway,subnet);
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
while (!Serial){}
Serial.print(“Chat server address:”);
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
//When client sends first byte, say hello
if (client) {
if (!alreadyConnected) {
// clear out the input buffer:
client.flush();
commandString = “”; //clear the commandString variable
server.println(“–> Please type your command and hit Return…”);
alreadyConnected = true;
}
while (client.available()) {
// read the bytes incoming from the client:
char newChar = client.read();
if (newChar == 0x0D) //If a 0x0D is received, a Carriage Return, then evaluate the command
{
server.print(“Received this command: “);
server.println(commandString);
processCommand(commandString);
} else
{
Serial.println(newChar);
commandString += newChar;
}
}
}
}
void processCommand(String command) {
server.println(“Proccessing command”);
server.print(command);
if (command.indexOf(‘photo’) > -1) {
Serial.println(“Photo command recieved”);
server.print(“Reading from photoresistor: “);
server.print(analogRead(A0));
commandString = “”;
return;
}
if (command.indexOf(“ledOn”) > -1) {
Serial.println(“LED ON command recieved”);
digitalWrite(ledPinB, HIGH);
server.println(“lED turned ON”);
commandString = “”;
return;
}
if (command.indexOf(“ledOFF”) > -1) {
Serial.println(“LED OFF command recieved”);
digitalWrite(ledPinB, LOW);
server.println(“LED turned OFF”);
commandString = “”;
return;
}
commandString = “”;
instructions();
}
void instructions()
{
server.println(“I don’t understand”);
server.println(“Please use one of these commands:”);
server.println(“* photo, to get a reading from the photoresistor”);
server.println(“* ledon, to turn on the LED”);
server.println(“* ledoff, to turn off the LED”);
}