Sunday, March 24, 2013

Fun with Embedded HTTP Servers

I have been distracted for the past week by an embedded HTTP server. I am currently taking a very exciting course entitled Internet Embedded Systems taught by Senior Embedded Software Designer Robert Laswick. The end goal of the course is to achieve web based control and status of hardware.
Olimex MOD-IO with ENC28J60 Module
After I completed the core objective of the course I decided to run absolutely wild with the concept. I created some Makefile targets that minify, gzip and convert web resources (HTML, JavaScript, CSS) into C header files which I can then serve up to the browser.

Overview
This entire project is hosted on the Olimex MOD-IO development board with the famous ENC28J60 Ethernet controller attached. Briefly, the ENC28J60 Ethernet controller is a very cost effective way to Ethernet enable low-cost, low-power 8-bit microcontrollers using the SPI bus.

The course made use of the TuxGraphics TCP/IP stack which is surprisingly easy to bring online. The ATmega16 CPU on this development board is not natively supported by the stack but only a few simple modifications are required to make it compile and load without warning. I was up and running in under an hour.

The Interface
One severe limitation of this hardware is the 1kB of RAM. This limits the response size to approximately 700 bytes. I could have broken up my responses into multiple parts but decided to keep all responses under the buffer size.

The Relay Control Homepage
I have been doing web development since I was in elementary school. This fusion of hardware and high-level web development has been a lot of fun. The relay control homepage turned out great.

Here is the Makefile target that I use to minify, gzip and convert to a C header file the stylesheet used for this project. You can yell at me for using sed twice, I was in a hurry!

%.css.h: %.css
    yui-compressor --type css $^ > $^.min
    gzip -c $^.min > $^.gz
    xxd -i $^.gz | sed -e 's/.\[\]\ =/\[\]\ PROGMEM\ =/g' | sed -e 's/unsigned/const/g' > $@
    rm -f $^.gz $^.min

I have one of these for JavaScript and HTML (varies slightly) as well.

Uptime Counter
I have implemented 3 basic features. The first is the System Uptime counter. This is a simple struct residing on the microcontroller that keeps track of days, hours, minutes and seconds.

typedef struct {
    uint16_t days;
    uint8_t hours;
    uint8_t minutes;
    uint8_t seconds;
    uint16_t millis;
} uptime_t;

I increment uptime once per millisecond in a timer compare match ISR. It would have been faster to just keep track of uptime in a uint64_t and then convert this into days, hours, minutes and seconds when required, but I like this approach.

void __attribute__((signal)) TIMER0_COMP_vect(void) {
    if(++uptime.millis == 1000) {
        uptime.millis = 0;
       
        if(++uptime.seconds == 60) {
            uptime.seconds = 0;
           
            if(++uptime.minutes == 60) {
                uptime.minutes = 0;
               
                if(++uptime.hours == 24) {
                    uptime.hours = 0;
                    uptime.days++;
                }
            }
        }
    }
}


Uptime is served to the browser using JSON. This is fetched in JavaScript using an AJAX request. This results in a very efficient use of system resources. This system can handle 150+ hits per second on the dynamically generated JSON pages.
{"d":0, "h":0, "m":0, "s":9, "ms":754} 
Total HTTP Requests
This is a simple one. It simply counts the total number of HTTP requests. This is also served using JSON.
{"count": 35}

The "AVR Web Server" Section
Relay Control
The next step was to control some hardware. I decided to parse out a URL with the format /rly$num$state where $num is a number between 0 and 3 and $state is either 'on' or 'off'. If the microcontroller receives this command, it will turn on or off the selected relay.

In addition to this basic relay control, I have implemented HTTP authentication. When a relay command is sent, the device is also expecting the authentication headers. If no authentication headers are sent, the browser will receive a 401 response.

Authentication Required
Relay Status
I also have a relay status feature. Relay status is served using yet another JSON request.

Hardware with Programmer/Debugger Attached
Setup Overview
Conclusion
The number of possible use cases for hardware like this is unbounded. I may end up using it to control my homebrew fume extraction system, but that is a another blog for another day.

If you have any questions or comments, leave them below. Thanks for reading!  

39 comments :

  1. You could use only one sed:
    sed -e 's/xxx/yyy/g' -e 's/aaa/bbb/g'

    Nice work! I look at this and see a home automation system.

    ReplyDelete
    Replies
    1. Thanks for the tip!

      I plan to automate my fume extraction system this summer.

      Delete
  2. Great work!
    My graduation project is to realise a web server on an STM32F4 board, I am going eventually to use the LwIP tcp/ip.
    I'd like to ask you the difference between your stack and the LwIP one.
    Thanks.

    ReplyDelete
    Replies
    1. Nice keyboard by the way ;)

      Delete
    2. I have not personally used the LwIP stack but a quick read of the features shows that LwIP is much better but requires more system resources.

      I believe the LwIP stack is portable across many processors but the TuxGraphics stack is AVR specific.

      From the LwIP Website: "This makes lwIP suitable for use in embedded systems with tens of kilobytes of free RAM and room for around 40 kilobytes of code ROM."

      Even the biggest of AVRs don't have those kinds of resources. I am unsure if the TuxGraphics stack is compatible with the XMega line of AVR processors. I imagine with some tweaks it could be made to work.

      Delete
    3. Thanks.
      Keep up the good work!

      Delete
    4. FYI. An STM32F4 processor and the LWIP stack with an ENC28J60 is exactly what is used on the NetduinoPlus 2.

      Delete
  3. This is a great direction to be studying - network / machine interfaces or "virtualization" as some people seem to want to call it is going to be in increasing demand in some areas of the world with aging population demographics. A decline in available labor means more automation will be needed. But, on a more positive note, there is also the idea that more young people can be learning this technology to forge niche businesses for themselves in economically depressed areas that could be improved through strategic automation. Something to think about.

    Btw I recommend taking some business classes before you graduate if you haven't already. Making good use of this knowledge will be greatly benefited from some ability to capitalize on what you are learning. You've got a bright future ahead. :-D

    ReplyDelete
  4. Would html file be even smaller if you embedded svg directly (svg tag with a path)?

    ReplyDelete
    Replies
    1. The SVG file uncompressed is around 700 bytes. When gzipped, the svg is around 380 bytes. As far as I know, gzipped base64 encoded images are only supported in very few browsers.

      I have leveraged the img tag to have the svgz be served on another HTTP request.

      Delete
  5. Can you post the sourse code please.I want to build something like this.

    ReplyDelete
    Replies
    1. Hi Manos,

      Normally I like to share the source for my projects. In this case, there really isn't much to share. I am more or less using the default web server application provided with the stack.

      You will find the TuxGraphics TCP/IP stack here: http://tuxgraphics.org/common/src2/article09051/

      I am planning to do a summer project where I automate a some 120VAC Mains lines. I will have some code to share at that point.

      Delete
    2. Ok thank you anyways,i'll give it a try.Good luck

      Delete
  6. Can you make it with mod-wifi

    ReplyDelete
  7. goodness, extraordinary, I was thinking about how to fix skin break out normally. what's more, discovered your site by google, took in a ton, presently i'm somewhat clear. I've bookmark your site and furthermore include rss. keep us refreshed. buy cloud vps

    ReplyDelete
  8. That is really nice to hear. thank you for the update and good luck. www.dedicated-servers.com

    ReplyDelete

  9. You write this post very carefully I think, which is easily understandable to me. Not only this, but another post is also good. As a newbie, this info is really helpful for me. Thanks to you.
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai

    ReplyDelete
  10. Top Technologies to learn
    Excellent blog with lots of information. I have to thank for this. Do share more.

    ReplyDelete
  11. Python Training in Chennaii

    I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates.

    ReplyDelete




  12. Each of the women associated with our agency is strictly professional when it comes to rendering her services to her clients.Russian Call Girls in Gurgaonhere at our escort agency make sure that their talents are such that they will never fall short of giving the best of services to their clients. Check our other Services...
    Russian Escorts in Gurgaon
    Call Girls in Faridabad
    Escorts in Faridabad
    Escorts Service in Faridabad
    Russian Escorts in Faridabad

    ReplyDelete

  13. great job :)
    would you share your school project main description?
    I would be glad to talk about this project more with you.

    ReplyDelete

  14. Each of the women associated with our agency is strictly professional when it comes to rendering her services to her clients.Model Call Girls in Udaipurhere at our escort agency make sure that their talents are such that they will never fall short of giving the best of services to their clients. Check our other Services...
    Call Girls In Udaipur
    Russian Escorts in Udaipur
    Foreigner Escorts in Udaipur
    Model Call Girls in Udaipur
    Russian Call Girls Udaipur
    Russian Escorts in Udaipur

    ReplyDelete
  15. I have been reading for the past two days about your blogs and topics, still on fetching! Wondering about your words on each line was massively effective. Techno-based information has been fetched in each of your topics. Sure it will enhance and fill the queries of the public needs. thanks for ut efforts
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  16. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


    ReplyDelete
  17. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  18. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!! embedded hardware

    ReplyDelete
  19. selenium interview questions and answers pdf download

    Get here for Selenium Interview Questions and Answers for freshers and experienced person those who want to explore more on testing selenium interview questions and answers for experienced

    ReplyDelete
  20. devops interview questions and answers for freshers

    Important DevOps Interview Questions and Answers for freshers and experienced to start your career in DevOps! 101 DevOps Basic Interview Questions for Freshers.devops interview questions and answers for experienced

    ReplyDelete
  21. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Security Guard License
    Ontario Security License
    Security License Ontario
    Security License

    Thank you..

    ReplyDelete
  22. Great Post, thanks for sharing such an amazing blog with us. Visit Ogen Infosystem for creative website design and PPC Services in Delhi, India.
    Website Designing Company in Delhi

    ReplyDelete
  23. It helps companies decide what the next step would be and if that step would take them forward, or not. data science course syllabus

    ReplyDelete