Arduino web server template engine

Hi,
I am planning on using arduino web server for my home automation project, and i am wondering if there is some template engine that would work on Arduino. I see that there are couple of template engines running on C/C++

But I don't know if Arduino Mega can handle it. Using pure html pages with ajax requests seems like a pain in the ass, and it is not modular.

Do you know if it's good and even possible idea? Do you use some template engine?

I can't imagine any Arduino (apart from the Linux side of a Yun) being capable of running a template engine.

The Python Bottle web framework works nicely on a Yun.

Maybe you could use a template engine on a PC to develop your HTML and then copy and paste it into your Arduino application.

...R

Thanks for reply.

Arduino Yun sounds like the best bet or I have to wait for the Arduino TRE.

I can still do the automation on MEGA, but I am not sure if it will be enough...

I will use SD card for all the web files (html,js) + json config. So the arduino MEGA will be accessing server files from SD card, it needs to have json library and it will collect data from other arduinos wirelessly through radio communication.

Do you think that MEGA would be enough for this?

kerel:
Do you think that MEGA would be enough for this?

I really have no idea. It is not the sort of thing I would try to do on an Arduino - but I know I am peculiar.

...R

The Sming open framework is intended for this sort of thing on the ESP8266, but the resources on that chip far exceed what the Mega SRAM of 8K provides.

As the ESP8266 Arduino core and WiFi library mimic the Arduino WiFi shield, you may find some examples in that architecture applicable to the Mega2560 Arduino.

Another way is to use a raw C++ technique: see this reference. A block of text can be stored with replaceable tags. You can see it in action here.

If you are running low on SRAM, you may wish to consider one of CrossRoads excellent 1284 boards with 2x (16K) SRAM in an UNO form factor.

Ray

It might be reasonably easy to do a very crude one.

You need a function that takes a template (in progmem, say, or on an SD card), a set of substitution parameters, and an output stream. It has to scan the template stream, replace tokens with the parameters, and spit the result to the output stream as it goes. Managing the parameter types might be a thing - perhaps it might be best for the parameter object to convert everything to strings. Alternatively, a union might be the way to go:

struct Param {
  char *name;
  byte type;
  union PP {
    char c;
    int i;
    long l;
    char * s;
    float f;
    double d;
    // etc, etc
  } p;
} params[10];

void processTemplate(InputStream in, OutputStream out, struct Param *params, int nParams) {
  …;
}

It might even be feasible to do nested stuff - subtemplates, iterators etc. It needs to be kept small, though.

I thought about it, and I've maybe found the solution, correct me if I am wrong.

The whole arduino web app will be using ajax, so I will load all the html structure in .js files, and arduino will be used just for getting states of devices and making the communication between other devices. So basically the js files will be my "template engine".

The C code and JS code will be sharing one jSon file from SD card with settings (info about devices). This should not affect Arduinos capability and memory, since it would load html only once, and other requests would be just for the devices data.

Is it possible?

Howdy Kerel,

I believe the chaps above are hinting that an Arduino, even the better ones are really not suited to be used as web servers for anything other than anything really basic.

A raspberry Pi would be far more suitable for that task using say Apache, PHP & SQLite3.

However to find out the status of pins, variables etc... on an Arduino remotely is very easily done and you have two simple options.

#1 Listen for a client request on the Arduino and deliver the details

This would be using the Arduino as a very simple web server, see here https://www.arduino.cc/en/Tutorial/WebServer for exampe code, change what you're responding with to a format you find most useful.

#2 Sent HTTP POST messages every N seconds/minutes to a more capable web server

Personally I'd suggest #2, it's lighter on the coding side, but either will work.

An example is in a query I asked earlier today here http://forum.arduino.cc/index.php?topic=379807.0 which is run through a timer to send automatically to a file called "post.php".

This keeps the sending of data automatic, light weight and then the details received can be managed in PHP where you can do pretty much anything you want (including running your JS, Ajax etc...).

Also if you're hell-bent on JSON with the Arduino, then see this library GitHub - bblanchon/ArduinoJson: 📟 JSON library for Arduino and embedded C++. Simple and efficient. it works very well :slight_smile:

Hope that helps Kerel,

Matt

PS. I am assuming you have an Ethernet connection or Wifi board attached to be able to respond or send data across the network.

Howdy Moggiex,

I think your solution is the best.

To have raspberry pi server and arduino mega connect via ethernet to router, arduino would be just a gateway for the radio communication to the raspberry pi and all the data would be stored on raspberry.

Thanks everyone!

kerel:
I thought about it, and I've maybe found the solution, correct me if I am wrong.

The whole arduino web app will be using ajax, so I will load all the html structure in .js files, and arduino will be used just for getting states of devices and making the communication between other devices. So basically the js files will be my "template engine".

Is it possible?

It's possible, and I have an example in the playground (WebServerJson). JSON rather than AJAX, but same thing.

PaulMurrayCbr:
JSON rather than AJAX, but same thing.

I thought they were very different things?

I thought JSON is a format for data (an alternative to XML) whereas AJAX is a system for updating web pages asynchronously using Javascript code in the browser.

...R

Hi Kerel,

An after thought for you, have you seen the ESP8266 boards?

They’re wifi boards that you can program with or without an Arduino (and you can use the Arduino IDE too!).

See here for an instructable http://www.instructables.com/id/ESP8266-mini-Tutorial/

Just be 100% aware they like a lot of current that a Arduino just can’t deliver, so you’ll want to give them a separate 3.3V power source, just make sure the GND’s are connected together.

Which should be noted is very cheap & simple to do as you can create one using 1 LM1117 chip & two capacitors, a circuit diagram is here:

And by the way, for my network, I’ve decided to go to the trouble of using PoE, which is “Power over Ethernet” to power each node, provide a network connection and each unit is going to be wired directly.

The walls I have in my current house are 2-3 foot thick and nothing is going through them, grrrrrr. I’ve tried the RFM69HW chips at 433Mhz, 868Mhz & 915Mhz, the cheaper 315Mhz chips, the NRF24L01+ and NRF24L01-PA-LNA at 250Kbps, 1Mps & 2Mps, none of them have enough strength to cover the distances needed, so I’m going “stone-age” & wiring them up directly.

Hope that helps Kerel,

Matt