I've made with UDP and work properly, but only wen use a router. In this case everythig work, even when turno off / on server or client.
But In AP mode only work when power on, (one or two sends), and stop. After one or two seconds the server stop to recive.
Anybody know why?
SERVER
// Ref : http://www.esp8266.com/viewtopic.php?f=28&t=2295&p=13730#p13730
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
const char *ssid = "PacoESP";
const char *password = "your Password";
void setup() {
Serial.begin(115200);
// WiFi.begin("yourSSID", "your Password");
//WiFi.softAP("PacoESP", "your Password");
//WiFi.softAPIP("PacoESP", "your Password");
WiFi.softAP(ssid, password);
//WiFi.mode(WIFI_AP_STA);
WiFi.mode(WIFI_AP); // para forzar que solo sea conexión AP
port.begin(localPort);
//PACO
Serial.print("[Connecting] ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.softAPIP());
}
void loop() {
int packetSize = port.parsePacket();
Serial.print("Recibido(IP/Size/Data): ");
Serial.print(port.remoteIP());Serial.print(" / ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize) {
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
port.beginPacket(port.remoteIP(),port.remotePort());
port.write("Your UDP packet was received OK\r\n");
port.endPacket();
}
Serial.println("");
delay(500);
//PACO
Serial.print("[Connected] ");
Serial.println (WiFi.localIP());
Serial.println(WiFi.softAPIP());
Serial.println("");
}
Client
// Ref : http://www.esp8266.com/viewtopic.php?f=28&t=2295&p=13730#p13730
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP port;
char packetBuffer[255];
unsigned int localPort = 9999;
void setup() {
Serial.begin(115200);
//WiFi.begin("yourSSID", "your Password");
WiFi.begin("PacoESP", "your Password");
port.begin(localPort);
//PACO
Serial.print("[Connecting]: ");
Serial.println(WiFi.localIP());
}
void loop() {
//RECEPCION
int packetSize = port.parsePacket();
Serial.print("Recibiendo(Size/dato): ");
Serial.print(packetSize);Serial.print(" / ");
if (packetSize) {
int len = port.read(packetBuffer, 255);
if (len > 0) packetBuffer[len-1] = 0;
Serial.print(packetBuffer);
//port.beginPacket(port.remoteIP(),port.remotePort());
//port.write("Your UDP packet was received OK\r\n");
//port.endPacket();
}
Serial.println("");
//ENVIO
//port.beginPacket("192.168.0.145",9999);
port.beginPacket("192.168.4.1",9999);
port.write("Envio millis: ");
char buf[20];
unsigned long testID = millis();
sprintf(buf, "%lu", testID);
Serial.print("enviando: ");Serial.println(buf);
port.write(buf);
port.write("\r\n");
port.endPacket();
delay(500);
//PACO
Serial.print("[Connected] ");
Serial.println(WiFi.localIP());
Serial.println("");
}