Can an Arduino, with a single UART, use the ethernet shield and serial (via a max232 chip) at the same time? For instance, I need to do this:
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
#include <Udp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 220 }; // 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
int OnkyoPin = 13; // Onkyo pin
String readString = String(30); //string for fetching data from address
boolean OnkyoON = false; //Onkyo status flag
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 13 to output
pinMode(OnkyoPin, OUTPUT);
//enable Serial1 datada print
Serial.begin(9600);
Serial2.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() < 100)
{
//store characters to string
readString += c; //replaces readString.append(c);
}
//output chars to Serial1 port
Serial1.print(c);
//if HTTP request has ended
if (c == '\n') {
//dirty skip of "GET /favicon.ico HTTP/1.1"
if (readString.indexOf("?") <0)
{
//skip everything
}
else
//lets check if Onkyo should be on
if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))
{
//Onkyo has to be turned ON
digitalWrite(OnkyoPin, HIGH); // set the Onkyo on
OnkyoON = true;
}else{
//Onkyo has to be turned OFF
digitalWrite(OnkyoPin, LOW); // set the Onkyo OFF
OnkyoON = false;
}
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//controlling Onkyo via checkbox
client.println("<h1>Onkyo Control</h1>");
//address will look like http://192.168.1.110/?L=1 when submited
if (OnkyoON)
client.println("<form method=get name=Onkyo><input type=checkbox name=L value=0>Onkyo
<input type=submit value=submit></form>");
else
/* prints value as string in hexadecimal (base 16) */
client.println("<form method=get name=Onkyo><input type=checkbox name=L value=1>Onkyo
<input type=submit value=submit></form>");
client.println("
");
//printing Onkyo status
client.print("<font size='5'>Onkyo status: ");
if (OnkyoON)
{
client.println("<font color='green' size='5'>ON");
Serial2.write("!1PWR01") ;
Serial2.print(13,BYTE);
}
else
{
client.println("<font color='grey' size='5'>OFF");
Serial2.write("!1PWR00") ;
Serial2.print(13,BYTE);
}
client.println("</body></html>");
//clearing string for next read
readString="";
//stopping client
client.stop();
}
}
}
}
}
Do I need the Mega 2560 and serial2 or could I do it all on the same UART from something like an UNO?