I'm trying to test this code out from Getting hands on Arduino Ethernet Shield - Do It Easy With ScienceProg at very bottom . When I compiled this code, I get an error saying that a "stray \ in program" on the underlined code. The problem is that it is using double forward slashes, which comments out the entire link. How do I fix this problem?
#include <WString.h>
#include <Ethernet.h>
/*
Simple Ethernet Test
Arduino server outputs simple text to browser
and controlling LED with simple checkbox
The circuit:
* Arduino Duemilanove
* Arduino Ethernet shield
* Basic FTDI breakout 5V
* LED connected to GND and digital pin 4 via resistor
By Minde
http://www.sciencprog.com/
*/
byte mac[] = { 0xF0, 0x7B, 0xCB, 0x20, 0xE9, 0x91 }; //physical mac address
byte ip[] = { 134, 71, 201, 251 }; // ip in lan
byte gateway[] = { 134, 71, 201, 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)
int ledPin = 13; // LED pin
[u][b]char link[]= ”http://www.scienceprog.com/”;[/b][/u]
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada 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.append(c); }
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == ‘\n’) {
//lets check if LED should be lighted
if(readString.contains(“L=1?)) {
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }
// now output HTML data starting with standart header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
//set background to yellow
client.print(“<body style=background-color:yellow>”);
//send first heading
client.println(“<font color=’red’><h1>HTTP test routines</font></h1>”);
client.println(“<hr />”);
client.println(“<hr />”);
//output some sample data to browser
client.println(“<font color=’blue’ size=’5?>Sample data: “);
client.print(sampledata);//lets output some data
client.println(“
”);//some space between lines
client.println(“<hr />”);
//drawing simple table
client.println(“<font color=’green’>Simple table: “);
client.println(“
”);
client.println(“<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>”);
client.println(“<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>”);
client.println(“
”);
client.println(“<hr />”);
//printing some link
client.println(“<font color=’blue’ size=’5?>Link: “);
client.print(“<a href=”);
client.print(link);
client.println(“>Visit Scienceprog!</a>”);
client.println(“
”);
client.println(“<hr />”);
//controlling led via checkbox
client.println(“<h1>LED control</h1>”);
//address will look like http://192.168.1.110/?L=1 when submited
client.println(“<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>”);
client.println(“
”);
//printing LED status
client.print(“<font size=’5?>LED status: “);
if (LEDON)
client.println(“<font color=’green’ size=’5?>ON”);
else
client.println(“<font color=’grey’ size=’5?>OFF”);
client.println(“<hr />”);
client.println(“<hr />”);
client.println(“</body></html>”);
//clearing string for next read
readString=”";
//stopping client
client.stop();
}}}}}
This is a classic issue while cutting and pasting code from an html page. Replace all single and double quotes with ones typed from your keyboard and you should be good to go.
okay, I've fixed the quotation issues, but now another error came up
'class String' has no member named 'append'
How do I fix this?
Im using Arduino 022 if that helps?
#include <WString.h>
#include <Ethernet.h>
/*
Simple Ethernet Test
Arduino server outputs simple text to browser
and controlling LED with simple checkbox
The circuit:
* Arduino Duemilanove
* Arduino Ethernet shield
* Basic FTDI breakout 5V
* LED connected to GND and digital pin 4 via resistor
By Minde
http://www.sciencprog.com/
*/
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 110 }; // 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)
int ledPin = 4; // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada 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.append(c); }
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//lets check if LED should be lighted
if(readString.contains("L=1")) {
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//set background to yellow
client.print("<body style=background-color:yellow>");
//send first heading
client.println("<font color=’red’><h1>HTTP test routines</font></h1>");
client.println("<hr />");
client.println("<hr />");
//output some sample data to browser
client.println("<font color='blue' size='5'>Sample data: ");
client.print(sampledata);//lets output some data
client.println("
");//some space between lines
client.println("<hr />");
//drawing simple table
client.println("<font color='green'>Simple table: ");
client.println("
");
client.println("<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>");
client.println("<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>");
client.println("
");
client.println("<hr />");
//printing some link
client.println("<font color='blue' size='5'>Link: ");
client.print("<a href=");
client.print(link);
client.println(">Visit Scienceprog!</a>");
client.println("
");
client.println("<hr />");
//controlling led via checkbox
client.println("<h1>LED control</h1>");
//address will look like http://192.168.1.110/?L=1 when submited
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
client.println("
");
//printing LED status
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}}}}}
Okay, I've now fix the code and it compiled alright.
So now, I think I'm suppose to go to the ethernet sheild's ip address. I typed in 192.168.0.106 on the browser , but it does not take me anywhere. Am I forgetting something?
#include <SPI.h>
#include <WString.h>
#include <Ethernet.h>
/*
Simple Ethernet Test
Arduino server outputs simple text to browser
and controlling LED with simple checkbox
The circuit:
* Arduino Duemilanove
* Arduino Ethernet shield
* Basic FTDI breakout 5V
* LED connected to GND and digital pin 4 via resistor
By Minde
http://www.sciencprog.com/
*/
byte mac[] = { 0xF0, 0x7B, 0xCB, 0x20, 0xE9, 0x91 }; //physical mac address
byte ip[] = { 192, 168, 0, 106 }; // ip in lan
byte gateway[] = { 192, 168, 0, 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)
int ledPin = 13; // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada 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') {
//lets check if LED should be lighted
if(readString.indexOf("L=1")) {
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//set background to yellow
client.print("<body style=background-color:yellow>");
//send first heading
client.println("<font color=’red’><h1>HTTP test routines</font></h1>");
client.println("<hr />");
client.println("<hr />");
//output some sample data to browser
client.println("<font color='blue' size='5'>Sample data: ");
client.print(sampledata);//lets output some data
client.println("
");//some space between lines
client.println("<hr />");
//drawing simple table
client.println("<font color='green'>Simple table: ");
client.println("
");
client.println("<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>");
client.println("<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>");
client.println("
");
client.println("<hr />");
//printing some link
client.println("<font color='blue' size='5'>Link: ");
client.print("<a href=");
client.print(link);
client.println(">Visit Scienceprog!</a>");
client.println("
");
client.println("<hr />");
//controlling led via checkbox
client.println("<h1>LED control</h1>");
//address will look like http://192.168.1.110/?L=1 when submited
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
client.println("
");
//printing LED status
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}}}}}
I modified the below code to use with IDE21 and tested it. I don't have an LED connected to test, but everything seems to work ok.
//#include <WString.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port
byte sampledata=50; //some sample data – outputs 2 (ascii = 50 DEC)
int ledPin = 13; // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
pinMode(ledPin, OUTPUT);
//enable serial datada 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.append(c); }
readString += c; }
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
//lets check if LED should be lighted
if(readString.indexOf("L=1") >0) {
//if(readString.contains("L=1")) {
//led has to be turned ON
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
//led has to be turned OFF
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false; }
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//set background to yellow
client.print("<body style=background-color:yellow>");
//send first heading
client.println("<font color='red'><h1>HTTP test routines</font></h1>");
client.println("<hr />");
client.println("<hr />");
//output some sample data to browser
client.println("<font color='blue' size='5'>Sample data: ");
client.print(sampledata);//lets output some data
client.println("
");//some space between lines
client.println("<hr />");
//drawing simple table
client.println("<font color='green'>Simple table: ");
client.println("
");
client.println("<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>");
client.println("<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>");
client.println("
");
client.println("<hr />");
//controlling led via checkbox
client.println("<h1>LED control</h1>");
//address will look like http://192.168.1.110/?L=1 when submited
client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED
<input type=submit value=submit></form>");
client.println("
");
//printing LED status
client.print("<font size='5'>LED status: ");
if (LEDON)
client.println("<font color='green' size='5'>ON");
else
client.println("<font color='grey' size='5'>OFF");
client.println("<hr />");
client.println("<hr />");
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}}}}}
Looks like you are using Pin13, which already has a LED connected to it.
Pin 13 doesn't operate as other pins when using server code. It momentarily blinks, but stays half lit the rest of the time. I changed the code from using pin 13 to pin 4, and connected a multimeter to pin 4. The generated web page toggles pin 4 high and low as expected.
How come you did not change your mac address? I thought you have to change it according to your PC or laptop.
I want to make sure I'm doing this right. I have
-the USB cable connected to my laptop
-ethernet cable connected from the sheild to my laptop ethernet port.
additional info:
and I am using Wi-Fi on my laptop via router.
this is what i get when I ping my Ip:
C:\Users\Allan Lee>ping 192.168.0.106
Pinging 192.168.0.106 with 32 bytes of data:
Reply from 192.168.0.106: bytes=32 time<1ms TTL=128
Reply from 192.168.0.106: bytes=32 time<1ms TTL=128
Reply from 192.168.0.106: bytes=32 time<1ms TTL=128
Reply from 192.168.0.106: bytes=32 time<1ms TTL=128
Ping statistics for 192.168.0.106:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
How come you did not change your mac address? I thought you have to change it according to your PC or laptop.
Never heard of that. You may not want duplicate mac address in a network that does anything functional with the mac address.
I want to make sure I'm doing this right. I have
-the USB cable connected to my laptop
-ethernet cable connected from the sheild to my laptop ethernet port.
I connect my ethernet sheld to my wireless router, not my computer. Connecting to the ethernet port on a computer may generat extra issues that may need to be addressed.
Connecting directly to your computer is potentially a little tricky, in that you probably need a "crossover" ethernet cable. You would also need to manually configure an IP address for both the computer and shield.