LEGO Rocket Launcher - Part 3 - Microcontroller

The Rocket Launcher is controlled by an ESP8266-12 microcontroller.

I bought a NodeMCU Amica v2 board. It costs less than 10 euros from european internet sellers or 2/3 euros from asiatic sellers (if you can wait a month for shipping).




The ESP8266's GPIO pins (D0, D1, D2, D3, D5, D6) are connected to motor driver board inputs (C1, C2, C3). 
The motor board is able provide +5V to ESP8266, as shown in the figure; don't forget GND pin.


I flashed the ESP8266 using NodeMCU firmware version 1.5.4.1-final.

Then I uploaded the needed lua scripts, init.lua and lrr.lua:
  • init.lua configures wifi and then calls lrr.lua
  • lrr.lua: runs a web server listening for motor commands

wifi.setmode(wifi.STATIONAP)
wifi.sta.config("YOURssid","YOURpassword")
print(wifi.sta.getip())
cfg={}
cfg.ssid="ESP8266"
wifi.ap.config(cfg)
dhcp_config ={}
dhcp_config.start = "192.168.4.100"
wifi.ap.dhcp.config(dhcp_config)
wifi.ap.dhcp.start()
dofile("lrr.lua")



The script sets ESP8266 in AP+STATION mode:

  • AP mode: ESP8266 works as access point (SSID = ESP8266) and DHCP server; DHCP scope starts at 192.168.4.100; ESP IP is 192.168.4.1
  • STATION mode: ESP connects to home wifi network as DHCP client; you have to find on your modem router the ESP IP

C1a = 0
C1b = 1
C2a = 2
C2b = 3
C3a = 5
C3b = 6
gpio.mode(C1a, gpio.OUTPUT)
gpio.mode(C1b, gpio.OUTPUT)
gpio.mode(C2a, gpio.OUTPUT)
gpio.mode(C2b, gpio.OUTPUT)
gpio.mode(C3a, gpio.OUTPUT)
gpio.mode(C3b, gpio.OUTPUT)
gpio.write(C1a, gpio.HIGH)
gpio.write(C1b, gpio.HIGH)
gpio.write(C2a, gpio.HIGH)
gpio.write(C2b, gpio.HIGH)
gpio.write(C3a, gpio.HIGH)
gpio.write(C3b, gpio.HIGH)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        buf = buf.."HTTP/1.1 200 OK\n\n"
        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> ESP8266 Web Server</h1>";
        buf = buf.."<p>GPIO01 <a href=\"?pin=ONL\"><button>UP</button></a></p>";
        buf = buf.."<p>GPIO10 <a href=\"?pin=ONR\"><button>DN</button></a></p>";
        buf = buf.."<p>GPIO23 <a href=\"?pin=ONU\"><button>LT</button></a></p>";
        buf = buf.."<p>GPIO32 <a href=\"?pin=OND\"><button>RT</button></a></p>";
        buf = buf.."<p>GPIO56 <a href=\"?pin=ONF\"><button>FR</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ONL")then
              gpio.write(C1a, gpio.LOW)
              gpio.write(C1b, gpio.HIGH)
              tmr.delay(500000)
              gpio.write(C1a, gpio.HIGH)
              gpio.write(C1b, gpio.HIGH)
            elseif(_GET.pin == "ONR")then
                gpio.write(C1a, gpio.HIGH)
                gpio.write(C1b, gpio.LOW)
                tmr.delay(500000)
                gpio.write(C1a, gpio.HIGH)
                gpio.write(C1b, gpio.HIGH)
                elseif(_GET.pin == "OND")then
                    gpio.write(C2a, gpio.LOW)
                    gpio.write(C2b, gpio.HIGH)
                    tmr.delay(500000)
                    gpio.write(C2a, gpio.HIGH)
                    gpio.write(C2b, gpio.HIGH)
                    elseif(_GET.pin == "ONU")then
                        gpio.write(C2a, gpio.HIGH)
                        gpio.write(C2b, gpio.LOW)
                        tmr.delay(500000)
                        gpio.write(C2a, gpio.HIGH)
                        gpio.write(C2b, gpio.HIGH)
                        elseif(_GET.pin == "ONF")then
                            gpio.write(C3a, gpio.LOW)
                            gpio.write(C3b, gpio.HIGH)
                            tmr.delay(500000)
                            gpio.write(C3a, gpio.HIGH)
                            gpio.write(C3b, gpio.HIGH)
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)

The script :

  • defines GPIO pin (DO, D1, D2, D3, D5, D6) as outputs and set motors off (level HIGH)
  • starts a web server listening for commands (UP, DOWN, LEFT, RIGHT, FIRE) from browser or android app.











Commenti

Post popolari in questo blog

Build the footswitch (part 2)

Build the footswitch (part 1)

Add more preset selections to Plug