-->
Page 1 of 9

remote control with only esp8266

PostPosted: Sun Nov 22, 2020 11:30 am
by maxniz
hi, i have programmed two ESP8266-1 in LUA ..one as AP and the other as a client.
now i want create a simple remote controller ,
so when i push a button (gpio 2)
on the AP, i want to turn on a led(gpio 2) on the client .
now they are connected but i don't' know how to program this....
anybody can help me ??
tnx
Max

Re: remote control with only esp8266

PostPosted: Tue Nov 24, 2020 10:37 am
by quackmore
check out these resources:

reading digital input

reading digital input with interrupts

sending messages using http

receiving messages using http

setting digital output

you'll find full examples but you'll have to customize them for your project

if you can choose you setup I'd prefer using the client for reading the button and the AP for managing the led

the client will periodically read the input (or will detect changes using interrupts)
the client will then signal input changes sending http messages to the AP
the AP will check received messages and will manage the led accordingly

Re: remote control with only esp8266

PostPosted: Tue Nov 24, 2020 2:37 pm
by maxniz
tnx,
"if you can choose you setup I'd prefer using the client for reading the button and the AP for managing the led"
is the same .. i can exchange them ... but i have work in a site without internet .... i have to push a button on une of them and turn on a led (relay) on the other side... i gave a look on what you have sent but i'm not able to write the program.. now I look at the address assignment for turn on the led

this is the client lua code

print("ESP8266 Client")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("prova","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")

rele1 = 4
rele2 = 3
gpio.mode(rele1, gpio.OUTPUT)

tmr.alarm(1, 10, 1, function()
if(wifi.sta.getip()~=nil) then

gpio.write(rele1, gpio.LOW);
else
gpio.write(rele1, gpio.HIGH);

end
end)


but i want a led always on when connect and another one for the button pushed
have any sample ??? i also think to use the uart.....when i press the button ,i send something on the other side....GPIO2... so,with a simple bjt ,i could turn on someting
have you any idea ???

tnx
Max

Re: remote control with only esp8266

PostPosted: Wed Nov 25, 2020 7:03 am
by quackmore
got it

I think I can spend some more time here, just because lua is so fun...

it will take more than one post to get it done

today will focus on the client,
checkout the code below,
give it a try
then customize it where you find "YOUR CODE HERE"

and have a look to the docs NodeMCU docs
cause I copied most of the code from there

the code will be split into four files just to keep different things separated

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

print("Running")
file.close("init.lua")
dofile("wifi_station.lua")
dofile("application.lua")


credentials.lua
Code: Select all-- credentials.lua
SSID="your-ssid"
PASSWORD="your-password"


wifi_station.lua
Code: Select all-- wifi_station.lua
-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")

connected_to_AP = false

-- Define WiFi station event callbacks
wifi_connect_event = function(T)
  print("Connection to AP("..T.SSID..") established!")
  print("Waiting for IP address...")
  if disconnect_ct ~= nil then
    disconnect_ct = nil
  end
end

wifi_got_ip_event = function(T)
  print("Wifi connection is ready! IP address is: "..T.IP)
  connected_to_AP = true
  -- turn on the connection led ...
  -- YOUR CODE HERE
end

wifi_disconnect_event = function(T)
  if T.reason == wifi.eventmon.reason.ASSOC_LEAVE then
    --the station has disassociated from a previously connected AP
    return
  end

  connected_to_AP = false

  print("\nWiFi connection to AP("..T.SSID..") has failed!")

  -- turn off the connection led ...
  -- YOUR CODE HERE

  --There are many possible disconnect reasons, the following iterates through
  --the list and returns the string corresponding to the disconnect reason.
  for key,val in pairs(wifi.eventmon.reason) do
    if val == T.reason then
      print("Disconnect reason: "..val.."("..key..")")
      break
    end
  end

  if disconnect_ct == nil then
    disconnect_ct = 1
  else
    disconnect_ct = disconnect_ct + 1
  end
  print("Retrying connection...(attempt "..(disconnect_ct+1)..")")
end

-- Register WiFi Station event callbacks
wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, wifi_connect_event)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, wifi_disconnect_event)

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config({ssid=SSID, pwd=PASSWORD})
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default


application.lua
Code: Select all-- application.lua

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

  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)