User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Embedded Web Servers

Thu Mar 09, 2017 1:51 am

What is the difference between a web server (like Apache) and an "embedded" web server?

In my mind it has to do with footprint. If its small enough to fit on let say a PI, then its embedded. I might also mention that it has something to do with "embed-ability" in that it can be tightly coupled with your code in ways that a formal httpd can't.

My embedded httpd of choice is mongoose-meerkat. I've had a chance to go in and look at its code and it is a comfortable, learning embedded web server that has a bunch of nice examples even an intermediate can draw from. If you need basic connectivity on the web from python directly, or from a process (like my DAEMON1 framework), then MM might be right for you.

Download the source from: https://www.cesanta.com/

Now what I'm talking about here is the simplest way to build this DAMN thing, ok? I don't want comments from the peanut gallery on how this isn't done, as I've done it before and this is the simplest way to do it.

How MM is set up is it has its embeddedness in a *.h and a *.c file (very clever way to do it I think...though according to some, unorthodox) and you set it up and call it from a *.c file. I assume that the actual *.c file you call it from is the process you want to embed the web server in.

Here is an example of how you embed it. Its not embedded into anything, just itself, so it results in a free-standing web server, or httpd. You just change the variables below to what you need (ie. its port, and its htdocs directory filespec). Very cool.

Code: Select all

// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

#include "mongoose.h"
#include "mongoose.c"

static const char *s_http_port = "8080";
static struct mg_serve_http_opts s_http_server_opts;

static void ev_handler(struct mg_connection *nc, int ev, void *p) {
  if (ev == MG_EV_HTTP_REQUEST) {
    mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
  }
}

int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

 daemon(1,0); //turn into a system daemon

  mg_mgr_init(&mgr, NULL);
  nc = mg_bind(&mgr, s_http_port, ev_handler);

  // Set up HTTP server parameters
  mg_set_protocol_http_websocket(nc);
  s_http_server_opts.document_root = "./htdocs/";      // Serve current directory
  s_http_server_opts.dav_document_root = ".";  // Allow access via WebDav
  s_http_server_opts.enable_directory_listing = "yes";
  s_http_server_opts.cgi_file_pattern = "*.cgi|*.exe";
 
  printf("Starting web server on port %s\n", s_http_port);
  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}

OK, this is how its done. Copy over mongoose.h and mongoose.c to the same directory as the previous code (cut and paste it into a file called "httpd.c") then compile it manually thus:

gcc httpd.c -o httpd

and invoke:

./httpd

Its set up for port 8080 so go in on LYNX (if you are tethered and telnetting....sorry SSH'ing...in) or via browser on port 8080 like this: http://127.0.0.1:8080 which will serve what is on your ./htdocs/ directory (index.html set for default).

Another way to build it is by modifying simplest_web_server.c from its examples section and manually building such. (Which is how I did this build process).

NOW YOU HAVE AN EMBEDDED WEB SERVER!

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Thu Mar 09, 2017 1:56 am

Where is my htdocs directory. You gotta make one...its in the same directory as your httpd, m****. If it ain't, there mkdir it like this:

mkdir htdocs

then you put in a placeholder index.html file. Just pico it like this <html><h2>WEB SERVER RUNNING!!</h2></html> and <ctrl>X out.

You have your web server running. Check it with lynx or browser (like chrome or something). Remember example code is on port 8080. "Can I put it where its suppose to be?" YEESSS! Just change the port to 80. lol.
Last edited by woodystanford on Thu Mar 09, 2017 4:37 am, edited 1 time in total.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Thu Mar 09, 2017 2:17 am

This might work...

Download mongoose.h and mongoose.c directly from from Github:

https://github.com/cesanta/mongoose/

It MIGHT work. And then hang onto the code.

Someone get with me if this works, the downloading from github part I mean.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Thu Mar 09, 2017 3:09 am

OK, I got this working good on my Beagleboard, so check out:

http://beagleboard.org/discuss?place=ms ... vIva5kAwAJ

For instructions: the build instructions are GOOD for R-Pi though (the technique). I just don't have a Pi to verify it with.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Thu Mar 09, 2017 4:57 am

Strapping components together IoT style.

Image

User avatar
rpdom
Posts: 14766
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Embedded Web Servers

Thu Mar 09, 2017 6:07 am

You keep mentioning the Beagleboard in all your posts. Have you tried any of this stuff on a Pi? Have you even got a Pi?

(Throws peanuts from the gallery)

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Thu Mar 09, 2017 3:00 pm

rpdom wrote:You keep mentioning the Beagleboard in all your posts. Have you tried any of this stuff on a Pi? Have you even got a Pi?

(Throws peanuts from the gallery)
Actually I don't...still saving my pennies for one. Aren't they the same? *innocent look*

But seriously, I figure they are both POSIX-compliant so if I stick with UN*X I should be good. I only talk about stuff that's common to both.

scotty101
Posts: 3649
Joined: Fri Jun 08, 2012 6:03 pm

Re: Embedded Web Servers

Thu Mar 09, 2017 3:28 pm

woodystanford wrote: Actually I don't...still saving my pennies for one. Aren't they the same? *innocent look*
A pi is much cheaper than a beaglebone.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

LdB
Posts: 1177
Joined: Wed Dec 07, 2016 2:29 pm

Re: Embedded Web Servers

Thu Mar 09, 2017 5:20 pm

Ok so you know an embedded web server will be a very cutdown version of a proper webserver.

They usually only support a limited set of HTML and other languages and usually only allow limited connections. The reason for all that is the answer for any embedded .... LACK OF ACTUAL MEMORY.

The Beagle and PI are really strange for embedded because they do have relatively a lot of memory and to be honest you are being very loose with the term embedded to encompass them. Those who do actual microcontroller programming would not really consider a Pi or BeagleBoard embedded. They would argue if these really were embedded you would have to run uClinux not actual Linux on them.

If you want to see small google "iPic" which is ridiculously small and the memory and code are given to you in bytes, most implementations target 256 bytes of RAM and 512 bytes of ROM.

jim2007
Posts: 9
Joined: Tue Jan 17, 2017 1:46 am
Location: Switzerland
Contact: Website

Re: Embedded Web Servers

Fri Mar 10, 2017 3:10 am

OP what you are describing is NOT an embedded web server !

My services on RPi are deployed as a single binary executable plus a few libraries. You start it up and point your browser at it and it renders a HTML interface. There are no folders, no HTML pages and no configuration, unless you want it to run on a different port, which can be done via a command line parameter. That is what is expected of an embedded server.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Tue Mar 14, 2017 10:37 pm

jim2007 wrote:OP what you are describing is NOT an embedded web server !

My services on RPi are deployed as a single binary executable plus a few libraries. You start it up and point your browser at it and it renders a HTML interface. There are no folders, no HTML pages and no configuration, unless you want it to run on a different port, which can be done via a command line parameter. That is what is expected of an embedded server.
Look you have an issue with Mongoose-Meerkat being an embedded web server, you take it up with them. *rolls eyes*

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Tue Mar 14, 2017 10:57 pm

OK, here is some python code for writing a web page to the HTDOC directory of the MM web server.

Code: Select all

import sys
import time

##################################
#
# Web Server Test Code for Python
#
# All the program does is write a file
# to a HTDOCS directory in html format.
#
# Compile and run the MM HTTPD
# (Mongoose-Meerkat) example. Remember
# it just returns back to prompt, but
# its running in the background. Also
# you have to make you own htdoc directory
# for it.
#
# use ps to find pid and kill with kill
##################################

print("Web Server Interface for Mongoose-Meerkat\n")

try:
  # open file stream
  file = open("./htdocs/python1.html", "w")
except IOError:
  print "There was an error writing to", file_name
  sys.exit()

file.write("<html>")
file.write("<h1>Python to Web Server Example<h1>")
file.write("<b>If you are reading this on your web server ")
file.write("you have successfully attached your python to ")
file.write("the TCP/IP network<p>\n")
file.write("</html>")
file.close()

print("done.\n")

#sleep for 1 second...what the heck...
time.sleep(1)

sys.exit()
copy and paste it out of here to a textfile, like "pythonhtml.py" and invoke with:

python pythonhtml.py

If you have MM running in the background, you should be able to connect to it on port 8080. If not check port 80.
Last edited by woodystanford on Tue Mar 14, 2017 11:31 pm, edited 2 times in total.

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Tue Mar 14, 2017 10:58 pm

scotty101 wrote:
woodystanford wrote: Actually I don't...still saving my pennies for one. Aren't they the same? *innocent look*
A pi is much cheaper than a beaglebone.
Yeah I found that out when I got a PI Zero W off of Ebay for like $25 (shipping included). Hoping to play with that soon as it hasn't come in yet.

addendum: Just came in. Missing that little MicroSD thingy thingy so I'll just have to head down to family dollar to get one or two. Hmmm, I wonder how I'm going to load it with whatever I need. (Wondering)

User avatar
woodystanford
Posts: 122
Joined: Sun Feb 19, 2017 8:17 am

Re: Embedded Web Servers

Tue Mar 21, 2017 8:06 pm

Look here's my point (and the point I'm thinking Mr. Torvaldes is trying to make) is one of these Pi's gotta find itself into a fridge eventually, if you know what I mean. (Holds it up by its USB teather like a school teacher). Look at it, its about the size of a postage stamp or there abouts. And it doesn't even blink like my Beagleboard.

If you just have mastery over the base protocols of TCPI/IP you should be able to set it up such that it has sufficient control over its stack that it can wirelessly connect with the devices around it with which to implement the IoT, right? I mean I'm not trying to be difficult but really peoples.<3

I really don't understand what peoples difficulty is with me (kinda shaking my head a little). I'm just shooting files across the file system among other things. Minding my own business.

I mean how hard is it to put a plastic enclosure around my Pi-W, sink a hole through and feed my thermistor in and have it connect up wirelessly with my broadband router over WiFi and just talk at low-speed to whatever? I mean how hard can it really be? (musing, I really wish I could get the UART's on my Beagle working....I gotta find someone that can direct me to a PDF of instructions that will enable them somehow).

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23083
Joined: Sat Jul 30, 2011 7:41 pm

Re: Embedded Web Servers

Wed Mar 22, 2017 2:10 pm

OK, are you actually saying you have only just got a Pi? And yet you have been posting Pi tutorials for some time? Are you taking the mick?

(If you paid 25 for a Zero, you could have got a A+ or saved another 10 and got a Pi3, which is a considerably better purchase for the stuff you have been doing)
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

Return to “C/C++”