HTML with CC3000 wifi shield

I have problem with html code used with a server that used CC3000 wifi shield
the code inside loop is:

void loop(void)
{
// Try to get a client which is connected.
Adafruit_CC3000_ClientRef client = httpServer.available();
if (client) {
Serial.println(F(“Client connected.”));
// Process this request until it completes or times out.
// Note that this is explicitly limited to handling one request at a time!

// Clear the incoming data buffer and point to the beginning of it.
bufindex = 0;
memset(&buffer, 0, sizeof(buffer));

// Clear action and path strings.
memset(&action, 0, sizeof(action));
memset(&path, 0, sizeof(path));

// Set a timeout for reading all the incoming data.
unsigned long endtime = millis() + TIMEOUT_MS;

// Read all the incoming data until it can be parsed or the timeout expires.
bool parsed = false;
while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) {
if (client.available()) {
buffer[bufindex++] = client.read();
}
parsed = parseRequest(buffer, bufindex, action, path);
}

// Handle the request if it was parsed.
if (parsed) {
Serial.println(F(“Processing request”));
Serial.print(F(“Action: “)); Serial.println(action);
Serial.print(F(“Path: “)); Serial.println(path);
// Check the action to see if it was a GET request.
if (strcmp(action, “GET”) == 0) {
// Respond with the path that was accessed.
// First send the success response code.
client.fastrprintln(F(“HTTP/1.1 200 OK”));
// Then send a few headers to identify the type of data returned and that
// the connection will not be held open.
client.fastrprintln(F(“Content-Type: text/plain”));
client.fastrprintln(F(“Connection: close”));
client.fastrprintln(F(“Server: Adafruit CC3000”));
// Send an empty line to signal start of body.
client.fastrprintln(F(””));
// Now send the response data.
client.fastrprintln(F(“Hello world!”));
client.fastrprint(F(“You accessed path: “)); client.fastrprintln(path);
//nuovo codice
// now output HTML data starting with standart header
// client.println(“HTTP/1.1 200 OK”);
// client.println(“Content-Type: text/html”);
// client.println();
// client.print(“ARDUINO Controllo LED via WEB”);
//send first heading
client.println(“

LED CONTROL by internet connection

”);
client.println(”
”);
client.println(“

LED control

”);
//printing LED status
client.print(“LED status: “);
if (LEDON) {
client.println(“ON”);
}
else
{
client.println(“OFF”);
}
client.print(“

ACCENDI | SPEGNI

”);
client.println(””);
readString=””;

//nuovo codice
}
else {
// Unsupported action, respond with an HTTP 405 method not allowed error.
client.fastrprintln(F(“HTTP/1.1 405 Method Not Allowed”));
client.fastrprintln(F(""));
}
}

// Wait a short period to make sure the response had time to send before
// the connection is closed (the CC3000 sends data asyncronously).
delay(100);

// Close the connection when done.
Serial.println(F(“Client disconnected”));
client.close();
}
}

// Return true if the buffer contains an HTTP request. Also returns the request
// path and action strings if the request was parsed. This does not attempt to
// parse any HTTP headers because there really isn’t enough memory to process
// them all.
// HTTP request looks like:
// [method] [path] [version] \r\n
// Header_key_1: Header_value_1 \r\n
// …
// Header_key_n: Header_value_n \r\n
// \r\n
bool parseRequest(uint8_t* buf, int bufSize, char* action, char* path) {
// Check if the request ends with \r\n to signal end of first line.
if (bufSize < 2)
return false;
if (buf[bufSize-2] == ‘\r’ && buf[bufSize-1] == ‘\n’) {
parseFirstLine((char*)buf, action, path);
return true;
}
return false;
}

// Parse the action and path from the first line of an HTTP request.
void parseFirstLine(char* line, char* action, char* path) {
// Parse first word up to whitespace as action.
char* lineaction = strtok(line, " ");
if (lineaction != NULL)
strncpy(action, lineaction, MAX_ACTION);
// Parse second word up to whitespace as path.
char* linepath = strtok(NULL, " ");
if (linepath != NULL)
strncpy(path, linepath, MAX_PATH);
}

// Tries to read the IP address and other connection details
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F(“Unable to retrieve the IP Address!\r\n”));
return false;
}
else
{
Serial.print(F("\nIP Addr: “)); cc3000.printIPdotsRev(ipAddress);
Serial.print(F(”\nNetmask: “)); cc3000.printIPdotsRev(netmask);
Serial.print(F(”\nGateway: “)); cc3000.printIPdotsRev(gateway);
Serial.print(F(”\nDHCPsrv: “)); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F(”\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}

I can’t see the code in browser

client.println(“

LED CONTROL by internet connection

”);
client.println("
");
client.println(“

LED control

”);
//printing LED status
client.print("LED status: ");
if (LEDON) {
client.println(“ON”);

Where is the error? I tried fastrprint with F or not.

Thanks for help!!

Where is the error?

There are a number of things that have to happen before you send that data. I'm going to guess that they didn't all happen.

In parseRequest(), print the data in buf, with an identifier and start and end markers:

   Serial.print(buf: [");
   Serial.print(buf);
   Serial.println("]");

Show us ALL of your code and ALL of your serial output and whatever you do see in the browser (you may need to right click and select something like View Page Source).

This is the entire code:

/***************************************************
Adafruit CC3000 Breakout/Shield Simple HTTP Server

****************************************************/
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include “utility/debug.h”
#include “utility/socket.h”

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID “x” // cannot be longer than 32 characters!
#define WLAN_PASS “x”

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2

#define LISTEN_PORT 83 // What TCP port to listen on for connections.
// The HTTP protocol uses port 80 by default.

#define MAX_ACTION 10 // Maximum length of the HTTP action that can be parsed.

#define MAX_PATH 64 // Maximum length of the HTTP request path that can be parsed.
// There isn’t much memory available so keep this short!

#define BUFFER_SIZE MAX_ACTION + MAX_PATH + 20 // Size of buffer for incoming request data.

#define TIMEOUT_MS 500

Adafruit_CC3000_Server httpServer(LISTEN_PORT);
uint8_t buffer[BUFFER_SIZE+1];
int bufindex = 0;
char action[MAX_ACTION+1];
char path[MAX_PATH+1];
int ledPin = 5; // LED pin
String readString; //string
boolean LEDON = false; //LED status flag
void setup(void)
{
Serial.begin(115200);
Serial.println(F(“Hello, CC3000!\n”));

Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);

// Initialise the module
Serial.println(F("\nInitializing…"));
if (!cc3000.begin())
{
Serial.println(F(“Couldn’t begin()! Check your wiring?”));
while(1);
}

Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F(“Failed!”));
while(1);
}

Serial.println(F(“Connected!”));

Serial.println(F(“Request DHCP”));
while (!cc3000.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}

// Display the IP address DNS, Gateway, etc.
while (! displayConnectionDetails()) {
delay(1000);
}

// Start listening for connections
httpServer.begin();

Serial.println(F(“Listening for connections…”));
}

void loop(void)
{
// Try to get a client which is connected.
Adafruit_CC3000_ClientRef client = httpServer.available();
if (client) {
Serial.println(F(“Client connected.”));
// Process this request until it completes or times out.
// Note that this is explicitly limited to handling one request at a time!

// Clear the incoming data buffer and point to the beginning of it.
bufindex = 0;
memset(&buffer, 0, sizeof(buffer));

// Clear action and path strings.
memset(&action, 0, sizeof(action));
memset(&path, 0, sizeof(path));

// Set a timeout for reading all the incoming data.
unsigned long endtime = millis() + TIMEOUT_MS;

// Read all the incoming data until it can be parsed or the timeout expires.
bool parsed = false;
while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) {
if (client.available()) {
buffer[bufindex++] = client.read();
}
parsed = parseRequest(buffer, bufindex, action, path);
}

// Handle the request if it was parsed.
if (parsed) {
Serial.println(F(“Processing request”));
Serial.print(F(“Action: “)); Serial.println(action);
Serial.print(F(“Path: “)); Serial.println(path);
// Check the action to see if it was a GET request.
if (strcmp(action, “GET”) == 0) {
// Respond with the path that was accessed.
// First send the success response code.
client.fastrprintln(F(“HTTP/1.1 200 OK”));
// Then send a few headers to identify the type of data returned and that
// the connection will not be held open.
client.fastrprintln(F(“Content-Type: text/plain”));
client.fastrprintln(F(“Connection: close”));
client.fastrprintln(F(“Server: Adafruit CC3000”));
// Send an empty line to signal start of body.
client.fastrprintln(F(””));
// Now send the response data.
client.fastrprintln(F(“Hello world!”));
client.fastrprint(F(“You accessed path: “)); client.fastrprintln(path);
//nuovo codice
// now output HTML data starting with standart header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
client.print(“ARDUINO Controllo LED via WEB”);
//send first heading
client.println(“

LED CONTROL by internet connection

”);
client.println(”
”);
client.println(“

LED control

”);
//printing LED status
client.print(“LED status: “);
if (LEDON) {
client.println(“ON”);
}
else
{
client.println(“OFF”);
}
client.print(“

ACCENDI | SPEGNI

”);
client.println(””);
readString=””;

}
else {
// Unsupported action, respond with an HTTP 405 method not allowed error.
client.fastrprintln(F(“HTTP/1.1 405 Method Not Allowed”));
client.fastrprintln(F(""));
}
}

// Wait a short period

delay(100);

// Close the connection when done.
Serial.println(F(“Client disconnected”));
client.close();
}
}

// Return true if the buffer contains an HTTP request. Also returns the request
// path and action strings if the request was parsed. This does not attempt to
// parse any HTTP headers because there really isn’t enough memory to process
// them all.
// HTTP request looks like:
// [method] [path] [version] \r\n
// Header_key_1: Header_value_1 \r\n
// …
// Header_key_n: Header_value_n \r\n
// \r\n
bool parseRequest(uint8_t* buf, int bufSize, char* action, char* path) {
// Check if the request ends with \r\n to signal end of first line.
if (bufSize < 2)
return false;
if (buf[bufSize-2] == ‘\r’ && buf[bufSize-1] == ‘\n’) {
parseFirstLine((char*)buf, action, path);
return true;
}
return false;
}

// Parse the action and path from the first line of an HTTP request.
void parseFirstLine(char* line, char* action, char* path) {
// Parse first word up to whitespace as action.
char* lineaction = strtok(line, " ");
if (lineaction != NULL)
strncpy(action, lineaction, MAX_ACTION);
// Parse second word up to whitespace as path.
char* linepath = strtok(NULL, " ");
if (linepath != NULL)
strncpy(path, linepath, MAX_PATH);
}

// Tries to read the IP address and other connection details
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F(“Unable to retrieve the IP Address!\r\n”));
return false;
}
else
{
Serial.print(F("\nIP Addr: “)); cc3000.printIPdotsRev(ipAddress);
Serial.print(F(”\nNetmask: “)); cc3000.printIPdotsRev(netmask);
Serial.print(F(”\nGateway: “)); cc3000.printIPdotsRev(gateway);
Serial.print(F(”\nDHCPsrv: “)); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F(”\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}

the problem is this part of code:
//send first heading
client.println(“

LED CONTROL by internet connection

”);
client.println("
");
client.println(“

LED control

”);
//printing LED status
client.print(“LED status: “);
if (LEDON) {
client.println(“ON”);
}
else
{
client.println(“OFF”);
}
client.print(“

ACCENDI | SPEGNI

”);
client.println(””);
readString="";

I see on screen

LED CONTROL by internet connection


LED control

etc..

I would like to do a control for light on/off simply.
I used web server example from adafruit (Hello word code) and an example from Ethernet shield with the code after // nuovo codice comment.

Thanks a lot

This is the entire code:

Please read #7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

Sorry :confused:

/***************************************************
Adafruit CC3000 Breakout/Shield Simple HTTP Server
 
     
****************************************************/
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"

// These are the interrupt and control pins
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                      SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "x"        // cannot be longer than 32 characters!
#define WLAN_PASS       "x"

// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

#define LISTEN_PORT           83      // What TCP port to listen on for connections.  
                                   // The HTTP protocol uses port 80 by default.

#define MAX_ACTION            10      // Maximum length of the HTTP action that can be parsed.

#define MAX_PATH              64      // Maximum length of the HTTP request path that can be parsed.
                                   // There isn't much memory available so keep this short!

#define BUFFER_SIZE           MAX_ACTION + MAX_PATH + 20  // Size of buffer for incoming request data.

#define TIMEOUT_MS            500   

Adafruit_CC3000_Server httpServer(LISTEN_PORT);
uint8_t buffer[BUFFER_SIZE+1];
int bufindex = 0;
char action[MAX_ACTION+1];
char path[MAX_PATH+1];
int ledPin = 5; // LED pin
String readString; //string
boolean LEDON = false; //LED status flag
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n")); 

Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);

// Initialise the module
Serial.println(F("\nInitializing..."));
if (!cc3000.begin())
{
 Serial.println(F("Couldn't begin()! Check your wiring?"));
 while(1);
}

Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
 Serial.println(F("Failed!"));
 while(1);
}

Serial.println(F("Connected!"));

Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
 delay(100); // ToDo: Insert a DHCP timeout!
}  

// Display the IP address DNS, Gateway, etc.
while (! displayConnectionDetails()) {
 delay(1000);
}

// Start listening for connections
httpServer.begin();

Serial.println(F("Listening for connections..."));
}

void loop(void)
{
// Try to get a client which is connected.
Adafruit_CC3000_ClientRef client = httpServer.available();
if (client) {
 Serial.println(F("Client connected."));
 // Process this request until it completes or times out.
 // Note that this is explicitly limited to handling one request at a time!

 // Clear the incoming data buffer and point to the beginning of it.
 bufindex = 0;
 memset(&buffer, 0, sizeof(buffer));
 
 // Clear action and path strings.
 memset(&action, 0, sizeof(action));
 memset(&path,   0, sizeof(path));

 // Set a timeout for reading all the incoming data.
 unsigned long endtime = millis() + TIMEOUT_MS;
 
 // Read all the incoming data until it can be parsed or the timeout expires.
 bool parsed = false;
 while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) {
   if (client.available()) {
     buffer[bufindex++] = client.read();
   }
   parsed = parseRequest(buffer, bufindex, action, path);
 }

 // Handle the request if it was parsed.
 if (parsed) {
   Serial.println(F("Processing request"));
   Serial.print(F("Action: ")); Serial.println(action);
   Serial.print(F("Path: ")); Serial.println(path);
   // Check the action to see if it was a GET request.
   if (strcmp(action, "GET") == 0) {
     // Respond with the path that was accessed.
     // First send the success response code.
     client.fastrprintln(F("HTTP/1.1 200 OK"));
     // Then send a few headers to identify the type of data returned and that
     // the connection will not be held open.
     client.fastrprintln(F("Content-Type: text/plain"));
     client.fastrprintln(F("Connection: close"));
     client.fastrprintln(F("Server: Adafruit CC3000"));
     // Send an empty line to signal start of body.
     client.fastrprintln(F(""));
     // Now send the response data.
     client.fastrprintln(F("Hello world!"));
     client.fastrprint(F("You accessed path: ")); client.fastrprintln(path);
//nuovo codice
// now output HTML data starting with standart header
  client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
     client.println();
      client.print("<html><head><title>ARDUINO Controllo LED via WEB</title><meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' ></head><body>");
     //send first heading
     client.println("<h1>LED CONTROL by internet connection</h1>");
     client.println("<hr />");
     client.println("<h1>LED control</h1>");
     //printing LED status
     client.print("<span>LED status: </span>");
       if (LEDON) {
        client.println("<span style='color:green'>ON</span>");
         }
         else
         {
        client.println("<span style='color:grey'>OFF</span>");
       }
     client.print("<h2><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></h2>");
     client.println("</body></html>");
     readString="";





   }
   else {
     // Unsupported action, respond with an HTTP 405 method not allowed error.
     client.fastrprintln(F("HTTP/1.1 405 Method Not Allowed"));
     client.fastrprintln(F(""));
   }
 }

 // Wait a short period

 delay(100);

 // Close the connection when done.
 Serial.println(F("Client disconnected"));
 client.close();
}
}

// Return true if the buffer contains an HTTP request.  Also returns the request
// path and action strings if the request was parsed.  This does not attempt to
// parse any HTTP headers because there really isn't enough memory to process
// them all.
// HTTP request looks like:
//  [method] [path] [version] \r\n
//  Header_key_1: Header_value_1 \r\n
//  ...
//  Header_key_n: Header_value_n \r\n
//  \r\n
bool parseRequest(uint8_t* buf, int bufSize, char* action, char* path) {
// Check if the request ends with \r\n to signal end of first line.
if (bufSize < 2)
 return false;
if (buf[bufSize-2] == '\r' && buf[bufSize-1] == '\n') {
 parseFirstLine((char*)buf, action, path);
 return true;
}
return false;
}

// Parse the action and path from the first line of an HTTP request.
void parseFirstLine(char* line, char* action, char* path) {
// Parse first word up to whitespace as action.
char* lineaction = strtok(line, " ");
if (lineaction != NULL)
 strncpy(action, lineaction, MAX_ACTION);
// Parse second word up to whitespace as path.
char* linepath = strtok(NULL, " ");
if (linepath != NULL)
 strncpy(path, linepath, MAX_PATH);
}

// Tries to read the IP address and other connection details
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;

if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
 Serial.println(F("Unable to retrieve the IP Address!\r\n"));
 return false;
}
else
{
 Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
 Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
 Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
 Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
 Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
 Serial.println();
 return true;
}
}

the problem is this part of code:
//send first heading
     client.println("<h1>LED CONTROL by internet connection</h1>");
     client.println("<hr />");
     client.println("<h1>LED control</h1>");
     //printing LED status
     client.print("<span>LED status: </span>");
       if (LEDON) {
        client.println("<span style='color:green'>ON</span>");
         }
         else
         {
        client.println("<span style='color:grey'>OFF</span>");
       }
     client.print("<h2><a href='/?L=1'>ACCENDI</a> | <a href='/?L=0'>SPEGNI</a></h2>");
     client.println("</body></html>");
     readString="";

I see on screen
<h1>LED CONTROL by internet connection</h1>
<hr />
<h1>LED control</h1>
etc..

That code does something. You need to describe what it actually does.

You want that code to do something. Presumably, what you want it to do differs from what it actually does. You need to describe the difference(s).

Actually the code write on browser:

h1>LED CONTROL by internet connection


LED control

LED status: etc ...

I don’t understand why I can’t see the right interface for switch on (accendi) or off (spegni) a LED on port number 5.

Why are you sending TWO HTTP/1.1 200 OK messages? That entire first block of html response data is hitting the bit bucket. You might as well stop sending it.

I don't understand why I can't see the right interface for switch on (accendi) or off (spegni) a LED on port number 5.

And I don't know what etc. means as far as what the browser shows. Show a screen shot of what is ACTUALLY displayed.

In attachement there is the print screen that I see :frowning:

Screenshot.jpg