Go Down

Topic: loading html page from SD card instead of arduino main code ? (Read 1 time) previous topic - next topic

Ivo_Creamo_Durec

May 29, 2019, 10:10 am Last Edit: May 29, 2019, 10:12 am by Ivo_Creamo_Durec
hello, i have a primitive code to just make a server from arduino.

Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,243,253);
IPAddress dns(192,168,240,55);
IPAddress subnet(255,255,252,0);
IPAddress gateway(192,168,240,2);

EthernetServer server(80);

void setup()
{
    Ethernet.begin(mac, ip, dns, gateway, subnet);
    server.begin();
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino Web Page</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>CAUKOOO</h1>");
                    client.println("<p>bavis sa ?</p>");
                    client.println("</body>");
                    client.println("</html>");
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}


my question is, can i somehow make two partitions on SD card 2 GB and one partition using on dynamic storing data and the other to just be constant and load from it html page, and maybe some php ...

could this be possible ? i know that code must be compiled and not just put on SD card, but i mean it like this :

Code: [Select]


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

IPAddress ip(192,168,243,253);

//i don't write code to enable sd card and that stuff ...

/*this string will be stored in sd card*/
String loadFromSD[3] = "HTTP/1.1 200 OK","Content-Type: text/html","Connection: close";

/*when client will type static ip address of arduino to the web browser, it will wait for client */

if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read(); // read 1 byte (character) from client

                // last line of client request is blank and ends with \n
                // respond to client only after last line received

                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println(loadFromSD[0]);
                    client.println(loadFromSD[1]);
                    client.println(loadFromSD[2]);

                    ...more code for website...
                    }



could this be possible ?

Juraj

hello, i have a primitive code to just make a server from arduino.

Code: [Select]

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,243,253);
IPAddress dns(192,168,240,55);
IPAddress subnet(255,255,252,0);
IPAddress gateway(192,168,240,2);

EthernetServer server(80);

void setup()
{
    Ethernet.begin(mac, ip, dns, gateway, subnet);
    server.begin();
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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");
                    client.println();
                    // send web page
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Arduino Web Page</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1>CAUKOOO</h1>");
                    client.println("<p>bavis sa ?</p>");
                    client.println("</body>");
                    client.println("</html>");
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}


my question is, can i somehow make two partitions on SD card 2 GB and one partition using on dynamic storing data and the other to just be constant and load from it html page, and maybe some php ...

could this be possible ? i know that code must be compiled and not just put on SD card, but i mean it like this :

Code: [Select]


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

IPAddress ip(192,168,243,253);

//i don't write code to enable sd card and that stuff ...

/*this string will be stored in sd card*/
String loadFromSD[3] = "HTTP/1.1 200 OK","Content-Type: text/html","Connection: close";

/*when client will type static ip address of arduino to the web browser, it will wait for client */

if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                char c = client.read(); // read 1 byte (character) from client

                // last line of client request is blank and ends with \n
                // respond to client only after last line received

                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println(loadFromSD[0]);
                    client.println(loadFromSD[1]);
                    client.println(loadFromSD[2]);

                    ...more code for website...
                    }



could this be possible ?

you can make folders and serve complete files (and serve data with separate aJAX requests)

part of my main Arduino project
https://github.com/jandrassy/Regulator/blob/master/Regulator/WebServer.ino

You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Ivo_Creamo_Durec

and i will just paste it in main code and then upload it to arduino ? or what and where should i store it ?
and which method of yours is having or doing that aJAX ?

Juraj

and i will just paste it in main code and then upload it to arduino ? or what and where should i store it ?
and which method of yours is having or doing that aJAX ?
no, you must know what you are doing. understand the code.

the server side of the AJAX request are the REST calls to WebServer

AJAX requests are made from browser over XmlHttpRequest in Java Script
https://github.com/jandrassy/Regulator/blob/master/Regulator/data/script.js
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Ivo_Creamo_Durec

ok but please tell me how is that request send ? i don't understand the logic behind it ...
can be php request be written directly in arduino main code like a function ?

<?php

... code for request or sending the data to server

?>

or it should be stored in an sql server and just called by arduino with http request to run this query ?

query will send packet and arduino will just fill it with requested data in order and then send it back to server, which will then process the data obtained and record them in the database table ?

could this kind of "solution" be possible ? or am i wrong ?

Juraj

ok but please tell me how is that request send ? i don't understand the logic behind it ...
can be php request be written directly in arduino main code like a function ?

<?php

... code for request or sending the data to server

?>

or it should be stored in an sql server and just called by arduino with http request to run this query ?

query will send packet and arduino will just fill it with requested data in order and then send it back to server, which will then process the data obtained and record them in the database table ?

could this kind of "solution" be possible ? or am i wrong ?
my Arduino web server has two functions. serve static files from the SD card (html, js, css, png, cs, log).
and second function is to handle AJAX request returning data in JSON format. you can see this in the source code WebServer.ino

the web part is coded in javascript and runs in the browser. if I enter in browser the URL of the Arduino, the Arduino returns index.html which includes script.js. the script starts and builds the web page content with data retrieved with AJAX call
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Ivo_Creamo_Durec

so the ajax is in .js file which is loaded by index.html ?

also i found a useful link about ajax topic and loading from SD card as well :)
it helped me so i put the link here if someone else reading this post will want to learn more :)

https://pimylifeup.com/arduino-web-server/

Json_Smith

I think you can do it as its done on the website of which link you have posted ... at the end is loading .htm file from sd card and above that reading is ajax request code.

As i suggest, you want to send data from arduino to server, no send data from server to arduino, right ?

Ivo_Creamo_Durec

#8
Jun 03, 2019, 08:20 am Last Edit: Jun 03, 2019, 08:22 am by Ivo_Creamo_Durec
@Juraj thank you for an idea about ajax request :) i learned how it works and its pretty easy but powerful ... thanks again

@Json_Smith yes, you are right ... i downloaded this file from github :

https://github.com/pimylifeup/Arduino-Web-Server

i loaded the code to arduino, loaded .htm file in sd card and tested it... it worked so i started learning on it and i understand it now :) thanks ...

Go Up