Station AP client / server messaging
Posted: Fri Jan 16, 2015 8:28 pm
Hi,
I was wondering if it is possible to use the ESP8266 as and AP, have multiple clients (laptops) connect to this AP, i.e 192.168.4.2 and 192.168.4.3, and have the AP send messages to the two clients, outside of the conn:on recieve callback.
What I have tried below is basically setting up the AP, creating a server and listening for any connections from the clients, i.e 192.168.4.2. The client sends a simple message '0' which the script can interpret to call a specific function. This function can then perform some task and if necessary (and it is) connect to the client and send a message back.
I relatively new to this area and sockets, servers and clients, and where I can successfully call the function DoSomething, and the callback returns 'Recieved' to my python script, i don't recieve the send for 'Step 1'. Maybe I'm doing the impossible, communicating through the same port at the same time when I shouldn't be? I'm not sure so any help is appreciated.
I was wondering if it is possible to use the ESP8266 as and AP, have multiple clients (laptops) connect to this AP, i.e 192.168.4.2 and 192.168.4.3, and have the AP send messages to the two clients, outside of the conn:on recieve callback.
What I have tried below is basically setting up the AP, creating a server and listening for any connections from the clients, i.e 192.168.4.2. The client sends a simple message '0' which the script can interpret to call a specific function. This function can then perform some task and if necessary (and it is) connect to the client and send a message back.
I relatively new to this area and sockets, servers and clients, and where I can successfully call the function DoSomething, and the callback returns 'Recieved' to my python script, i don't recieve the send for 'Step 1'. Maybe I'm doing the impossible, communicating through the same port at the same time when I shouldn't be? I'm not sure so any help is appreciated.
Code: Select all
-- Configure access point
wifi.setmode(wifi.STATIONAP)
-- Declare configuration variable
cfg={}
cfg.ssid="SSID"
cfg.pwd="password"
-- Pass to access point and configure
wifi.ap.config(cfg)
-- Get current internal state
-- Load internal server to handle all communications
-- Check if server is available or create
if srv == nil then
-- Create server
srv=net.createServer(net.TCP)
-- a simple http server
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
-- Print payload message
print(payload)
-- Call task function and pass payload
local output = Task(payload)
-- Send back any response to caller or all clients
conn:send(output)
end)
end)
end
-- Depending on payload call certain functions
function Task (command)
-- Declare variable to hold output
local output = ""
-- Select task
if command == "0" then
-- Do something '0'
output = DoSomething()
end
-- Return output
return output
end
function DoSomething ()
-- Command clients to do something
print("Step 1")
sck = net.createConnection(net.TCP, false)
sck:connect(80,"192.168.4.2")
sck:send("Step 1")
-- Sleep
tmr.delay(1000)
-- Return result
return 'Recieved'
end