Hello,
I'm trying to send HTTP request to an embedded server on a computer and get back the HTTP reply to simulate a small LAN using two Ethernet shields and two arduino UNOs. A computer terminal is connected to the arduino using ethernet, the web server is connected to the other arduino, the two arduinos are connected together serially using the the ALTSoftSerial library that utilizes pins 8 & 9 instead of traditional UART. My code works fine if the server reply is displayed on the serial monitor but I want to display the HTTP reply on default web browser like firefox. I have tried to use a combination of my code and the code of webserver example but with no decent results. I need some guidance concerning the combined code and I shall post the 2 codes below. (Note the same code is uploaded on both arduinos)
Code 1
#include <SPI.h>
#include <Ethernet.h>
#include <AltSoftSerial.h>
AltSoftSerial altSerial;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; //assigned arduino LAN IP address
byte server[] = { 192, 168, 1, 3 }; // laptop running apache LAN IP address
EthernetClient client; //apache web server running on port 80
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(4800);
altSerial.begin(4800);
Serial.println("starting simple arduino client test");
Serial.println();
delay(1000);
Serial.println("connecting...");
if (client.connect(server,80)) {
Serial.println("connected");
client.println("GET /mohaa.txt HTTP/1.0"); //php page invoking my web service
client.println();
} else {
Serial.println("connection failed");
}
}
void loop()
{
char c;
if (client.available()) {
c = client.read();
altSerial.print(c);
Serial.print(c);
}
if (altSerial.available()>0) {
c = altSerial.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================================");
Serial.println("");
client.stop();
for(;;);
}
}
Code 2
#include <SPI.h>
#include <Ethernet.h>
#include <AltSoftSerial.h>
AltSoftSerial altSerial;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 1, 102};
byte server[] = {192, 168, 1, 3 };
EthernetClient client;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
//EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(4800);
altSerial.begin(4800);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
//server.begin();
Serial.println("connecting...");
if (client.connect(server,80)) {
Serial.println("connected");
client.println("GET /mohaa.txt HTTP/1.0"); //php page invoking my web service
client.println();
} else {
Serial.println("connection failed");
}
}
void loop() {
// listen for incoming clients
//EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
altSerial.print(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("");
client.println("");
// output the value of each analog input pin
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}