How can I connect to some esp8266 each order.
Posted: Sat Jul 30, 2016 8:27 pm
Hi!
I have a problem in my project.
I tried to connect Smart Phone Hotspot with StationAp mode ESP8266 and station mode ESP8266.
This is stationAp mode ESP8266.
This is station ESP8266 code
I have a problem in my project.
I tried to connect Smart Phone Hotspot with StationAp mode ESP8266 and station mode ESP8266.
This is stationAp mode ESP8266.
Code: Select all
--init.lua
dofile("stationAp.lua")
dofile("webServer.lua")
Code: Select all
--stationAp.lua
print("Ready to start soft ap AND station")
local str=wifi.ap.getmac();
local ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17));
wifi.setmode(wifi.STATIONAP)
local cfg={}
cfg.ssid="ESP8266_"..ssidTemp;
cfg.pwd="1111111111"
wifi.ap.config(cfg)
cfg={}
cfg.ip="192.168.10.1";
cfg.netmask="255.255.255.0";
cfg.gateway="172.20.10.1";
wifi.ap.setip(cfg);
wifi.sta.config("iPhone","sda342342")
wifi.sta.connect()
local cnt = 0
gpio.mode(0,gpio.OUTPUT);
tmr.alarm(0, 1000, 1, function()
if (wifi.sta.getip() == nil) and (cnt < 20) then
print("Trying Connect to Router, Waiting...")
cnt = cnt + 1
if cnt%2==1 then gpio.write(0,gpio.LOW);
else gpio.write(0,gpio.HIGH); end
else
tmr.stop(0);
print("Soft AP started")
print("Heep:(bytes)"..node.heap());
print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());
if (cnt < 20) then print("Conected to Router\r\nMAC:"..wifi.sta.getmac().."\r\nIP:"..wifi.sta.getip())
else print("Conected to Router Timeout")
end
gpio.write(0,gpio.LOW);
cnt = nil;cfg=nil;str=nil;ssidTemp=nil;
collectgarbage()
end
end)
Code: Select all
--webServer.lua
-- a simple HTTP server
if srv~=nil then
srv:close() --close the Server
end
pin = 5 -- dht11 signal pin
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
CDS= adc.read(0)
if status == dht.OK then
temp_1 = math.floor(temp)
humi_1 = math.floor(humi)
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
tmr.delay(1000000)
conn:on("receive", function(sck, payload)
print(payload)
sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Temperature & Humidity<br/></h1>" .. "<br/><h1>Temperature = " .. temp .. "C</hr>" .. "<br/><h1>Humidity = " .. humi .. "%</hr></br><h1>CDS=" .. CDS .. "</hr>")
end)
conn:on("sent", function(sck) sck:close() end)
print("CDS="..CDS)
tmr.delay(1000000)
end)
print("webserver ready...")
This is station ESP8266 code
Code: Select all
--init.lua
dofile("aaa.lua")
dofile("webServer.lua")
Code: Select all
--aaa.lua
function connect()
if(wifi.sta.status()==5)then
socket=net.createConnection(net.TCP, 0)
socket:connect(80,"192.168.10.2")
socket:on("receive",function(sck, c) print(c) end)
socket:send("hello world!!")
end
end
local pin=7
gpio.mode(pin,gpio.OUTPUT)
wifi.setmode(wifi.STATION)
if(wifi.getmode()==1)then
print("connecting station mode")
wifi.sta.config("ESP8266_ce9d34","1111111111")
wifi.sta.connect()
tmr.alarm(0, 5000, 0, function()
if(wifi.sta.getip()~=nil)then
print(wifi.sta.getip())
gpio.write(pin,gpio.HIGH)
print(wifi.sta.status())
connect()
else
print(tmr.now())
print("AP not exiest!!")
end
end )
if(wifi.sta.getip()==nil)then
print(wifi.sta.status())
end
else
print(wifi.sta.status())
end
Code: Select all
--webServer.lua
-- a simple HTTP server
if srv~=nil then
srv:close() --close the Server
end
pin = 5 -- dht11 signal pin
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
CDS= adc.read(0)
if status == dht.OK then
temp_1 = math.floor(temp)
humi_1 = math.floor(humi)
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
tmr.delay(1000000)
conn:on("receive", function(sck, payload)
print(payload)
sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>My House Temperature & Humidity<br/></h1>" .. "<br/><h1>Temperature = " .. temp .. "C</hr>" .. "<br/><h1>Humidity = " .. humi .. "%</hr></br><h1>CDS=" .. CDS .. "</hr>")
end)
conn:on("sent", function(sck) sck:close() end)
print("CDS="..CDS)
tmr.delay(1000000)
end)
print("webserver ready...")