Go Down

Topic: TinyWebServer modificata per arduino DUE (Read 552 times) previous topic - next topic

pablos

May 26, 2014, 11:38 pm Last Edit: May 27, 2014, 11:27 am by pablos Reason: 1
Per chi può interessare, allego zip file della libreria TinyWebServer modificata in diverse parti per adattarla alla board Arduino DUE, la migliore a mio parere che ho potuto provare e testare in questi 3 anni di ricerca, esperienza e studio su webserver-arduino (si può fare praticamente tutto).

Ad essa può essere:
- implementato l'autenticazione con cifratura base64 o MD5 (volendo anche riconoscimento del MAC o IP remoto)
- affiancato il protocollo websocket (in fase di test e upgrade)
- leggere qualsiasi file sulla SD da remoto
- Aggiungere/modificare/eliminare file sulla SD da remoto

Se qualcuno riscontra difetti e instabilità può serenamente scriverlo su questo treadh.

Saluti

Ps: fonte del progetto originale
2010 http://www.webweavertech.com/ovidiu/weblog/archives/000476.html
2013 https://github.com/ovidiucp/TinyWebServer
P.
no comment

leo72

Non ho scaricato, però grazie comunque per la condivisione  ;)


gcharles

Salve Pablos

ho scaricato la tua versione ma mi da un errore colpa del flash se faccio "verify and compile"


pablos

#4
Jul 19, 2014, 09:52 am Last Edit: Jul 19, 2014, 09:58 am by pablos Reason: 1
ciao, ci ri do un occhiata, però dovresti dirmi che errori ti da.
Forse ti riferisci agli esempi, la libreria è modificata, gli esempi non tutti, solo "Blink" funziona, negli altri 2 andrebbero tolte le F(, che sono 3-4 righe
no comment

gcharles

Ok Grazie

Il blink funziona ma il file upload no perche il Blink non fa uzo del Flash

Sorry for my poor Italian.

pablos

#6
Jul 19, 2014, 10:52 am Last Edit: Jul 21, 2014, 12:27 am by pablos Reason: 1
upload.ino modifyed for the DUE
Code: [Select]

see « Reply #10  »
no comment

gcharles


gcharles

Unfortunately although it compiles OK , than it is not running  see below the serial terminal output
******************************************************
Setting up SD card...
vol.init failed!
openRoot failedSetting up the Ethernet card...
Web server starting...
Ready to accept HTTP requests.
*****************************************************
Also it is saying that Ready to accept HTTP requests but eventually it is not connected to ethernet because if i key in the ip address cannot access from explorer

regards
Charles

pablos

#9
Jul 20, 2014, 10:56 pm Last Edit: Jul 20, 2014, 11:03 pm by pablos Reason: 1
I tried this sketch, it works for me.
Code: [Select]
Setting up SD card...
Setting up the Ethernet card...
Web server starting...
Ready to accept HTTP requests.


Arduino DUE
Ethernet shield + sd slot official V2
IDE 1.5.6-R2
MicroSD 8Gb format FAT 32

check if your SD is formatted correctly

bye
no comment

pablos

#10
Jul 21, 2014, 12:11 am Last Edit: Jul 21, 2014, 12:56 pm by pablos Reason: 1
ok sorry... I have another file.ino corrected version in the IDE, because CS pin causes problems with Arduino DUE, try this

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <TinyWebServer.h>

// Don't forget to modify the IP to an available one on your home network
byte ip[] = { 192, 168, 2, 177 };
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
boolean file_handler(TinyWebServer& web_server);
boolean index_handler(TinyWebServer& web_server);

TinyWebServer::PathHandler handlers[] = {
 {"/", TinyWebServer::GET, &index_handler },
 {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler },
 {"/" "*", TinyWebServer::GET, &file_handler },
 {NULL},
};

const char* headers[] = {"Content-Length",NULL};

TinyWebServer web = TinyWebServer(handlers, headers);

boolean has_filesystem = true;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void send_file_name(TinyWebServer& web_server, const char* filename) {
 if (!filename) {
   web_server.send_error_code(404);
   web_server.write("Could not parse URL");
 } else {
   TinyWebServer::MimeType mime_type = TinyWebServer::get_mime_type_from_filename(filename);
   web_server.send_error_code(200);
   web_server.send_content_type(mime_type);
   web_server.end_headers();
   if (file.open(&root, filename, O_READ)) {
     Serial.print("Read file "); Serial.println(filename);
     web_server.send_file(file);
     file.close();
   } else {
     web_server.write("Could not find file: ");
     web_server.write(filename);
     web_server.write("\n");
   }
 }
}

boolean file_handler(TinyWebServer& web_server) {
 char* filename = TinyWebServer::get_file_from_path(web_server.get_path());
 send_file_name(web_server, filename);
 free(filename);
 return true;
}

boolean index_handler(TinyWebServer& web_server) {
 send_file_name(web_server, "INDEX.HTM");
 return true;
}

void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) {
 static uint32_t start_time;
 static uint32_t total_size;

 switch (action) {
 case TinyWebPutHandler::START:
   start_time = millis();
   total_size = 0;
   if (!file.isOpen()) {
     // File is not opened, create it. First obtain the desired name
     // from the request path.
     char* fname = web_server.get_file_from_path(web_server.get_path());
     if (fname) {
Serial.print("Creating ");
       Serial.println(fname);
file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC);
free(fname);
     }
   }
   break;

 case TinyWebPutHandler::WRITE:
   if (file.isOpen()) {
     file.write(buffer, size);
     total_size += size;
   }
   break;

 case TinyWebPutHandler::END:
   file.sync();
   Serial.print("Wrote ");
   Serial.print (file.fileSize());
   Serial.print(" bytes in ");
   Serial.print(millis() - start_time);
   Serial.print(" millis (received ");
   Serial.print(total_size);
   Serial.println(" bytes)");
   file.close();
 }
}

void setup() {
  delay(1000);
  Serial.begin(9600);
  SPI.begin();
  pinMode(4,OUTPUT);
  digitalWrite(4,1);
  //pinMode(10,OUTPUT); //here cause problem at power reset initialize
  //digitalWrite(10,1);

 // initialize the SD card.
 Serial.print("Setting up SD card...\n");
 // pass over the speed and Chip select for the SD card
 if (!card.init(SPI_FULL_SPEED, 4)) {
   Serial.println("card failed");
   has_filesystem = false;
 }
 // initialize a FAT volume.
 if (!volume.init(&card)) {
   Serial.println("vol.init failed!");
   has_filesystem = false;
 }
 if (!root.openRoot(&volume)) {
   Serial.print("openRoot failed");
   has_filesystem = false;
 }

 if (has_filesystem) {
   // Assign our function to `upload_handler_fn'.
   TinyWebPutHandler::put_handler_fn = file_uploader_handler;
 }

 // Initialize the Ethernet.
 Serial.println("Setting up the Ethernet card...");
 Ethernet.begin(mac, ip);

//----------- debug connection LAN --------------------
 Serial.println();
 Serial.print("IP address Server: ");
 Serial.println(Ethernet.localIP());
 Serial.print("SubnetMask address: ");
 Serial.println(Ethernet.subnetMask());
 Serial.print("GatewayIP address: ");
 Serial.println(Ethernet.gatewayIP());
 Serial.print("DnsServer address: ");
 Serial.println(Ethernet.dnsServerIP());
 Serial.println();
//------------------------------------------------------

 // Start the web server.
 Serial.println("Web server starting...");
 web.begin();
  pinMode(10,OUTPUT);
 Serial.println("Ready to accept HTTP requests.");
}

void loop() {
 if (has_filesystem) {
   web.process();
 }
}


test load and upload
Code: [Select]
Setting up SD card...
Setting up the Ethernet card...

IP address Server: 192.168.2.177
SubnetMask address: 255.255.255.0
GatewayIP address: 192.168.2.1
DnsServer address: 192.168.2.1

Web server starting...
Ready to accept HTTP requests.

-----test read file from client---------------
Read file INDEX.HTM
Read file STYLE.CSS
Read file JQUERY.JS
Read file MAIN.JS
Read file LIGHTS.PNG
Read file FAVICON.ICO
Read file F.HTM
Read file SETUP.HTM
Read file SETUP.INI
Read file MENU.HTM
Read file TR-DATA.HTM
Read file FAVICON.ICO

------- test UPLOAD -------------
Creating DATALOG.TXT
Write 104 bytes in 110 millis (received 104 bytes)
Creating GRAZIA.TXT
Write 87 bytes in 100 millis (received 87 bytes)
Read file GRAZIA.TXT
Read file FAVICON.ICO
Read file DATALOG.TXT


Ps:Do not use  pinMode(10,OUTPUT); and digitalWrite(10,1);
no comment

gcharles

#11
Jul 21, 2014, 08:07 pm Last Edit: Jul 21, 2014, 10:01 pm by gcharles Reason: 1
Thanks Pablos

If I use your above code I can have the SD card running but only if I change the Speed of the SD from  (SPI_FULL_SPEED, 4) to (SPI_HALF _SPEED, 4) while the server never connects to my router , but at least the SD works.

In the next days I will try to figure out why I have to lower the speed with the same Eternet Shield V2 + 4Gb micro Sdhc card because if I use them with an Arduino mega with (SPI_FULL_SPEED, 4) is ok .

But yesterday I was reading the below tread and found out that it can be something related to the init of the Due microprocessor see link below .

http://forum.arduino.cc/index.php?topic=243778.msg1745661#msg1745661

pablos

#12
Jul 21, 2014, 11:07 pm Last Edit: Jul 21, 2014, 11:26 pm by pablos Reason: 1

http://forum.arduino.cc/index.php?topic=243778.msg1745661#msg1745661

ahhh ok, I read it very interesting .... thanks for the link!

I also found a problem with the library UDP and sockets W5100 for connection NTP servers, that I am solving.

bye
no comment

gcharles

Hello Pablos

I tried a lot in the last days to make my code run based on your last sketch you uploaded and for me now I have the fileupload working fine with the below code but what I found out is that if I try to open a file from SD with command  SD.open it does not work .

below I inserted my test sketch based on your last one you provided also I attached a screen shot of the serial monitor.

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <TinyWebServer.h>

File myFile;

// Don't forget to modify the IP to an available one on your home network
byte ip[] = { 192, 168, 1, 177 };
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
boolean file_handler(TinyWebServer& web_server);
boolean index_handler(TinyWebServer& web_server);

TinyWebServer::PathHandler handlers[] = {
  {"/", TinyWebServer::GET, &index_handler },
  {"/upload/" "*", TinyWebServer::PUT, &TinyWebPutHandler::put_handler },
  {"/" "*", TinyWebServer::GET, &file_handler },
  {NULL},
};

const char* headers[] = {"Content-Length",NULL};

TinyWebServer web = TinyWebServer(handlers, headers);

boolean has_filesystem = true;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void send_file_name(TinyWebServer& web_server, const char* filename) {
  if (!filename) {
    web_server.send_error_code(404);
    web_server.write("Could not parse URL");
  } else {
    TinyWebServer::MimeType mime_type = TinyWebServer::get_mime_type_from_filename(filename);
    web_server.send_error_code(200);
    web_server.send_content_type(mime_type);
    web_server.end_headers();
    if (file.open(&root, filename, O_READ)) {
      Serial.print("Read file "); Serial.println(filename);
      web_server.send_file(file);
      file.close();
    } else {
      web_server.write("Could not find file: ");
      web_server.write(filename);
      web_server.write("\n");
    }
  }
}

boolean file_handler(TinyWebServer& web_server) {
  char* filename = TinyWebServer::get_file_from_path(web_server.get_path());
  send_file_name(web_server, filename);
  free(filename);
  return true;
}

boolean index_handler(TinyWebServer& web_server) {
  send_file_name(web_server, "INDEX.HTM");
  return true;
}

void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) {
  static uint32_t start_time;
  static uint32_t total_size;

  switch (action) {
  case TinyWebPutHandler::START:
    start_time = millis();
    total_size = 0;
    if (!file.isOpen()) {
      // File is not opened, create it. First obtain the desired name
      // from the request path.
      char* fname = web_server.get_file_from_path(web_server.get_path());
      if (fname) {
   Serial.print("Creating ");
        Serial.println(fname);
   file.open(&root, fname, O_CREAT | O_WRITE | O_TRUNC);
   free(fname);
      }
    }
    break;

  case TinyWebPutHandler::WRITE:
    if (file.isOpen()) {
      file.write(buffer, size);
      total_size += size;
    }
    break;

  case TinyWebPutHandler::END:
    file.sync();
    Serial.print("Wrote ");
    Serial.print (file.fileSize());
    Serial.print(" bytes in ");
    Serial.print(millis() - start_time);
    Serial.print(" millis (received ");
    Serial.print(total_size);
    Serial.println(" bytes)");
    file.close();
  }
}

void setup() {
   delay(1000);
   Serial.begin(9600);
   //SPI.begin();
   //pinMode(4,OUTPUT);
   //digitalWrite(4,1);
   //pinMode(10,OUTPUT); //here cause problem at power reset initialize
   //digitalWrite(10,1);

  // initialize the SD card.
 


  Serial.print("Setting up SD card...\n");
  // pass over the speed and Chip select for the SD card
  if (!card.init(SPI_HALF_SPEED, 4)) {
    Serial.println("card failed");
    has_filesystem = false;
  }
  // initialize a FAT volume.
  if (!volume.init(&card)) {
    Serial.println("vol.init failed!");
    has_filesystem = false;
  }
  if (!root.openRoot(&volume)) {
    Serial.print("openRoot failed");
    has_filesystem = false;
  }

  if (has_filesystem) {
    // Assign our function to `upload_handler_fn'.
    TinyWebPutHandler::put_handler_fn = file_uploader_handler;
   

   
  }

  // Initialize the Ethernet.
  Serial.println("Setting up the Ethernet card...");


Ethernet.begin(mac, ip);
 
//----------- debug connection LAN --------------------
  Serial.println();
  Serial.print("IP address Server: ");
  Serial.println(Ethernet.localIP());
  Serial.print("SubnetMask address: ");
  Serial.println(Ethernet.subnetMask());
  Serial.print("GatewayIP address: ");
  Serial.println(Ethernet.gatewayIP());
  Serial.print("DnsServer address: ");
  Serial.println(Ethernet.dnsServerIP());
  Serial.println();
//------------------------------------------------------
  Serial.println("Web server starting...");
  web.begin();
  //pinMode(10,OUTPUT);
  Serial.println("Ready to accept HTTP requests.");
//pinMode(10,1);
}

void loop() {
  if (has_filesystem) {
    web.process();
  }
  printDirectory();
  delay(2000);
  Test_file();
delay(2000);
}

void printDirectory() {
  Serial.print("\nCard type: ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }


  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);


  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
}
void Test_file()
{
// Open the settings file for reading:
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  }

gcharles

i solved the issue by adding the below in the Setup

if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    ucg.setPrintPos(3, 50);
    ucg.print("SD Initialized");
   
  }



thanks

Go Up