ESP8266-EVB NodeMCU Active Relay GPIO Issue

Started by brucedav, October 13, 2015, 10:34:43 pm

Previous topic - Next topic

brucedav

Hey Guys,  I'm running NodeMCU on my ESP8226-EVB and I can't seem to active the Relay on GPIO 5.  Any ideas?


outpin=5

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()
    pwm.close(outpin)
    gpio.mode(outpin,gpio.OUTPUT)
    dotaz=string.sub(payload,kdesi[2]+1,#payload)
    if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH)return end
    if dotaz=="OFF" then gpio.write(outpin,gpio.LOW)return end
    if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin)return end
    pwm.setup(outpin,1000,dotaz*10)
    pwm.start(outpin)
    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>ESP8266</title></head>\n')
    conn:send('<body><h1>Sample GPIO output control</h1>\n')
    conn:send('<h3>For Lua NodeMcu 0.9.2 build 20141219 only !!!</h3>\n')
   conn:send('<h3>Tested with IE 11. and Chrome 39.</h3>\n')
    conn:send('<IMG SRC="http://esp8266.fancon.cz/common/led.gif" WIDTH="150" HEIGHT="101" BORDER="1"><br><br>\n')
    conn:send('<form action="" method="POST">\n')
    conn:send('<input type="submit" name="pwmi" value="OFF">\n')
    conn:send('<input type="submit" name="pwmi" value="10">\n')
    conn:send('<input type="submit" name="pwmi" value="20">\n')
    conn:send('<input type="submit" name="pwmi" value="30">\n')
    conn:send('<input type="submit" name="pwmi" value="40">\n')
    conn:send('<input type="submit" name="pwmi" value="50">\n')
    conn:send('<input type="submit" name="pwmi" value="60">\n')
    conn:send('<input type="submit" name="pwmi" value="70">\n')
    conn:send('<input type="submit" name="pwmi" value="80">\n')
    conn:send('<input type="submit" name="pwmi" value="90">\n')
    conn:send('<input type="submit" name="pwmi" value="ON"> % of power<br><br>\n')
    conn:send('<input type="submit" name="pwmi" value="FLC"> HW blinker</form>\n')
    conn:send('</body></html>\n')
    conn:on("sent",function(conn) conn:close() end)
    end)
end)

brucedav

I've included below the full init.lua code (with some redaction of course, haha) -->


-- Begin WiFi configuration

local wifiConfig = {}

-- wifi.STATION         -- station: join a WiFi network
-- wifi.SOFTAP          -- access point: create a WiFi network
-- wifi.wifi.STATIONAP  -- both station and access point
wifiConfig.mode = wifi.STATIONAP  -- both station and access point

wifiConfig.accessPointConfig = {}
wifiConfig.accessPointConfig.ssid = "xxxxx"   -- Name of the SSID you want to create
wifiConfig.accessPointConfig.pwd = "xxxxx"    -- WiFi password - at least 8 characters

wifiConfig.accessPointIpConfig = {}
wifiConfig.accessPointIpConfig.ip = "192.168.111.1"
wifiConfig.accessPointIpConfig.netmask = "255.255.255.0"
wifiConfig.accessPointIpConfig.gateway = "192.168.111.1"

wifiConfig.stationPointConfig = {}
wifiConfig.stationPointConfig.ssid = "xxxxx"        -- Name of the WiFi network you want to join
wifiConfig.stationPointConfig.pwd =  "xxxxx"                -- Password for the WiFi network

-- Tell the chip to connect to the access point

wifi.setmode(wifiConfig.mode)
print('set (mode='..wifi.getmode()..')')

if (wifiConfig.mode == wifi.SOFTAP) or (wifiConfig.mode == wifi.STATIONAP) then
    print('AP MAC: ',wifi.ap.getmac())
    wifi.ap.config(wifiConfig.accessPointConfig)
    wifi.ap.setip(wifiConfig.accessPointIpConfig)
end
if (wifiConfig.mode == wifi.STATION) or (wifiConfig.mode == wifi.STATIONAP) then
    print('Client MAC: ',wifi.sta.getmac())
    wifi.sta.config(wifiConfig.stationPointConfig.ssid, wifiConfig.stationPointConfig.pwd, 1)
end

print('chip: ',node.chipid())
print('heap: ',node.heap())

wifiConfig = nil
collectgarbage()

-- Connect to the WiFi access point.
-- Once the device is connected, you may start the HTTP server.

if (wifi.getmode() == wifi.STATION) or (wifi.getmode() == wifi.STATIONAP) then
    local joinCounter = 0
    local joinMaxAttempts = 5
    tmr.alarm(0, 3000, 1, function()
       local ip = wifi.sta.getip()
       if ip == nil and joinCounter < joinMaxAttempts then
          print('Connecting to WiFi Access Point ...')
          joinCounter = joinCounter +1
       else
          if joinCounter == joinMaxAttempts then
             print('Failed to connect to WiFi Access Point.')
          else
             print('IP: ',ip)
          end
          tmr.stop(0)
          joinCounter = nil
          joinMaxAttempts = nil
          collectgarbage()
       end
    end)
end

outpin=5

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()
    pwm.close(outpin)
    gpio.mode(outpin,gpio.OUTPUT)
    dotaz=string.sub(payload,kdesi[2]+1,#payload)
    if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH)return end
    if dotaz=="OFF" then gpio.write(outpin,gpio.LOW)return end
    if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin)return end
    pwm.setup(outpin,1000,dotaz*10)
    pwm.start(outpin)
    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>ESP8266</title></head>\n')
    conn:send('<body><h1>Sample GPIO output control</h1>\n')
    conn:send('<h3>For Lua NodeMcu 0.9.2 build 20141219 only !!!</h3>\n')
   conn:send('<h3>Tested with IE 11. and Chrome 39.</h3>\n')
    conn:send('<IMG SRC="http://esp8266.fancon.cz/common/led.gif" WIDTH="150" HEIGHT="101" BORDER="1"><br><br>\n')
    conn:send('<form action="" method="POST">\n')
    conn:send('<input type="submit" name="pwmi" value="OFF">\n')
    conn:send('<input type="submit" name="pwmi" value="10">\n')
    conn:send('<input type="submit" name="pwmi" value="20">\n')
    conn:send('<input type="submit" name="pwmi" value="30">\n')
    conn:send('<input type="submit" name="pwmi" value="40">\n')
    conn:send('<input type="submit" name="pwmi" value="50">\n')
    conn:send('<input type="submit" name="pwmi" value="60">\n')
    conn:send('<input type="submit" name="pwmi" value="70">\n')
    conn:send('<input type="submit" name="pwmi" value="80">\n')
    conn:send('<input type="submit" name="pwmi" value="90">\n')
    conn:send('<input type="submit" name="pwmi" value="ON"> % of power<br><br>\n')
    conn:send('<input type="submit" name="pwmi" value="FLC"> HW blinker</form>\n')
    conn:send('</body></html>\n')
    conn:on("sent",function(conn) conn:close() end)
    end)
end)

ohorn

You forgot to convert the GPIO# to the NodeMCU pin#.
see here: https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#new_gpio_map

So: you should use outpin=1 in your code as the relais is connected to GPIO5 -> NodeMCU D1 -> pin# 1

Cheers