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?
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.
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.
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.
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.
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...).
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.
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".
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.
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.