Got some time today to play more with nodemcu. I thought i would try the gpio module to, you know, turn on a led :)
I hooked up a led directly to gpio2 pin. And issued the commands (in a connected terminal)
gpio.mode(2,gpio.OUTPUT)
gpio.write(2,gpio.HIGH)
Nothing happened. This is supposed to set gpio2 to output and set it to a high evel, turning the led on. Or so i thought. I tried with gpio0 as well but to no avail. I didnt really believe my gpio pins were broken so I looked a little bit more at the documentation. And lo and behold, there was a gpio mapping table there :) It turns out gpio2 is mapped to index 4. So:
gpio.mode(4,gpio.OUTPUT)
gpio.write(4,gpio.HIGH)
did the trick :)
With nodemcu there is an example .lua script called webap_toggle_pin.lua. It starts a webserver and lets you toggle the logic level of a pin. Awesome!
A few tweeks of the code:
wifi.setmode(wifi.STATION);
wifi.ap.config({ssid="YOURNETWORKHERE",pwd="YOURPASSWORDHERE"});
pin = 4;
gpio.mode(pin, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
local buf = "";
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
if(method == nil)then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
end
local _GET = {}
if (vars ~= nil)thenfor k, v instring.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
endend
buf = buf.."<h1> Hello, NodeMcu.</h1><form src=\"/\">Turn PIN <select name=\"pin\" onchange=\"form.submit()\">";
local _on,_off = "",""if(_GET.pin == "ON")then
_on = " selected=true";
gpio.write(pin, gpio.HIGH);
elseif(_GET.pin == "OFF")then
_off = " selected=\"true\"";
gpio.write(pin, gpio.LOW);
end
buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option></select></form>";
client:send(buf);
client:close();
collectgarbage();
end)
end)
Upload this to the esp and issue restart and dofile. I had som problems at first with running it. It seems that you cant update the same file that is running. So if you want to upload this to main.lua make sure it is not currently running that script. Now, this information might not be 100% correct but making sure of this works for me.
Now you can get the ip by typing print(wifi.sta.getip()). Then you can just take that ip and put in your browser and control your led :)
Luatool is a tool to put files on the esp where nodemcu can find them and execute them.
The script takes a file (defaults to main.lua) and uploads it and stores it with the same name. So simply executeing ./luatool puts a file named main.lua (in the same dir) on the esp.
After it was done i connected to the serial port again and exectuted node.restart().
Then, the magic comman, dofile("main.lua") executes the file.
Since the program was a small webserver it was easy to verify. First i got the ip adress from the esp using print(wifi.sta.getip()), then i entered that ip in the browser. Voila!
The restart and dofile commands can be set as arguments to the luatool. I will check it out later.
It was pretty straightforward i must say. git-cloned the repo and burned the pre built binaries using the esptool from espressif package (see here: https://nurdspace.nl/ESP8266/First_setup).
Then i used cutecom on /dev/ttyUSB0 (9600 baud) and tried it out. Nodemcu has some nice documentation for what modules are installed and can be used.
In my last post i wrote that i had some pins "floating" when i uploaded the new firmware. When i tried to upload nodemcu today that didn't work. So, following the isntructions on this page: http://www.electrodragon.com/w/ESP8266#Pin_Wiring_.28V090.29, I connected GPIO0 to GND and GPIO2 to VCC when booting! Then, after boot, i disconnected GPIO2 (to floating) and uploaded firmware. I dont know if this is something with the blinky example that i hade burned or if i missed something i did the last time i wrote.
Anyways, it seems to work and it is time to test nodemcu :)
After downloading and compiling it (it was automatic and worked great) i tried to burn the blinky example. I connected CH_PD to VCC and GPIO2 and GPIO0 ended up floating. This was the way that worked for me to have the esptool program connect to the esp.
Next step is to burn the node-mcu firmware and test som Lua action!
So, I got the esp connected to a usb-serial adapter (pl2303) and used cutecom to send AT commands. It works :)
At fist i had trouble. I was getting a lot of "noise" on the line. The device was spitting "0x00" all the time. With the correct baud setting i was able to see some clear text messages in between.
A note on the interwebs led me to the solution: since i am using a sepparate PSU to power the esp i needed to have common ground between the psu and the serial-adapter. A bit of cable connected between them and everything worked lika a charm! :)