-->
Page 1 of 4

Communication between two ESP8266

PostPosted: Fri Mar 06, 2015 10:31 am
by archivi
Hi

For my current project i need stable communication between two ESP8266 modules. Basicly i want to send every second or maybe faster measurements data from remote location.

I've already done some coding but its not working as it should be, beacause after a while server module is not receiving any data at all. It worked maybe 5 minutes.

Server side:
Code: Select all-- Konfiguracja stacji
print("Configuring Weather Station AP\n")
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="WSServer"
cfg.pwd="12345678"
wifi.ap.config(cfg)
print("Server running: \nSSID:",cfg.ssid,"\npwd:",cfg.pwd)
print("MAC:",wifi.ap.getmac())
print("IP:",wifi.ap.getip())

print("\nConfiguring TCP Server\n")
server = net.createServer(net.TCP)
server:listen(80, function(conn)
    conn:on("receive", function(conn, receivedData)
        print("received: " .. receivedData)         
        message = "Hi, I read You."
        conn:send(message); message = nil
    end)
    conn:on("sent", function(conn)
      collectgarbage()
    end)
end)
print("\nDone!\n")


Client side:
Code: Select all-- CODE FOR CLIENT ESP
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
SSID="WSServer"
PSW="12345678"
wifi.sta.config(SSID,PSW) -- connecting to server
wifi.sta.connect()
print("Waiting for connection")
tmr.alarm(1,1000,1,function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected!")
          print("SSID:",SSID);
          print("IP:",wifi.sta.getip())
          print("MAC:",wifi.sta.getmac())
          sk=net.createConnection(net.TCP, 0)
          sk:connect(80,"192.168.4.1")
         
      else
      print("...")
      end
end)

function send(data)
sk:send(data)
end


Any thoughts are welcome.

Re: Communication between two ESP8266

PostPosted: Fri Mar 13, 2015 4:24 pm
by archivi
I've managed to get this working. It's working very well but i have not tested it on longer ranges then few meters though.

I have no idea why but even if I set timeout to 28800 on server and send data every second, the client disconnects from server for no reason after some time. Therefore I've written simple function that reconnect's automatically after the connection is lost.

So, here's the code. Maybe someone will find it useful for his own project :)

SERVER:
Code: Select allprint("configuring ap")

wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="WS"
cfg.pwd="12345678"
wifi.ap.config(cfg)

print("config TCP server")
server = net.createServer(net.TCP,28800)
server:listen(80, function(conn)
     conn:on("receive",function(conn,data)
          print(data)
         
     end)
     conn:on("sent",function(conn)
          collectgarbage()
     end)
          conn:on("connection",function(conn)
          print("Connected")
     end)
end)
print("done")



CLIENT:
Code: Select all--------- SETTINGS ---------
SSID="WS"
PWD="12345678"
----------------------------
---------- VARIABLES --------
conn_flag = 0
--------- FUNCTIONS --------
function connect_wifi(ssid,pwd)
if (conn_flag == 0) then
     print("Waiting for connection...")
     print("#1")
else
     print("Reconnecting...")
     print("#1")
end
timer1=0
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pwd)
wifi.sta.connect()
tmr.alarm(1,1000,1,function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected to:", SSID)
          print("IP:",wifi.sta.getip())
          print("MAC:",wifi.sta.getmac())
          conn_flag = 1
     elseif(timer1==10) then
          print("Unable to connect to:",SSID)
          tmr.stop(1)
     end
end)
end

function connect_tcp()
tmr.alarm(2,1000,1,function() 
        sk = net.createConnection(net.TCP, 0) 
        sk:on("receive", function(sk, receivedData)
            print("received: " .. receivedData)
        end)
        sk:on("connection", function(sk)
               print("Connected!")
               print("#2")
               tmr.stop(2)
        end )
        sk:on("disconnection", function(sk)
               print("Disconnected!")
               print("#0")
               connect_wifi(SSID,PWD)
               connect_tcp()
        end )
        sk:connect(80, "192.168.4.1") -- server ESP IP
    collectgarbage()
end)
end
function send(data)
sk:send(data)
end

----------------------------

connect_wifi(SSID,PWD)
connect_tcp()



To send data to server I'm using atmega8 like this:

Code: Select allvoid send_wifis(char *s){
   uart_puts("send(\"");
   uart_puts(s);
   uart_puts("\")\n\r");

}

void send_wifilong(uint32_t liczba){
   uart_puts("send(\"");
   uart_putlong(liczba,10);
   uart_puts("\")\n\r");
}



Code: Select allprint("#2")
I'm using # to send information to atmega about the current connection status so you may skip this one.

Thanks for the great firmware :)

Re: Communication between two ESP8266

PostPosted: Mon Mar 16, 2015 10:24 pm
by swilson
I was just thinking about this. A serial-wifi-wifi-serial connection might come in handy sometimes for certain things. Did you solve the disconnect problem yet?

Re: Communication between two ESP8266

PostPosted: Wed May 06, 2015 3:51 am
by ashwinispatil
archivi wrote:I've managed to get this working. It's working very well but i have not tested it on longer ranges then few meters though.

I have no idea why but even if I set timeout to 28800 on server and send data every second, the client disconnects from server for no reason after some time. Therefore I've written simple function that reconnect's automatically after the connection is lost.

So, here's the code. Maybe someone will find it useful for his own project :)

SERVER:
Code: Select allprint("configuring ap")

wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="WS"
cfg.pwd="12345678"
wifi.ap.config(cfg)

print("config TCP server")
server = net.createServer(net.TCP,28800)
server:listen(80, function(conn)
     conn:on("receive",function(conn,data)
          print(data)
         
     end)
     conn:on("sent",function(conn)
          collectgarbage()
     end)
          conn:on("connection",function(conn)
          print("Connected")
     end)
end)
print("done")



CLIENT:
Code: Select all--------- SETTINGS ---------
SSID="WS"
PWD="12345678"
----------------------------
---------- VARIABLES --------
conn_flag = 0
--------- FUNCTIONS --------
function connect_wifi(ssid,pwd)
if (conn_flag == 0) then
     print("Waiting for connection...")
     print("#1")
else
     print("Reconnecting...")
     print("#1")
end
timer1=0
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pwd)
wifi.sta.connect()
tmr.alarm(1,1000,1,function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected to:", SSID)
          print("IP:",wifi.sta.getip())
          print("MAC:",wifi.sta.getmac())
          conn_flag = 1
     elseif(timer1==10) then
          print("Unable to connect to:",SSID)
          tmr.stop(1)
     end
end)
end

function connect_tcp()
tmr.alarm(2,1000,1,function() 
        sk = net.createConnection(net.TCP, 0) 
        sk:on("receive", function(sk, receivedData)
            print("received: " .. receivedData)
        end)
        sk:on("connection", function(sk)
               print("Connected!")
               print("#2")
               tmr.stop(2)
        end )
        sk:on("disconnection", function(sk)
               print("Disconnected!")
               print("#0")
               connect_wifi(SSID,PWD)
               connect_tcp()
        end )
        sk:connect(80, "192.168.4.1") -- server ESP IP
    collectgarbage()
end)
end
function send(data)
sk:send(data)
end

----------------------------

connect_wifi(SSID,PWD)
connect_tcp()



To send data to server I'm using atmega8 like this:

Code: Select allvoid send_wifis(char *s){
   uart_puts("send(\"");
   uart_puts(s);
   uart_puts("\")\n\r");

}

void send_wifilong(uint32_t liczba){
   uart_puts("send(\"");
   uart_putlong(liczba,10);
   uart_puts("\")\n\r");
}



Code: Select allprint("#2")
I'm using # to send information to atmega about the current connection status so you may skip this one.

Thanks for the great firmware :)


hey I am doing the same thing I can establish communication between 2 esps but I want to upload I file from esp client to server.
How to upload a file in nodemcu firmware using lua. please help me out