Bryan
Published © GPL3+

Control Your RC Car With Your Android Tablet

Control your RC car with your tablet just for fun.

BeginnerShowcase (no instructions)3 hours5,014
Control Your RC Car With Your Android Tablet

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Roboremo
NodeMCU firmware
NodeMCU firmware

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Circuit diagrams

Code

LUA code

Lua
-------------------------------
-- wifi hot spot config
wifi.setmode(wifi.SOFTAP)
cfg={}
cfg.ssid="mywifi"
cfg.pwd="qwerty123"
cfg.ip="192.168.0.1"
cfg.netmask="255.255.255.0"
cfg.gateway="192.168.0.1"
port = 9876
wifi.ap.setip(cfg)
wifi.ap.config(cfg) 
-------------------------------
  
function stringStarts(a,b)
    return string.sub(a,1,string.len(b))==b
end

function stringEnds(a,b)
   return b=='' or string.sub(a,-string.len(b))==b
end

-------------------------------
-- servo config
servo = {}
servo.pin = 2 --this is GPIO0
servo.value = 75
servo.id = "servo"
-------------------------------

-------------------------------
-- ESC config
esc = {}
esc.pin = 3 --this is GPIO0
esc.value = 75
esc.id = "esc"
-------------------------------

-------------------------------
-- LEDs config
ledcon = {}
ledcon.pin = 1 --this is GPIO0

ledon = {}
ledon.pin = 4
-------------------------------

cmd = ""

-------------------------------
-- PWM setup 50 Hz for servo and ESC
pwm.setup(servo.pin, 50, servo.value) 
pwm.start(servo.pin)
pwm.setup(esc.pin, 50, esc.value) 
pwm.start(esc.pin)
-------------------------------

-------------------------------
-- LEDs setup
gpio.mode(ledcon.pin,gpio.OUTPUT)
gpio.mode(ledon.pin,gpio.OUTPUT)
gpio.write(ledon.pin,gpio.HIGH)
-------------------------------

function exeCmd(st)
    if stringStarts(st, servo.id.." ") then -- value comes after id + space
        -- servo command
        servo.value = tonumber( string.sub(st,1+string.len(servo.id.." "),string.len(st)) )
        pwm.setduty(servo.pin, servo.value)
    elseif stringStarts(st, esc.id.." ") then -- value comes after id + space
    	-- ESC command
        esc.value = tonumber( string.sub(st,1+string.len(esc.id.." "),string.len(st)) )
        pwm.setduty(esc.pin, esc.value)
    end    
end

-------------------------------
-- Analyse the TCP frame
function receiveData(conn, data)
    cmd = cmd .. data

    local a, b = string.find(cmd, "\n", 1, true)   
    while a do
        exeCmd( string.sub(cmd, 1, a-1) )
        cmd = string.sub(cmd, a+1, string.len(cmd))
        a, b = string.find(cmd, "\n", 1, true)
    end 
end

-------------------------------
-- TCP server creation
srv=net.createServer(net.TCP, 28800) 
srv:listen(port,function(conn)
    gpio.write(ledcon.pin,gpio.HIGH)
    conn:send("dbg connected ok\n")
     
    conn:on("receive",receiveData)  
    
    conn:on("disconnection",function(c) 
        gpio.write(ledcon.pin,gpio.LOW)
    end)
    
end)

Credits

Bryan

Bryan

2 projects • 2 followers
architecte IT

Comments