Wee-Things: Hello, World!
In this tutorial we will learn how to load a simple hello world script into our NodeMCU devkit board using the esp cli tool.
You should follow the introductory tutorial to get your Mac environment ready.
Once we have completed this tutorial we should be able to write our own programs and upload them to a NodeMCU board. There will be also a minimal introduction to strings in the Lua programming language.
Source Code
The actual program we are going to write is rather simple. The main focus is the process to get code running in the board. We will use the esp cli tool to upload and execute a file.
In the spirit of hello world, here is our very first program:
print("Hello, World!")
The print
statement takes a string parameter and will output it to the console, in this case the string "Hello World!". Below you can find some information about strings in Lua.
Create a new file
$ touch hello_world.lua
Create a new file, and name it hello_world.lua
and save it to the code directory. You can use any text editor or IDE to create the file.
The contents of the hello_world.lua
fie is our glorious program:
print("Hello, World!")
To upload the file to the board we will be using the esp cli tool. If you have not installed it yet you can follow the instructions on the tool's repository.
From terminal, cd
to the directory where you saved the hello_world.lua
file, and from there execute:
$ esp file write hello_world.lua
The esp file write
subcommand takes one argument, a path to a file that will be uploaded to the board.
You can peek the contents of the file system with the following command:
$ esp file list
To see a list of the esp file
subcommands type the esp file --help
command to display the help dialog.
Executing a file
Now we are going to execute our program.
$ esp file execute hello_world.lua
Your terminal window should show the following text:
Hello, World!
Lua Strings
In Lua you use two dots to concatenate strings,
print("Hello, ".."World!")
You could also use variables. If you assign a string value to a variable, you could dynamically create a string:
local name = "Peperone"
print("Hello "..name.."!") -- outputs: Hello Peperone!
If we want to interpolate variables in a string, it can get unwieldy pretty fast:
local name = "Peperone"
local something = "pancakes"
print("Hello "..name..", do you like "..something.."?")
-- outputs: Hello Peperone, do you like pancakes?
In lua you can do string interpolation using the string.format
utility- you can read about it here. But basically...
String interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
local name = "Peperone"
local something = "pancakes"
print(string.format("Hello %s, do you like %s?", name, something))
-- outputs: Hello Peperone, do you like pancakes?
Multiline
In Lua you can also have multiline strings using the [[
characters to open a multiline sequence, and the ]]
characters to close it.
conn:send([[<h2>The module MAC address is: ]].. ap_mac..[[</h2>
<h2>Enter SSID and Password for your WIFI router</h2>
<form action='' method='get' accept-charset='ascii'>
SSID:
<input type='text' name='SSID' value='' maxlength='32' placeholder='your network name'/>
<br />
Password:
<input type='text' name='PASS' value='' maxlength='100' placeholder='network password'/>
<br/>
<input type='submit' value='Submit' />
</form> </body> </html>]])