Tuesday 17 March 2015

IoT - Using the ESP8266 WIFI module to make a Web Enabled LED



ESP8266 Module


Info & Guides

NurdSpace complete guide

Electrodragon

LUA Programming Language

http://www.esp8266.com/


Software

CoolTerm - Free Serial Terminal Emulator

nodemcu Firmware & Programmer - LUA Firmware

LuaLoader - Great MultiFunctional software (run the file as Administrator if you do not see your comm port)

ESPlorer - IDE for ESP8266 developers






Pin-Outs

ESP8266




CH340 Programmer





Quick Guide

  1. Wire up 8266 as per schematic above.
  2. Confirm communications are working to the 8266 with Coolterm.
  3. Set the 8266 into 'Flash update' mode by setting GPI00 to GND.
  4. Reset the power to the device.
  5. Download Flasher & firmware found here and flash the firmware to the ESP8266.
  6. Confirm comms again with Coolterm noting the change in firmware.
  7. Set the 8266 into 'Flash update' mode by setting GPI00 to GND.
  8. Reset the power to the device.
  9. Download nodemcu found here and flash the ESP8266 with 'ESP8266Flasher' using the default binary (nodemcu_latest.bin: 0x00000).
  10. Confirm comms again with Coolterm. a status line similar to
  11. 'NodeMCU 0.9.5 build 20150311 powered by Lua 5.1.4' will indicate LUA firmware is now installed.
  12. Download LuaLoader from here and set it to your correct Com Port. If LuaLoader does not see your Com Port set it to run as 'Administrator' in the file compatibility settings.
  13. You can now use LuaLoader to load programs onto the ESP8266 like the one below . . .



Webserver with web enabled LED



lua code


wifi.setmode(wifi.STATION);
wifi.sta.config({ssid="YOURROUTERNAME",pwd="YOURROUTERPASSWORD"});
gpio.mode(4, 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)then 
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do 
                _GET[k] = v 
            end 
        end
        buf = buf.."<h1><b> Arduino Virgins - IoT LED</b></h1><form src=\"/\">Turn GPIO2 <select name=\"pin\" onchange=\"form.submit()\">";
        local _on,_off = "",""
        if(_GET.pin == "ON")then
              _on = " selected=true";
              gpio.write(4, gpio.HIGH);
        elseif(_GET.pin == "OFF")then
              _off = " selected=\"true\"";
              gpio.write(4, gpio.LOW);
        end
        buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option></select></form>";
        client:send(buf);
        client:close();
        collectgarbage();
    end)

end)





Notes


  • Replace YOUROUTERNAME with the name of your own router and YOUROUTERPASSWORD with your routers password.
  • Upload this code using LuaLoader by copying it to your clipboard and using the 'Paste Text' button in LuaLoader to upload it to the ESP8266.
  • Connect an LED negative to a 220Ohm resistor, and the other end of the resistor to GND. Then connect the positive end of the LED to the GPIO2 pin on the ESP8266. . . . schematic to follow.
  • Use the 'Get IP' button in LuaLoader to get the IP address of the ESP8266(if you do not see a response of 'nil'you probably have the wrong routername and password setup in the above step).
  • Put the first address in your web browser. Its normally something like 192.168.0.20


You should see the following web page






ESP8266 PROJECT NOTES


  • My ESP8266-01 Module was set to 9600 baud by default.
  • After flashing it with ver  0.92 baud rate was 115200bps.
  • It CAN NOT be powered by the CH340 programmer. It has to have its own supply of 3.3v @ 20-250mA.



A high percentage of the project information was taken from Julian Ilets Videos shown below.


Part 1 of 4

Part 2 of 4

Part 3 of 4

Part 4 of 4

This may also be useful. More information from Great Scott















TO-DO

Create a colour wiring schematic in Fritzing.

Take a video of it working.

Upload more scripts.































1 comment:

  1. Hi!

    I would like control more GPIO . Can you help me add more button in the sourche code?

    Thanks

    ReplyDelete