WiFi and the Raspberry Pi

NodeMCU – Lua Language

Lua is a lightweight programming language designed for scripting, with embeddability and extensibility as primary goals. Lua is cross-platform because it is written in the standard C language and has a relatively simple application programming interface (API).

NodeMCU has written a Lua interpreter for the ESP8266, which I load into the ESP8266. Note that the Adafruit Huzzah ESP8266 comes preloaded with NodeMCU, so you can skip this step if you have not overwritten it. A great tutorial on how to flash NodeMCU on your Raspberry Pi is available online [9] as well as the Adafruit tutorial [10]. You can also find the NodeMCU combined firmware online [11].

Web Server Code

Listing 4 shows the code needed to implement a web server using NodeMCU. I build a file called init.lua and copy the above code into the Huzzah using the command:

Listing 4

Web Server Code Using NodeMCU

01 wifi.setmode(wifi.STATION)
02 wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
03 print(wifi.sta.getip())
04 led1 = 3
05 led2 = 4
06 gpio.mode(led1, gpio.OUTPUT)
07 gpio.mode(led2, gpio.OUTPUT)
08 srv=net.createServer(net.TCP)
09 srv:listen(80,function(conn)
10     conn:on("receive", function(client,request)
11         local buf = "";
12         local _, _, method, path, vars = string.find
             (request, "([A-Z]+)(.+)?(.+) HTTP");
13         if(method == nil)then
14             _, _, method, path = string.find
                 (request, "([A-Z]+) (.+) HTTP");
15         end
16         local _GET = {}
17         if (vars ~= nil)then
18             for k, v in string.gmatch
                 (vars, "(%w+)=(%w+)&*") do
19                 _GET[k] = v
20             end
21         end
22         buf = buf.."<h1> ESP8266 Web Server</h1>";
23         buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\
            "><button>ON</button></a>&nbsp;<a href=\
            "?pin=OFF1\"><button>OFF</button></a></p>";
24         buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\
            "><button>ON</button></a>&nbsp;<a href=\
            "?pin=OFF2\"><button>OFF</button></a></p>";
25         local _on,_off = "",""
26         if(_GET.pin == "ON1")then
27               gpio.write(led1, gpio.HIGH);
28         elseif(_GET.pin == "OFF1")then
29               gpio.write(led1, gpio.LOW);
30         elseif(_GET.pin == "ON2")then
31               gpio.write(led2, gpio.HIGH);
32         elseif(_GET.pin == "OFF2")then
33               gpio.write(led2, gpio.LOW);
34         end
35         client:send(buf);
36         client:close();
37         collectgarbage();
38     end)
39 end)
sudo python luatool.py \
  --port /dev/ttyUSB0 --src init.lua \
  --dest init.lua --restart

You can download luatool.py online [12]. A list of statements shows that you have downloaded the init.lua file into the ESP8266 and that the program has started. Make sure you change the Network SSID and network password in the init.lua file to match your local WiFi.

Open the website using Chrome. I've had issues with Safari on my Mac, but Chrome works. Figure 5 shows the resulting web page. Hitting GPIO1 Off will turn on the red LED and hitting GPIO2 Off will turn on the blue LED on the ESP8266 (Figure 6).

Figure 5: Chrome web page served up from your own ESP8266.
Figure 6: ESP8266 with LEDs turned on.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content