no error checking; won't deal with multiple requesting clients; might even fail if you hit "refresh" in your browser too quickly...
but if you access the arduino IP address and port in your brower (i.e. type in something like 'http://192.168.0.15:10001') then you should get a web page that shows you a message, displays its analog-in values and changes background colour with the value of analog-in 0.
and if you are able to enable port forwarding from your router you can let your arduino serve data to the world....
hope it is useful....
usman
Code: Select all
#include <AFSoftSerial.h>
#include <string.h>
#define XPORT_CP2 8
#define XPORT_RXPIN 2
#define XPORT_TXPIN 3
#define HTTP_HEADER "HTTP/1.0 200 OK\nServer: arduino\nContent-Type: text/html\n\n"
#define TOTAL_ANALOG_IN 6
char linebuffer[256];
char c;
int analog[TOTAL_ANALOG_IN];
int requestNumber = 0;
AFSoftSerial mySerial = AFSoftSerial(XPORT_TXPIN, XPORT_RXPIN);
void setup() {
Serial.begin(57600);
pinMode(XPORT_CP2, OUTPUT);
Serial.println("serial port ready");
mySerial.begin(19200);
Serial.println("ethernet response ready");
digitalWrite(XPORT_CP2, HIGH);
}
void loop()
{
checkAnalogIns();
if (requested()){
respond();
}
}
uint8_t serialavail_timeout(int timeout) { // in ms
while (timeout) {
if (mySerial.available()) {
return 1;
}
timeout -= 1;
delay(1);
}
return 0;
}
uint8_t readline_timeout(int timeout) {
uint8_t idx=0;
char c;
while (serialavail_timeout(timeout)) {
c = mySerial.read();
//Serial.print(c);
linebuffer[idx++] = c;
if ((c == '\n') || (idx == 255)) {
linebuffer[idx] = 0;
return idx;
}
}
linebuffer[idx] = 0;
return idx;
}
int requested(void) {
uint8_t ret;
char *found=0, *start=0, *end=0;
while (1) {
ret = readline_timeout(200);
found = strstr(linebuffer, "GET /");
if (((int)found) != 0) {
return 1;
}
return 0;
}
}
void XPort_flush(int timeout) {
while (serialavail_timeout(timeout)) {
mySerial.read();
}
}
void checkAnalogIns(){
for (int i = 0; i < 6; i++){
analog[i] = analogRead(i);
}
}
void respond(){
XPort_flush(50);
mySerial.print(HTTP_HEADER);
mySerial.print("<html><body bgcolor=\"0000");
mySerial.print(analog[0]/4, HEX);
mySerial.print("\"><h1>hello this is your Arduino speaking...</h1>");
mySerial.print("<h3>My analog-in values are:</h3><ul>");
for (int i = 0; i < TOTAL_ANALOG_IN; i++){
mySerial.print("<li><b>");
mySerial.print(i);
mySerial.print(": </b> ");
mySerial.print(analog[i]);
mySerial.print("</li>");
}
mySerial.print("</ul></body></html>");
XPort_disconnect();
Serial.print("Requested! No. ");
Serial.println(requestNumber++);
}
void XPort_disconnect(){
digitalWrite(XPORT_CP2, LOW);
delay(20);
digitalWrite(XPORT_CP2, HIGH);
}
Code: Select all
Baudrate (19200) ?
I/F Mode (4C) ?
Flow (00) ?
Port No (10001) ?
ConnectMode (C0) ?
Send '+++' in Modem Mode (Y) ?
Show IP addr after 'RING' (Y) ?
Auto increment source port (N) ?
Remote IP Address : (000) .(000) .(000) .(000)
Remote Port (0) ?
DisConnMode (80) ?
FlushMode (00) ?
DisConnTime (00:00) ?:
SendChar 1 (00) ?
SendChar 2 (00) ?
(incidentally, if anyone gets confused by the XPort seemingly beginning all data-serving with the bytes "ff fb soh ff fb etx" -- this is because you might have "Telnet Com Port Cntrl" enabled -- it should be disabled).