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)