Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By pacojarcia
#23180 I want to conect an ESP8266 with a sensor and send this information to another ESP8266 and in this module make somethig with this (serial port, LCD,...).
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
Code: Select all// 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
Code: Select all// 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("");
}
User avatar
By freedom2000
#23340 Hi

You try to connect teh client to this IP address port.beginPacket("192.168.4.1",9999);

You are right it is the address of the server so should work...

BUT, rememeber that you client board is also (by default) in Station + AP mode so exposes itself the 192.168.4.1 address.

So I believe (I had the same problem) that the client tries to connect to itself instead of connecting to the server.

Put the client board into Station Only mode and everything should work.

JP
User avatar
By pacojarcia
#23457 You are right. The problem was the client had two IPs and used to send the same IP than the server 192.168.4.1. I've solved.
I put the Server Code and de Client
SERVER
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUDP Udp;
char packetBuffer[255];
unsigned int localPort = 9999;
const char *ssid = "Your SSID"; 
const char *password = "Your password";

void setup() {
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
  Udp.begin(localPort);
  }

void loop() {
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len-1] = 0;
    Serial.print("Recibido(IP/Size/Data): ");
    Serial.print(Udp.remoteIP());Serial.print(" / ");
    Serial.print(packetSize);Serial.print(" / ");
    Serial.println(packetBuffer);

    Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
    Udp.write("recived: ");
    Udp.write(packetBuffer);
    Udp.write("\r\n");
    Udp.endPacket();
     }
}


CLIENT
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUDP Udp;
const char *ssid = "Your SSID";  //
const char *password = "Your Password";

char packetBuffer[255];
unsigned int localPort = 9999;
IPAddress ipServidor(192, 168, 4, 1);
IPAddress ipCliente(192, 168, 4, 10);
IPAddress Subnet(255, 255, 255, 0);
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA); // para que solo sea STA y no genere la IP 192.168.4.1
  WiFi.config(ipCliente, ipServidor, Subnet);
  Udp.begin(localPort);
}

void loop() {
unsigned long Tiempo_Envio = millis();
//ENVIO
    Udp.beginPacket(ipServidor,9999);
    Udp.write("Millis: ");
    char buf[20];   unsigned long testID = millis();    sprintf(buf, "%lu", testID);
    Udp.write(buf);
    Udp.write("\r\n");
    Udp.endPacket();
    Serial.print("enviando: ");Serial.println(buf);
delay(10); // para que le de tiempo a recibir la respuesta al envio anterior
 
//RECEPCION
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) packetBuffer[len-1] = 0;
    Serial.print("RECIBIDO(IP/Port/Size/Datos): ");
    Serial.print(Udp.remoteIP());Serial.print(" / ");
    Serial.print(Udp.remotePort()); Serial.print(" / ");
    Serial.print(packetSize);Serial.print(" / ");
    Serial.println(packetBuffer);
  }
Serial.println("");
delay(500);
}


I hope this Sketh be useful
User avatar
By Maurizio Ambra
#41864 hi

could i ask some question about ESP and udp comunication?

i have two ESP 8266-01, the first one in AP mode (server udp) and the second one is the client.
so i connect my laptop with the first esp in AP mode and send packages
to the address 192.168.3.255 without problem,so the server recive packages, but the second ESP connect with the first ESP-AP does not recive broadcast packages.
why?