I have two arduinos (An UNO R3 and a Mega 2560), and I need to communicate them via two ESP8266 modules to exchange some values.
In one arduino I'll use a SW-420 vibrarion module, and I need to send a value wirelessly to the other board, which it'll have an LCD screen that will display a message when a value is recieved.
How can I do that with that specific WiFi modules? Thanks in advance
-
You could have them both communicate with the same WiFi AP, or you could set one to be in SoftAP mode and connect the other to it. The choice is yours.Majenko– Majenko10/31/2015 11:03:39Commented Oct 31, 2015 at 11:03
-
@Majenko The second option looks way more attractive to me, can you share a tutorial of how to do it? Thanks.Daniel Corona– Daniel Corona11/01/2015 00:25:09Commented Nov 1, 2015 at 0:25
-
No, I can't. I never use the modules connected to an Arduino - I program them direct.Majenko– Majenko11/01/2015 00:39:38Commented Nov 1, 2015 at 0:39
1 Answer 1
Step 1: I would load NodeMCU on you ESP8266: https://github.com/nodemcu/nodemcu-firmware
Step 2: Connect to your home router or make an ap on one of your ESP and connect them... use the api: https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en over serial connection
Step 3: write a TCP server:
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload) //This payload will be send to your arduino over soft serial
conn:send("<h1> Hello, NodeMCU!!! </h1>")
end)
conn:on("sent",function(conn) conn:close() end)
end)
Step 4: write a TCP client:
suc = false;
function sendData( host, pathData )
suc = false;
print("GoSending!");
-- A simple http client
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
if(string.find(payload, "200 OK")~=nil) then
suc = true;
print("AllDone")
else
suc = false;
end
end)
conn:on("connection", function(c)
print("connected");
conn:send("GET /".. pathData .." HTTP/1.1\r\nHost: ".. host .."\r\n"
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
conn:on("disconnection", function()
if(suc==false) then
print("ErrorSending")
end
end)
conn:on("reconnection", function() print("reconnection") end )
conn:connect(80,host)
end
Step 5: Connect your esp boards with software serial to the arduino
Step 6: write (from board 1) to softserial to esp1: sendData( hostIP, yourData)
Step: 7 Parse the Data on sofserial read on the second arduino
Step 8: DONE!
EDIT:
Ok more infos on step 6. If you got your script running on your esp, you can call functions by sending the function name with parameters over your serial connection. So, this is some example code for your ardoino if you got the script from above:
SoftwareSerial wifi(3, 2); // RX, TX
//at some time in your programm
//Build up your string that will be send over serial
String i1 = "sendData(\"ipFromTheServer\", \"send.php?d=";
String i2 = i1+dataToSend;
String i3 = i2 +"\");";
wifi.println(i3);
This will call the sendData function on you esp. this will call the send.php on a Server... you can put a server up on an other esp and call it (dont have to be a php page)
More infos on step 7. the script on top will always print all incoming data. So you just have to read serial data on wifi:
if (wifi.available())
Serial.write(wifi.read()); //this will print all inc data to your arduino console (serial) parse this data!
Info: I've build this on my own... dont know if this is best practice :)
-
Thanks for your answer!! Can you provide some more info about steps 6 and 7? ThanksDaniel Corona– Daniel Corona11/14/2015 04:31:26Commented Nov 14, 2015 at 4:31
-
Add edit part. Hope this will help you :)Cracker0dks– Cracker0dks11/15/2015 16:23:55Commented Nov 15, 2015 at 16:23
-
I'll send you a link with all the code so you can check it out :) Link of the codes of my projectDaniel Corona– Daniel Corona11/16/2015 07:06:28Commented Nov 16, 2015 at 7:06
-
ok I have made a google docs: docs.google.com/document/d/… I can not test this code. If you got problems, just change the google docs and mark your part.Cracker0dks– Cracker0dks11/16/2015 14:44:43Commented Nov 16, 2015 at 14:44
Explore related questions
See similar questions with these tags.