I have two Arduino boards and two ESP8266 modules with the NodeMCU firmware, and I want to make this: One Aruino will be connected to one module and a vibration sensor, and when this sensor detects some vibration, it will send some data using the ESP8266 module as a server, then, the other Arduino will recieve the data with the other ESP module and display a message to a LCD screen.
I've already programmed the WiFi modules as a server and as a client using NodeMCU firmware and LuaUploader. The issue I have is that I don't know how to make a program in the Arduino IDE that communicate both Arduino boards using the ip adress assigned to both ESP modules, can you help me? Thanks
3 Answers 3
First, you need a SoftwareSerial interface, since the hardware serial is already used for debugging. To setup a SoftwareSerial this is an example:
#include <SoftwareSerial.h>
const byte rxPin = 6; // Wire this to Tx Pin of ESP8266
const byte txPin = 7; // Wire this to Rx Pin of ESP8266
// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);
void setup() {
Serial.begin(115200);
ESP8266.begin(115200); // Change this to the baudrate used by ESP8266
delay(1000); // Let the module self-initialize
}
void loop() {
while (ESP8266.available()){
String inData = ESP8266.readStringUntil('\n');
Serial.println("Got Data from ESP8266: " + inData);
// The rest of your code...
}
}
To send data from ESP8266 back to Arduino try something like this:
uart.setup(0, 115200, 8, 0, 1, 1 ) -- Change the baudrate to 115200
-- A simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
payload = payload + "\n"
uart.write(0, payload) -- send it back to Arduino via Serial interface
end)
conn:on("sent",function(conn) conn:close() end)
end)
uart.on("data", function(data)
-- When ESP8266 receives data from Arduino, it will trigger this event
end, 0)
-
And how can I send a data through the Arduino to the ESP8266?Daniel Corona– Daniel Corona2015年11月16日 03:22:48 +00:00Commented Nov 16, 2015 at 3:22
-
I'll send you a link with my code for both Arduino boards and both ESP modules, hope you can help me Codes for my projectDaniel Corona– Daniel Corona2015年11月16日 07:09:08 +00:00Commented Nov 16, 2015 at 7:09
-
Hello, I'll have a look at the codes and get back to you.Ikbel– Ikbel2015年11月16日 09:42:32 +00:00Commented Nov 16, 2015 at 9:42
You have the esp8266 talking. Now you need to come up with a method of passing data from the arduinos to the esp.
You can use serial, you can use i2c, spi? Or something of your own design. The problem wit using serial is the you are already using serial to communicate with the board from your PC. You can use SoftSerial on the Arduino, but not sure how you'd do it on the ESP.
There are ways of making both boards work as master and slave i2c and spi, but it's probably overkill.
You could use two pins, one pin sends a binary bit and the other to clock the data, set high when new data has been written and set low when it has been read.
Then just use the same sort of algorithm as you did between you eps.
I have assumed that one Arduino is connected to one esp. Let the ESPs worry about ip addresses. If you want the Arduino to specify the destination then use a simpler addressing system than IP and let the ESPs translate that.
-
Well, can I connect the RX and TX pins to another Arduino digital pins so I can use the SoftSerial? @MattDaniel Corona– Daniel Corona2015年11月15日 08:15:18 +00:00Commented Nov 15, 2015 at 8:15
-
Also, I programmed the NodeMCU server like this
srv:listen(80,function(conn) conn:on("receive",function(conn,payload) print(payload) conn:send("<h1> Hello, NodeMCU!!! </h1>") end) conn:on("sent",function(conn) conn:close() end) end)
Daniel Corona– Daniel Corona2015年11月15日 08:58:31 +00:00Commented Nov 15, 2015 at 8:58 -
You can use soft serial, but make sure you understand its limitations. The NodeMCU code just needs a tweak to store the data received from the serial and send it when someone connects to the http socket.Code Gorilla– Code Gorilla2015年11月16日 17:02:01 +00:00Commented Nov 16, 2015 at 17:02
After reviewing your codes, I've rewritten them this way, I didn't get a chance to test them though, so try them and let me know.
Server code: a. Arduino
#include <SoftwareSerial.h>
int ledPin = 13;
int txPin = 2;
int rxPin = 3;
SoftwareSerial wifi(rxPin, txPin);
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200); // This baudrate is better than 9600
wifi.begin(9600);
wifi.println("dofile('wifiserv.lua')"); // This where it goes
digitalWrite(ledPin, LOW);
delay(2000); // delay is necessary
}
void loop() {
while(wifi.available())
{
digitalWrite(ledPin, HIGH);
String inData = wifi.readStringUntil('\n');
if (inData.startsWith("Server IP: "))
Serial.println(inData); // This is just the Server IP address
else
Serial.println("Got new sensor value: " + inData)
}
digitalWrite(ledPin, LOW); // This means no data is available
}
Server code: b. ESP8266
uart.setup(0, 9600, 8, 0, 1, 1 )
wifi.setmode(wifi.STATION)
wifi.sta.config("mynet","pass")
print("Server IP: " .. wifi.sta.getip() .. "\n")
srv = net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn, s)
data = s:match("sensor=(%w+)")
data = data + "\n"
uart.write(0, data)
end)
end)
uart.on("data", function(cmd)
-- When ESP8266 receives data from Arduino, it will trigger this event
end, 0)
Client code: a. Arduino
#include <SoftwareSerial.h>
int ledPin = 13;
int EP = 9;
int txPin = 2;
int rxPin = 3;
int dato = 10;
String ip; // Assign this to your server IP
SoftwareSerial wifi(rxPin, txPin);
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(EP, INPUT);
Serial.begin(115200);
wifi.begin(9600);
wifi.println("dofile('wificlie.lua')"); // This where it goes
delay(2000); // delay is necessary
}
void loop()
{
long medida = TP_init();
delay(50);
if(medida > 1000)
{
Serial.println(medida);
digitalWrite(ledPin, HIGH);
String cmd = "ip=" + ip + " " + "data=" + String(dato);
Serial.println("Sending: " + cmd);
wifi.println(cmd);
}
else
{
digitalWrite(ledPin, LOW);
}
}
long TP_init()
{
delay(10);
long medida = pulseIn(EP, HIGH);
return medida;
}
Client code: b. ESP8266
wifi.setmode(wifi.STATION)
wifi.sta.config("mynet","pass")
uart.setup(0, 9600, 8, 0, 1, 1 )
uart.on("data", function(s)
uart.write(0, "GoSending!")
ip = s:match("ip=(%d+.%d+.%d+.%d+)")
data = s:match("data=(%w+)")
conn = net.createConnection(net.TCP, 0)
conn:on("connection", function(c)
print("connected");
conn:connect(80,ip)
conn:send("sensor=" .. data)
uart.write(0, "Data has been sent successfully")
end)
end, 0)
-
Comments are not for extended discussion; this conversation has been moved to chat. (cc @DanielCorona)Anonymous Penguin– Anonymous Penguin2015年12月01日 00:15:20 +00:00Commented Dec 1, 2015 at 0:15