-->
Page 1 of 1

AT commands for reading udp packages from the esp8266?

PostPosted: Wed Jun 29, 2016 9:50 am
by PQRX9000
Hi!

I have the managed to connect to the WiFi and started to listen for UDP packages on port 5000 with the following code:

Code: Select all#include <SoftwareSerial.h>
SoftwareSerial esp8266(50,51);            // Rx = pin 50, Tx = pin 51
/*
 * Baudrate 9600, (Set a new default baudrate with:
 * AT+UART_DEF=<baudrate>,<databits>,<stopbits>,<parity>,<flow control>)
 * Ex: AT+UART_DEF=9600,8,1,0,0
 *
 * Arduino Mega Tx (51)   -->   ESP8266 Rx
 * Arduino Mega Rx (50)   -->   ESP8266 Tx
 * VCC(3,3V)              -->   CH_PD
 * VCC(3,3V)              -->   RESET
 * VCC(3,3V)              -->   VCC (3,3V)
 * GND                    -->   GND
 */

const String mySSID = "\"?????\"";     // Rauter name
const String myPSK  = "\"?????\"";     // Rauter Password 

void esp8266_cmd(String cmd, int delayTime = 500)
{
  Serial.println(cmd);
  Serial.flush();
  esp8266.println(cmd);
  delay(delayTime);
  while( esp8266.available() )
    Serial.print((char)esp8266.read()); 
}

int connect_to_WiFi(int UDPport = 5000)
{
  esp8266_cmd("AT+RST", 1000);            // Reset device
  esp8266_cmd("ATE0", 500);               // ATE0 = Disable echo, ATE1 = Enable echo
  esp8266_cmd("AT", 500);                 // Test
  esp8266_cmd("AT+CWMODE=3", 1000);       // Station and Access point
  // Get an IP address
  esp8266_cmd("AT+CWJAP=" + mySSID  + "," + myPSK, 4500);
  esp8266_cmd("AT+CIFSR", 500);           // Print the given ip address
 
  // UDP packet listener on UDPport (See p.44 in the datasheat)
  // AT+CIPSTART=<type>,<remote IP>,<remote port>[,<UDP localport>,<UDP mode>][,<TCP keep alive>]
  /*
   * Examples (Single connection):
   * AT+CIPSTART="TCP","192.168.101.110",1000
   * AT+CIPSTART="UDP","0",0,10000,2                //http://bbs.espressif.com/viewtopic.php?t=74
   */
  String cmd     = "AT+CIPSTART=";
  String type    = "\"UDP\"";                       // type
  String rIP     = "\"0\"";                         // 0 : Only used for TCP
  String rPort   = "0";                             // 0 : Only used for TCP
  String udpPort = String(UDPport);                 // Listen for packages on this port
  String udpMode = "2";                             // Destination is allowed to change
  String udp_setup_cmd = cmd + type + "," + rIP + "," + rPort + "," + udpPort + "," + udpMode;
 
  Serial.print("udp_setup_cmd= "); Serial.println(udp_setup_cmd);
  Serial.flush();
  esp8266_cmd(udp_setup_cmd);
}

void setup() {
 
  Serial.begin(38400);
  esp8266.begin(9600);

  while( !Serial )  {}                  // Wait for peripherals to be ready
  while( !esp8266 ) {}                  // Wait for peripherals to be ready

  int port = 5000;                      // Recieve packages on this port 
  connect_to_WiFi( port );              // Connect to WiFi and listen for UDP packages on the given port
 
  Serial.println("Setup Complete");     // Indicate Setup Complete
}


/* Print received UDP packages from the esp8266 module */
// Not working...
void loop() {
    if( esp8266.available() ){
        while( esp8266.available() )
          Serial.write((char)esp8266.read()); 
        }
    }
}


I receive the following on the serial monitor when executing the code above:
Code: Select all.
.
.
WIFI CONNECTED
AT+CWJAP="?????","?????"
AT+CWJAP="?????","?????"

WIFI DISCONNECT
WIFI CONNECTAT+CIFSR
AT+CIFSR

+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:fdudp_setup_cmd= AT+CIPSTART="UDP","0",0,5000,2
AT+CIPSTART="UDP","0",0,5000,2
AT+CIPSTART="UDP","0",0,5000,2

0,CONNECT

OK

Setup Complete


My problem now is that I am unable to receive any packages that I am sending. I think that the problem is that I have to use some AT commands to check if any packages has been received as well as the number of bytes that has been received before sending a final command that fetches the data.

I have been trying to find the relevant information about this procedure without success so I wonder if anyone here can provide some suggestions on how to achieve this?

Best regards
PQRX9000