Hi
I am working on a simple project which try to control a TV via Internet.
I changed the sketch written by someone from Arduino team to suit my application as below.
==============xxx=========================================
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//for use with W5100 based ethernet shields
//Powering a servo from the arduino usually DOES NOT WORK.
//note that the below bug fix may be required
// Google Code Archive - Long-term storage for Google Code Project Hosting.
#include <SPI.h>
#include <Ethernet.h>
#include "sharpircode.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 72 }; // ip in lan
byte gateway[] = { 192, 168, 1, 254 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
//////////////////////
void setup(){
pinMode(8, OUTPUT); //IR LED
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//the pin for the servo co
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}
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("Home Automation");
client.println("");
client.println("");
client.println("
Home Automation
");client.println("
");
client.println("
");
client.println("<a href="/?TVon"">TV On");
client.println("<a href="/?TVoff"">TV Off
");
client.println("
");
client.println("<a href="/?Volup"">Vol Up");
client.println("<a href="/?Voldn"">Vol Dn
");
client.println("
");
client.println("");
client.println("");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
// control TV
if(readString.indexOf("?TVon") >0)//checks for on
{
SendSharpOn();
client.println("");
}
// control TV Volume
if(readString.indexOf("?Volup") >0)//checks for on
{
SendSharpVolUp();
client.println("");
}
if(readString.indexOf("?Voldn") >0)//checks for down
{
SendSharpVolDn(); // if this function is uploaded together with above two function, cannot open web page
client.println("");
}
//clearing string for next read
readString="";
}
}
}
}
}
void pulseIR(long microsecs) {
// we'll count down from the number of microseconds we are told to wait
cli(); // this turns off any background interrupts
while (microsecs > 0) {
// 38 kHz is about 13 microseconds high and 13 microseconds low
digitalWrite(8, HIGH); // this takes about 3 microseconds to happen
delayMicroseconds(10); // hang out for 10 microseconds
digitalWrite(8, LOW); // this also takes about 3 microseconds
delayMicroseconds(10); // hang out for 10 microseconds
// so 26 microseconds altogether
microsecs -= 26;
}
sei(); // this turns them back on
}
void SendSharpOn() {
// This is the code for Sharp TV, for others use the tutorial
// to 'grab' the proper code from the remote
for(int i=0; i<100 ; i++){
pulseIR(sharpon[i*2+0]);
delayMicroseconds(sharpon[i*2+1]);
}
}
void SendSharpVolUp() {
// This is the code for Sharp TV, for others use the tutorial
// to 'grab' the proper code from the remote
for(int i=0; i<100 ; i++){
pulseIR(sharpvolup[i*2+0]);
delayMicroseconds(sharpvolup[i*2+1]);
}
}
void SendSharpVolDn() {
for(int i=0; i<100 ; i++){
pulseIR(sharpvoldn[i*2+0]);
delayMicroseconds(sharpvoldn[i*2+1]);
}
}
============================ sharpircode.h =======================
int sharpon[]={
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 45320,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 43240,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 45740,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 0};
int sharpvolup[]={
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 46380,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 42180,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1720,
320, 720,
320, 46740,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 42580,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 46740,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 0};
int sharpvoldn[]={
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 45320,
320, 1720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 43220,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 45740,
320, 1740,
320, 720,
320, 720,
320, 720,
320, 720,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 720,
320, 1740,
320, 1740,
320, 1740,
320, 720,
320, 1740,
320, 0};
When I upload the sketch with SendSharpOn() and SendSharpVolUp(), I can open the web page and function are working.
However, when I upload all 3 function together, then I cannot open the web page.
Error message says that server did not response. Other web browser says that time out.
I try to fix the problems by using different IP address, it doesn't.
I think there is a bug in the program.
Will it be Ethernet library bug? Or SPI bug? Because there is a note at the top of the sketch but I have no idea how to do.
Anybody encounter such problem before and solution?
Thanks