2

Currently trying to implement a lightweight webserver using the WiFly shield and an Arduino UNO, this is the code I have so far:

#include <SPI.h> 
#include <WiFly.h>
WiFlyServer server(80);

/* Function defs */
void cmd(void)  { SpiSerial.print("$$$"); }
void dhcp(void) { SpiSerial.println("SET IP DHCP 0"); }
void pass(void) { SpiSerial.println("SET WLAN PHRASE 22222222"); }
void ssid(void) { SpiSerial.println("JOIN BTHOMEHUB"); }
void port(void) { SpiSerial.println("SET IP LOCAL 80"); }
void ip(void)   { SpiSerial.println("SET IP ADDRESS 192.168.1.123"); }
void tcp(void)  { SpiSerial.println("SET IP PROTOCOL 2"); }
void boot(void) { SpiSerial.println("REBOOT"); }

/* Pointer to a handler function */
typedef void (*Handler)(void);
Handler table[8] = { cmd, dhcp, pass, ssid, port, ip, tcp, boot };

void setup() {
  server.begin();
  Serial.begin(9600);
  SpiSerial.begin();

  for(short x = 0; x < 7; x++) {
    table[x]();
    delay(100);
  }
}

void loop() {
  WiFlyClient client = server.available();

  if(client){
    while(client.connected()) {
      if(client.available()) {
        client.println("<HTML><BODY><H1>Hi</H1><P>This is text.</P></BODY></HTML>");
      }
    }
    delay(100);
    client.flush();
    client.stop();
  }
}

This compiles well, but when hitting 192.168.1.123 in my web browser I get an ever-increasing output like:

*HELLO*<
<
<
<
<
<
<
<
<
<
<
< (cont.)

Definitely not the expected html. I have been playing with this code for hours now and this is the most positive output I can get. If I do a Serial.write(client.read()) within if(client.available()) { I am able to print all the request information to the serial monitor, it just seems I am perhaps sending the data in an incorrect format? Any suggestions? Cheers

Edit

I have updated the codebase to include the http headers,

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println("");
client.println("<!DOCTYPE html>");
client.println("<HTML><BODY><H1>Hi.....

now I get I different (similar) output, seems like it is printing the first character of each new line

*HELLO*H
C
C

<
<
H
C
C
4
  • A web server sends more than just HTML. It sends all the HTTP headers as well, which you seem to be missing. Commented Jul 29, 2016 at 23:23
  • @Majenko Added the http headers, check the edit Commented Jul 29, 2016 at 23:30
  • You still aren't outputting it right. There has to be a single blank line between the headers and the body to separate them. Commented Jul 29, 2016 at 23:31
  • @Majenko check now Commented Jul 29, 2016 at 23:33

1 Answer 1

1

Your browser sent a HTTP GET request. You need to send a HTTP response in kind, in accordance with the protocol, for your browser to correctly interpret the data. Something like this should do:

  #define TIMEOUT 100
  char response[] = "<HTML><BODY><H1>Hi</H1><P>This is text.</P></BODY></HTML>";
  unsigned long lastRead;
  ...

  while (client.connected()){
    if (client.available()){
      lastRead = millis();
      while (millis() - lastRead < TIMEOUT){
        while (client.available()){
          Serial.write(client.read());
          lastRead = millis();
        }
      }
      client.println("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: closed\r\n\r\n");
      client.print(response);
      break;
    }
  }
  delay(1000);
  client.stop();

EDIT:

It turns out that the Wifly shield sends a response status as well as "HELLO" by default. You have to disable that with a few commands:

set comm remote 0
set comm open 0
set comm close 0

This will ensure that "HELLO" isnt sent each time a client connection is made, though I'm not sure if this affects the default HTTP/0.9 200 OK response status that is always sent. You can try the sketch above with and without the response header to see what you get.

7
  • Hey, appreciate your answer -- I tried this but my browser just hangs Commented Jul 29, 2016 at 23:49
  • Weird, so with client.println("<!DOCTYPE html>"); I get a response, but again it looks like: *HELLO*H S C5 C < <H S C5 C Commented Jul 29, 2016 at 23:52
  • the serial monitor looks good: GET / HTTP/1.1 Host: 192.168.1.123 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 but the browser still hangs Commented Jul 30, 2016 at 0:00
  • With the timer it seems to hang regardless of the <!DOCTYPE html>, and yeah the browser renders the html fine :j Commented Jul 30, 2016 at 0:15
  • @WilliamPaul How about this? I changed the timeout and the print()s Commented Jul 30, 2016 at 0:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.