Can you clarify if it should print
" Hello world" or
Hello World
It will send "Hello world" to the client. It is up to the client to render that properly.
And what do you mean with "The rest is upto the other end"
That's rather obvious, isn't it? You can't send bold text. You send text and some meta information that says that the text should be bold. It is up to the client end to use the text and meta information to render bold text.
Depending on where the text is in the file, different actions happen. Stuff in the body is rendered. You have no body.
In addition, if it's web browser that is the client, there is another layer of HTTP headers before that. One of the headers typically says that the text is actually html, and should be rendered as such by the web browser. Without any headers (none are being sent in that code) most browsers will assume that it is a plain text file, and render it as such, which is why you are seeing the HTML tags in the output.
If you are not using a web browser, but some custom client software, then it will be up to that software to recognize the tags and render the text properly.
Robin2:
You can run the Python Bottle web framework on the Linux side of a Yun. It makes life sooooooo much easier.
I will second that - it is MUCH more powerful than trying to write your own server in the limited resources of the AVR processor, and it's much faster because it doesn't need to go through the overhead of being passed between two processors via a serial port. It's a little bit of a learning curve to get it working, but it's not that bad, and it's easier than trying to write your own server protocols.
Use something like Bottle on the Linux side to do the web services, and only use the sketch and AVR processor to handle the low level I/O you need through the shield pins (since only the AVR processor has access to the shield pins.) You're tying your hands behind your back trying to do it all in a sketch.
PaulS:
It will send "Hello world" to the client. It is up to the client to render that properly.
That's rather obvious, isn't it? You can't send bold text. You send text and some meta information that says that the text should be bold. It is up to the client end to use the text and meta information to render bold text.
Thanks Paul
Here is my code.
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
const int Boton = 3; // the number of the pushbutton pin
const int LED = 2; // the number of the led pin
void setup() {
Bridge.begin();
pinMode(Boton, INPUT); //Boton
pinMode(LED, OUTPUT); //Led
server.listenOnLocalhost();
server.begin();
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
void process(YunClient client){
client.println("<html>");
client.println("<title>The title goes here</title>");
client.println("<body>");
client.println("<h1>Hello from Arduino!</h1>");
client.println("<p>A web page from the Arduino server</p>");
client.println("</body>");
client.println("</html>");
}
ShapeShifter:
In addition, if it's web browser that is the client, there is another layer of HTTP headers before that. One of the headers typically says that the text is actually html, and should be rendered as such by the web browser. Without any headers (none are being sent in that code) most browsers will assume that it is a plain text file, and render it as such, which is why you are seeing the HTML tags in the output.
This renders properly on a web browser, because it contains the header that tells the browser that the page content is html and not plain text:
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
const int Boton = 3; // the number of the pushbutton pin
const int LED = 2; // the number of the led pin
void setup() {
Bridge.begin();
pinMode(Boton, INPUT); //Boton
pinMode(LED, OUTPUT); //Led
server.listenOnLocalhost();
server.begin();
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
}
void process(YunClient client){
// Send out some minimal HTTP response headers to indicate html content
client.println("Status: 200 OK");
client.println("Content-Type: text/html; charset=utf-8");
// Send out the blank line that signals the end of the headers and start of content
client.println();
// Send out the content of the page.
client.println("<html>");
client.println("<title>The title goes here</title>");
client.println("<body>");
client.println("<h1>Hello from Arduino!</h1>");
client.println("<p>A web page from the Arduino server</p>");
client.println("</body>");
client.println("</html>");
}
This is essentially your code, but note that I added just the status and content type headers at the beginning of process(), the bare minimum to get it to work. There are lots more headers that are potentially useful for your application - if you are going to write a web server from scratch (which is basically what you are doing in the sketch) then it would be very helpful for you to study the HTTP protocol and do all of the things that a proper web server needs to do.
OR, don't try to re-invent a web server in the limited resource space of the AVR processor - let the Linux side do most of the work. There are lots of ways to do it, and the Bottle framework that Robin suggested is a good way to start - it will be MUCH less work, and will have MUCH more capabilities.
Also note that I added an extra closing brace to the end of loop() that appeared to be missing from your code - it wouldn't compile the way it was.
While that code does work, and is OK for a very simple one-off application, if you are going to add to it and make it do more than that, I still strongly suggest you look into the alternatives, like the Bottle framework that Robin2 suggested. If you keep expanding your sketch to handle more cases, and provide more information, you will very quickly reach the memory and performance limits of the AVR microprocessor. While there is a leaning curve, using a proper web framework on the Linux side of the Yun will be much less painful in the long run.
While that code does work, and is OK for a very simple one-off application, if you are going to add to it and make it do more than that, I still strongly suggest you look into the alternatives, like the Bottle framework that Robin2 suggested. If you keep expanding your sketch to handle more cases, and provide more information, you will very quickly reach the memory and performance limits of the AVR microprocessor. While there is a leaning curve, using a proper web framework on the Linux side of the Yun will be much less painful in the long run.
Thanks. I started reading about "Python Bottle web framework". I'm still new to arduino and really don't know how to run things on the linux side, but I will learn.
In this specific case, I need a very simple use of the web server.