Saturday, June 11, 2016

ESP8266 PWM Android Controller

Control ESP8266 PWM outputs* with Android.

Android app: play.google/wifipwm
Android src: github/esp_wifi_pwm

demo:


ESP8266 init.lua :
wifi.setmode(wifi.STATION)
wifi.sta.config("WIFI_SSID","WIFI_PASSWORD")

local GP0 = 3
local GP2 = 4
local port = 3456

print(wifi.sta.getip())

srv=net.createServer(net.UDP)
srv:on("receive", function(cli, rcv)
    --print(rcv)
    rx={}
    for i, s in string.gmatch(rcv, "(%w+)=(%w+)") do
      rx[i]=s
      --print(i, rx[i])
    end
    if rx.d0 then
      pwm.setduty(GP0,rx.d0)
      --print("GP0 duty",rx.d0)
    elseif rx.d2 then
      pwm.setduty(GP2,rx.d2)
      --print("GP2 duty",rx.d2)
    elseif rx.f0 and rx.c0 then
      pwm.setup(GP0,rx.f0,rx.c0)
      pwm.start(GP0)
      print("GP0 freq",rx.f0,"cycles", rx.c0)
    elseif rx.f2 and rx.c2 then
      pwm.setup(GP2,rx.f2,rx.c2)
      pwm.start(GP2)
      print("GP2 freq",rx.f2,"cycles", rx.c2)
    end
    cli:send(rcv)
  end)
srv:listen(port)


*PWM outputs nodemcu pwm
  • pins GP0 and GP2
  • up to 1kHz only
  • 0-100%duty (note: 1023 cycles = 100% duty)