-->
Page 2 of 9

Re: remote control with only esp8266

PostPosted: Wed Nov 25, 2020 9:26 am
by maxniz
thanks for your time
i never use more files LUA... i always have used "init" i come from other language,sorry
i'm understanding !!
i have tried to insert your file without my code but i have this response..

<<NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4
Running
lua: wifi_station.lua:54: attempt to index field 'eventmon' (a nil value)

Hard Restart mercoledì 25 novembre 2020 15:15:25>>

line 54 is this :

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)


where is the problem ??
i'm waiting for the other parts while i look "deep" your code
thanks
Max

Re: remote control with only esp8266

PostPosted: Thu Nov 26, 2020 4:13 am
by quackmore
you are probably using an old build or you are missing some module
that code section is requested for managing the led when the client is connected to the AP
you can comment it out if you don't need it
but there is another module (http) I'm gonna use in today code
so I recommend you using an updated build

this is what I'm using
Code: Select allNodeMCU 3.0.0.0 built on nodemcu-build.com provided by frightanic.com
branch: release
commit: 4f6792773f93f36a7255cfd28dca7aa6c4aa9552
release: 3.0-release_20201107
release DTS: 202011071523
SSL: false
build type: integer
LFS: 0x0 bytes total capacity
modules: file,gpio,http,net,node,tmr,uart,wifi
build 2020-11-25 12:40 powered by Lua 5.1.4 on SDK 3.0.1-dev(fce080e)


you can use https://nodemcu-build.com/ to build it,
there is a section named "Select modules to include", just select the http module in addition to the standard ones (thank you nodemcu/nodemcu-firmware guys!!!!!)
you will get the binaries in your mail in 24h
flash it at 0x00000

now for the AP, checkout the code below,
give it a try
then customize it where you find "YOUR CODE HERE"

init.lua
Code: Select all-- init.lua

print("Running")

file.close("init.lua")
dofile("wifi_ap.lua")
-- application will run after ap config


credentials.lua
Code: Select allSSID="NODEMCU-1"
PASSWORD="node123456"
SVR_NET="192.168.10."
-- example
-- SVR_NET="192.168.10."
-- will set server address to 192.168.10.1
-- and DHCP starting adresses from 192.168.10.10
SVR_PORT=80


wifi_ap.lua
Code: Select all-- wifi_ap.lua
file.close("wifi_ap.lua")

dofile("credentials.lua")

print("configuring AP ...")

wifi.setmode(wifi.SOFTAP)
cfgSoftAP = {}
cfgSoftAP.ssid = SSID
cfgSoftAP.pwd = PASSWORD
wifi.ap.config(cfgSoftAP)
cfgIP = {}
cfgIP.ip = (SVR_NET.."1")
cfgIP.netmask = "255.255.255.0"
wifi.ap.setip(cfgIP)
dhcp_config = {}
dhcp_config.start = (SVR_NET.."10")
wifi.ap.dhcp.config(dhcp_config)
print("AP configured")

dofile("httpserver.lua")


httpserver.lua
Code: Select all-- httpserver.lua
http_error = function(conn)
    conn:send('HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: 81\r\nconnection: close\r\n\r\n{"error":"I\'m sorry, my responses are limited. You must ask the right question."}')
    conn:on("sent", function(conn)
        conn:close()
    end)
end

http_ok = function(conn)
    -- had to add extra \r\n cause HTTP module expects to always find body
    conn:send('HTTP/1.1 200 OK\r\n\r\n\r\n')
    conn:on("sent", function(conn)
        conn:close()
    end)
end

dofile("httroute.lua")

-- http server

srv = net.createServer(net.TCP)

print("starting the http server...")

srv:listen(SVR_PORT, function(conn)
    conn:on("receive", function(conn, payload)
        -- View the received data
        print("PAYLOAD ######################\n")
        print(payload)
        print("##############################")
        -- split header from content
        found = {}
        found = {string.find(payload, "\r\n\r\n")}
        if next(found) == nil then
            -- no content
            http_route(conn, payload, nil)
        else
            header = string.sub(payload, 1, (found[1] - 1))
            content = string.sub(payload, (found[2] + 1))
            http_route(conn, header, content)
        end
    end)
end)

print("http server started")


httproute.lua
Code: Select all-- httproute.lua
gpio.mode(4, gpio.OUTPUT)
gpio.write(4, gpio.HIGH)

http_route = function(conn, header, content)
    if string.match(header, "/led/turn/on") then
        -- known command, send acknowledge
        http_ok(conn)
        print("will turn the led on")
        -- turn on led
        -- YOUR CODE HERE
        gpio.write(4, gpio.LOW)
    elseif string.match(header, "/led/turn/off") then
        -- known command, send acknowledge
        http_ok(conn)
        print("will turn the led off")
        -- turn off led
        -- YOUR CODE HERE
        gpio.write(4, gpio.HIGH)
    else
        -- unknown command, send error
        http_error(conn)
        print("don't understand the command")
    end
end


and finally replace the following files into the client esp device
now the client will send a message to the ap (every 10 seconds)
for turning on/off the esp8266 led on D4 (the blue one)

enjoy and customize it for your purposes

credentials.lua
Code: Select allSSID="NODEMCU-1"
PASSWORD="node123456"


application.lua
Code: Select all-- application.lua
button_pressed = false
last_communicated_button_status = false

cmd_led_on = "http://192.168.10.1/led/turn/on"

periodic_task = function()
    -- custom application ...
    -- YOUR CODE HERE

    -- example begin

    if connected_to_AP and (button_pressed ~= last_commnicated_button_status) then
        if button_pressed then
            button_pressed = false
            print("sending command for turning led off")       
            cmd = "http://192.168.10.1/led/turn/off"
        else
            button_pressed = true
            print("sending command for turning led on")       
            cmd = "http://192.168.10.1/led/turn/on"
        end
        http.post(cmd, nil, nil, function(code, data)
          if (code < 0) then
             print("HTTP error, cannot communicate with AP")
             last_communicated_button_status = not(button_pressed)
          else
             print(code, data)
             last_communicated_button_status = button_pressed
          end
        end)
    end
    -- example end

    print("connected to AP " .. tostring(connected_to_AP))
    tmr.create():alarm(10000, tmr.ALARM_SINGLE, periodic_task)
end

print("now running the application")
print("connected to AP " .. tostring(connected_to_AP))
tmr.create():alarm(10000, tmr.ALARM_SINGLE, periodic_task)

Re: remote control with only esp8266

PostPosted: Fri Nov 27, 2020 8:14 am
by maxniz
while i'm waiting for the new NodeMCU 3.0.0.0 built on nodemcu-build.com , i'll study your new code
thank you again !!
Max

Re: remote control with only esp8266

PostPosted: Fri Nov 27, 2020 9:08 am
by quackmore
one last thing
checkout that your devices have the flash bigger than 512KB for the 3.0 build
otherwise you should go with 1.5.4.1-final (I mean in the nodemcu build site into "Select branch to build from" select 1.5.4.1-final (frozen, for 512KB flash))

in case you want to checkout the flash size with the currently installed version or with esptool.py have a look here https://stackoverflow.com/questions/39631011/how-to-determine-flash-size-of-nodemcu