Thursday, December 25, 2014

Fun with Lua and Other Small Device Challenges

I certainly can't blame anyone for not understanding what Lua is. A few months ago, I hadn't heard of it, either, and when I became acquainted with it, I must admit, I was a bit resentful that my new Arduino Yun was using this arcane language as the default shell, instead of bash, of which I had fair knowledge. Time heals all wounds though, and that has been the case with this rather odd little language as well. It's really a lot more powerful than bash by itself and is more akin to C or Python, at least in the sense of handling routine functions, like data manipulation with numbers, arrays, hashes and, Yay!, even JSON.

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.

Tuesday, February 22, 2011

Thoughts on Education Policy: Education Roundtable: Dealing with Disaffected Students

Thoughts on Education Policy: Education Roundtable: Dealing with Disaffected Students

Save Huntington Park High School
(California Schools)

Blogging for Respect

Education is under attack in our country and it is our obligation as teachers to fight back. Wisconsin is the first battle in many months where public support is showing the politicians and financial robber barons who have been brain-washing our society into thinking that their greed is good and necessary, and that we are the true pariah's of society; that this is the big lie. We won't stand for it, and we certainly won't endorse it. It is time for us to educate the public about how the leaders of our government and the captains of finance and industry have turned their backs on the public servants that have held this country together through good and bad times. Their deception will be exposed. The simplicity of their arguments will be taught in classrooms all over the country, and soon they will be on the run. This time they won't be able to fleece the public and point the finger at others. As we see our wages cut, furlough days added to our abbreviated year, benefits slashed and vitriol in the press about our practice, we gnash our teeth and hold on. Well, enough is enough. We are not the millionaires, not the tycoons, not the financiers or Wall Street bankers who have led our country to ruin. We are, and always have been, people who like to help others, who are educated, dedicated, and determined to set this political and societal injustice aright. We are not as docile and meek as the opposition would like us to believe. We are intelligent, and we understand what this campaign is all about...sucking the funding out of the public till and putting it in the pockets of the rich who have run out of purses to fleece. Stay tuned for more action. The day of reckoning is coming.