처음‎ > ‎지식기반‎ > ‎

ESP8266

게시자: Sundew Shin, 2015. 2. 18. 오전 1:27   [ 2015. 2. 21. 오전 12:09에 업데이트됨 ]
[monkey.lua]
pin=3
port=88
hz=50 -- 50 Hz for MG90S (Tower Pro)
delay=200000 -- movement delay
pause=400000 -- pause between crash

pwm.setup(pin,hz,68)
pwm.start(pin)
tmr.delay(delay) -- 200ms
-- prepare button
gpio.write(4,gpio.LOW)
gpio.mode(4,gpio.INPUT,gpio.FLOAT)

function clapping(numClap)
  for i = 1, numClap, 1 do
    if gpio.read(4) == 1 then break end 
    pwm.setup(pin,hz,88)
    pwm.start(pin)
    tmr.delay(delay) -- 200ms
    pwm.setup(pin,hz,68)
    pwm.start(pin)
    tmr.delay(pause) -- 200ms
  end
  gpio.write(4,gpio.LOW)
  pwm.close(pin)
end

wifi.setmode(wifi.STATION)
wifi.sta.config("","")

srv=net.createServer(net.TCP) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
  --next row is for debugging output only
  print(payload)

  function ctrlpower()
    dotaz=string.sub(payload,kdesi[2]+1,#payload)
    print(dotaz)
    if dotaz~=nil then clapping(dotaz)end  
  end

  --parse position POST value from header
  kdesi={string.find(payload,"pwmi=")}
  --If POST value exist, set LED power
  if kdesi[2]~=nil then ctrlpower()end

  conn:send('HTTP/1.1 200 OK\n\n')
  conn:send('<!DOCTYPE HTML>\n')
  conn:send('<html>\n')
  conn:send('<head><meta  content="text/html; charset=utf-8">\n')
  conn:send('<title>Clock Monkey</title></head>\n')
  conn:send('<body><h1>Clock Monkey control</h1>\n')
  conn:send('<form action="" method="POST">\n')
  conn:send('<input type="submit" name="pwmi" value="1">\n')
  conn:send('<input type="submit" name="pwmi" value="2">\n')
  conn:send('<input type="submit" name="pwmi" value="3">\n')
  conn:send('<input type="submit" name="pwmi" value="4">\n')
  conn:send('</body></html>\n')
  conn:on("sent",function(conn) conn:close() end)
  end)
end)




[init.lua]
print("Attempting to obtain IP...")
tmr.alarm(1,5000, 1, function() 
    if wifi.sta.getip()==nil then 
        print("Connection Failed.") 
    else 
        print("Connected: "..wifi.sta.getip()) 
        tmr.stop(1) 
    -- further code could go here

conn=net.createConnection(net.TCP, 0) 
conn:on("connection",function(conn, payload)
            conn:send("HEAD / HTTP/1.1\r\n".. 
                      "Host: google.com\r\n"..
                      "Accept: */*\r\n"..
                      "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
                      "\r\n\r\n") 
            end)            
conn:on("receive", function(conn, payload)
    print(string.sub(payload,string.find(payload,"Date: ")
           +6,string.find(payload,"Date: ")+35))
    print('(retrieved in '..((tmr.now()-t)/1000)..' milliseconds from Google.)')
    conn:close()
    end) 
t = tmr.now()    
conn:connect(80,'google.com')

    end 
end)

 


[servor.lua]

pin=4
port=88
hz=50 -- 50 Hz for MG90S (Tower Pro)

srv=net.createServer(net.UDP)
srv:on("receive", function(srv, position)
    pos=tonumber(position)
    if(pos == nil) then pos=72 end
    if(pos<35) then pos=35 end
    if(pos>110) then pos=110 end
    -- cheap ones required to be adjusted 
    print("[35=+90 72=0 110=-90]:", pos)
    pwm.setup(pin,hz,pos)
    pwm.start(pin)
    tmr.delay(200000) -- 200ms
    pwm.close(pin) 
end)
srv:listen(port)

Comments