ComputerSense
All things educational, but particularly for those involved in teaching computers, and or technology
Thursday, December 25, 2014
Fun with Lua and Other Small Device Challenges
I have written my first fairly featured Yun script, that enables me to take pictures with a webcam and control it remotely through a web site. Actually, I am still working on it, but I finally figured out the one piece that was causing me so much grief, how to parse information saved on a web page through PHP in JSON format and use it to read an array of values used to control the webcam. This is not an original project, but I hadn't seen it done in Lua before. The conventional solution is written for the Raspberry Pi or Yun in Python, which is fine, but my reasoning for using Lua, was that it is closer to the Yun's native operating system, and should load faster than Python. This would give me an opportunity to learn about this interface to the Linux OS on the YUN. I bought twenty of them for my classes! I was keeping my fingers crossed that the Lua feature set would be sufficient to handle the ability to write files, shell out to applications like wput and wget, and to be able to translate information downloaded from a remote web site into an intelligible format. With luck, this language might even be worthwhile to teach to students interested in learning how to program small devices.
Lua looks a bit like Python. There is no need to explicitly declare data types before execution.
myfile="/mnt/sda1/filename.txt" works just fine. It is an untyped language that gives you the option to set local or global values for functions, which makes it a bit like JavaScript. It has it's own quirks for sure! You can make a local function with a declaration like local function myfunction().
There is no need for any opening brackets, like in so many languages, and you end the function with a simple end. No punctuation needed at all...very Python-like. It also integrates very nicely with the operating system. To call wget, for example, you embed the command needed in a string variable, and call it with a call to the os via os.execute("command"). To download Yahoo's web page, you would write os.execute("wget http://Yahoo.com"). This would download the webpage via wget, and wget would save the file locally. After that, you can issue file commands to manipulate the file as needed. That is just one simple approach. I used wput to allow me to log into my ftp site to upload pictures from my webcam. This all went very smoothly.
The biggest issue up to now that I have had to contend with, is the formatting of JSON variables. JavaScript has no issues with single quoted values such as {'name':'Fred','Occupation':'caveman'}.
PHP, which I used as the glue to write the data to a site to hold my commands to remotely control
the webcam, is pickier than JavaScript and will make life tough if you don't use it's JSON encoding and decoding functions to wrap up values in a format suitable to be written to a data file. PHP will wrap up everything you need in single quotes, where JavaScript is lax enough to allow you to omit
quotes entirely from the descriptor fields, You can get away with var person={name:'Wilma'}; in JavaScript and post it up to PHP. If you look carefully, you can see that the variable descriptor, name, is not quoted. PHP's json.encode() function will clean that up and quote both string data and descriptors. Now you can pick out the data in an associative array and rewrite your cleaned up JSON to a file....but here's the rub and it took me quite a while to figure out. First I tried to find examples for JSON and Lua on the web, and that didn't yield very much. Finally, I discovered that I could link in the JSON library by writing an include statement that looked like this, local JSON=require 'json'..
Like PHP require and include can be used to add libraries and routines to the core functions. The good news was that the json library is internal, and loads very quickly, which keeps things fast and small, which were two of my main goals to start with.
Lua's JSON implementation doesn't like single quotes at all! Uff. Had I known, I would have saved myself quite a bit of time and grief! The equivalent function to replace in Lua is called string.gsub(), and it's a bit odd. That function ultimately, was the solution to getting my script to manipulate the camera through a simple program called fswebcam. Yes, I tried using motion, for all you Linux purists, and it was a colossal headache. I decided that more coding and less struggling with motion's picayune configuration options would work better on a small device like the Yun and it's rather limited Linux OS.
Back to string.gsub(). Well that oddball program works backwards to the c family of replace functions that most of us are used to writing. Instead of writing something like this using JavaScript syntax: var myword=myword.replace("/n","/t"), which would replace the first instance of a tab, with a newline character, Lua does it this way: newword=string.gsub("/t","/n"). Since you can't replace the string myword directly, like you can in JavaScript, you need to create another variable, newword in this case. Instead of replacing the last characters in quotes with the first set, you replace the first ones found , "/t", with the second, "/n". It makes more sense in a logical straight-forward sort of way, but is counter-intuitive to someone who has been doing this the opposite way for years. One very nice feature of this string.gsub() routine, and Yes, you always write the words string,gsub; They are part of the string formatting library; Lua will replace all the instances of the character(s) you are replacing. This eliminates the need for loops. You do have an option to replace a maximum number of instances of the characters you are looking for with a third parameter. Ultimately, this function was very nice, and did the trick. I will publish more code with explanations in my next blog post whenever that will be, and reveal more of the mysteries inherent in coding web programs with small devices using Lua, PHP and Javascript. It has been a challenge, but is also a great learning opportunity.
Saturday, May 25, 2013
Round and round
It has been a very long time since I blogged. This blog has been untouched by me or any other human for years. Now that I'm teaching at a different school, Elizabeth Learning Center, my life has taken a few turns largely for the better. I am teaching at a school that still has a California Partnership Academy in technology. The kids are pleasant and mostly try pretty hard. Some have real talent and ambition and are engaged in what I teach. The work load is heavier. We put in more effort to give these kids a meaningful experience as a whole. The teachers are dedicated and the principal is very supportive of our academy.
Our superintendent and his minions are another story altogether. I will be taking a stab at National Board Teacher Certification with a cohort from our school. My goal is to keep things relevant and fast paced in the classroom and have the kids get the most out of the classroom experience that they can. A little raise is built into the certification, which sincerely can't hurt. In any event I will be adding a lot of new entries to this blog as my experiences mount up. I a looking forward to the end of this year. I just finished a unit on SQL and PHP, something I hadn't tried in the past. I will carefully plan out my lessons for next year. Hopefully, the NBTCS program will prove beneficial in the process.