0

How to: Upload a Lua file to the SIM5320a and execute it
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to: Upload a Lua file to the SIM5320a and execute it

by frooty4adafruit on Mon Oct 10, 2016 3:06 pm

Hi everybody!

Since I'm too busy to start (another) blog at the moment, I thought I'd leave some tips and discoveries here..

It turns out, the SIM5320a has a Lua virtual machine onboard which can execute your own applications directly on the chip! I'm working to see if I can simplify my hardware design on my project by eliminating a controller board (currently a Pi). Also, I noticed the SIM5320a has two I2C pins on it which may be available via the Lua libraries, but more of that in a later post. Currently, I'd like to show how I was able to upload HelloWorld.lua to the Fona and execute it. For reference, my SIM5320a breakout board is connected to a Pi Zero, which I am accessing via my Mac and ssh. (I should post that set up, too, which was a bear, a lot harder than for a Pi2 or Pi3...)

1.) SSH into the Pi Zero (named fonaPi.local on my internal network):
Code: Select all | TOGGLE FULL SIZE
ssh pi@fonaPi.local


2.) Write the HelloWorld.lua program (I used 'vi'. I'm old-school.)
Code: Select all | TOGGLE FULL SIZE
atctl.setport(1) --Set the serial port that talks over rx/tx pins.
atctl.send("\r\nHello World\r\n") -- Send "Hello World" over the serial port.
atctl.setport(-1) -- Release the port.  Dunno if this matters.

It is *important* to note that I ended the file with a carriage return. This makes it easier later.

3.) Check the size of the file on the bash command line:
Code: Select all | TOGGLE FULL SIZE
ls -al ls -al HelloWorld.lua

Shell output:
Code: Select all | TOGGLE FULL SIZE
-rw-r--r-- 1 pi pi 211 Oct 10 18:18 HelloWorld.lua

The file is 211 bytes.

4.) See if minicom is installed using the bash command line:
Code: Select all | TOGGLE FULL SIZE
 which minicom

Shell output:
Code: Select all | TOGGLE FULL SIZE
/usr/bin/minicom


5.) If not installed, install minicom from the command line.
Code: Select all | TOGGLE FULL SIZE
 sudo apt-get install minicom


6.) Connect to the SIM5320a fona board:
Code: Select all | TOGGLE FULL SIZE
minicom -D /dev/ttyAMA0 -b 115200

Note: It's a pain to get the Pi zero to see /dev/ttyAMA0. I'll describe the instructions in a Pi zero configuration post if requested.

7.) Prepare to upload the file from the minicom terminal:
Code: Select all | TOGGLE FULL SIZE
AT+CFTRANRX="c:/HelloWorld.lua",211

Response:
Code: Select all | TOGGLE FULL SIZE
>

Note: It is VERY IMPORTANT not to hit enter or any other key in the terminal window, since that will be sent as part of the file uploaded!
Note: 211 is the number of bytes we found earlier. Make sure it matches the results obtained in step 3.

8.) Upload the file from the minicom command terminal.

Hit 'ctrl+a'
Hit 's'

Response from minicom:
Code: Select all | TOGGLE FULL SIZE
+-[Upload]--+
| zmodem    |
| ymodem    |
| xmodem    |
| kermit    |
| ascii     |
+-----------+

9.) Select 'ascii' by arrowing down and hitting enter. You should see your Pi's local directory.

10.) Select 'HelloWorld.lua' by arrowing down the file and hitting 'space'. Then hit 'enter'.

Response (from the SIM):
Code: Select all | TOGGLE FULL SIZE
OK


11.) Make sure your file is there (Type this in the minicom terminal.):
Code: Select all | TOGGLE FULL SIZE
AT+FSLS=0

Response:
Code: Select all | TOGGLE FULL SIZE
+FSLS: FILES:                                                                     
HelloWorld.lua

12.) Check the file is *completely* there (from the minicom terminal):
Code: Select all | TOGGLE FULL SIZE
AT+CFTRANTX="C:/HelloWorld.lua"

Response:
Code: Select all | TOGGLE FULL SIZE
+CFTRANTX: DATA,211                                                               
atctl.setport(1) --Set the serial port that talks over rx/tx pins.               
atctl.send("\r\nHello World\r\n") -- Send "Hello World" over the serial port.     
atctl.setport(-1) -- Release the port.  Dunno if this matters.                   
                                                                                 
                                                                                 
+CFTRANTX: 0

13.) Run the code from the minicom terminal:
Code: Select all | TOGGLE FULL SIZE
AT+CSCRIPTSTART="c:/HelloWorld.lua",0

Response:
Code: Select all | TOGGLE FULL SIZE
OK                                                                               
                                                                                 
Hello World                                                                       

+CSCRIPT: 0

14.) Check out the Lua supplemental documentation for all the fun stuff you can do!

http://tinyurl.com/jhc4ej6

Note: Maybe somebody at Adafruit can get an updated version of this doc and post it?

frooty4adafruit
 
Posts: 40
Joined: Tue Sep 29, 2015 2:27 am

Re: How to: Upload a Lua file to the SIM5320a and execute it

by adafruit_support_rick on Tue Oct 11, 2016 11:30 am

Wow! I had no idea it could do that. Thanks for figuring it out!

adafruit_support_rick
 
Posts: 35095
Joined: Tue Mar 15, 2011 11:42 am
Location: Buffalo, NY

Re: How to: Upload a Lua file to the SIM5320a and execute it

by frooty4adafruit on Tue Oct 11, 2016 1:01 pm

There is more cool stuff-- you can compile the lua script directly on the chip! Also, there is a way to bind your script to a new AT command. I haven't gotten that far yet, but I'll post something when I do!

frooty4adafruit
 
Posts: 40
Joined: Tue Sep 29, 2015 2:27 am

Re: How to: Upload a Lua file to the SIM5320a and execute it

by adafruit2 on Tue Oct 11, 2016 1:08 pm

thats cool - thanks for posting!

adafruit2
Site Admin
 
Posts: 18652
Joined: Fri Mar 11, 2005 7:36 pm

Re: How to: Upload a Lua file to the SIM5320a and execute it

by frooty4adafruit on Tue Oct 11, 2016 2:04 pm

My pleasure-- Now, if anyone figures out how to upload a file using XMODEM and the AT+CRXFILE command (as the documentation suggests is possible), let us know!
This is supposed to allow a file to be uploaded via the XMODEM protocol, but minicom and the SIM are not playing well together.

The SIM5320a can play a .wav file, so I'd like to upload one.. I don't think the method I described earlier will work for binary files.. The alternative is to use the SIM5320a's capability as an HTTP or FTP client and fetch the files.

frooty4adafruit
 
Posts: 40
Joined: Tue Sep 29, 2015 2:27 am

Re: How to: Upload a Lua file to the SIM5320a and execute it

by Richmccarty on Mon Mar 06, 2017 8:21 pm

Dunno if this helps, but there is both a Lua ide and a visual (drag & drip) file transfer app for windows. The ide automatically handles the code uploading and compiling and has a debug window with start/stop. I think it uses the diag USB port to download the code.

Richmccarty
 
Posts: 25
Joined: Fri Jan 27, 2017 1:11 pm

Re: How to: Upload a Lua file to the SIM5320a and execute it

by frooty4adafruit on Mon Mar 06, 2017 10:25 pm

Thanks! I wrote the manufacturer for additional specs-- it looks like they also recommend using (their?) transfer program. I think the protocol is off-standard, since I could never get minicom to work as it should.

I will try the approach you mention, but I'm not sure whether Windows in a VM will work. We'll see! (I have a mac.)

frooty4adafruit
 
Posts: 40
Joined: Tue Sep 29, 2015 2:27 am

Re: How to: Upload a Lua file to the SIM5320a and execute it

by Richmccarty on Tue Mar 07, 2017 1:14 pm

It's definitely worth the effort to get the Lua code working. Today, I was quickly able to disconnect the external comm port from the AT interface and then use it to send and receive strings in my Lua code. The SMS library is also really easy to use. Calling sms. Read() returns a comma seperated string that is pretty easy to parse in Lua.
I am an old-school C programmer and this is my first time using Lua and I'd have to say that I like it for this application. I wish I'd known about it when I started this project as it would have saved me many hours dealing with the comm port interface on the arduino.

Richmccarty
 
Posts: 25
Joined: Fri Jan 27, 2017 1:11 pm

Re: How to: Upload a Lua file to the SIM5320a and execute it

by frooty4adafruit on Tue Mar 07, 2017 1:19 pm

Definitely cool stuff. I've also found that the libraries look pretty easy to use. I'm excited to see about using the I2C interface. Who knows? Maybe the SIM could be end up being the main processor... Wish the SPI interface were exposed.

frooty4adafruit
 
Posts: 40
Joined: Tue Sep 29, 2015 2:27 am

Re: How to: Upload a Lua file to the SIM5320a and execute it

by Richmccarty on Tue Mar 07, 2017 2:04 pm

Yes, the SIM module will end up being the main processor. It would be nice if more of the module I/O was broken out on the board, but full control of the external comm port makes using a subprocesser a simple matter.

Richmccarty
 
Posts: 25
Joined: Fri Jan 27, 2017 1:11 pm

Re: How to: Upload a Lua file to the SIM5320a and execute it

by Richmccarty on Fri Mar 10, 2017 6:04 pm

I had a bit more time this week to work on the Lua code. So far, the event handler, atctl (external serial port), SMS and voice_call events and libraries work as expected. Many actions, like an incoming voice call, cause multiple events but I've not as yet needed to use the sio (unsolicited result codes) which really simplifies things. The waitevt() works kind of like the select function in Linux C, but returns an event # and three parameters. The voice_call event, for example, has a single parameter indicating the new state (open, active, ringing, etc) so the code can ignore the sio events caused by a voice call.
Next on to mms & ftp!

Richmccarty
 
Posts: 25
Joined: Fri Jan 27, 2017 1:11 pm

Please be positive and constructive with your questions and comments.