Report
2 years ago.

Not getting UDP connection to work with AT+CIPSTA and AT+CIPSEND

So I am trying to establish UDP connection using AT+CIPSTA_CUR, but whenever I am sending message using AT+CIPSEND, I get "ERROR" and "link is not valid". I am supposed to be sending "Hello" only once but comes up as four times and get "ERROR" and "Link is not valid". For code I am using to establish connection, what would be the best option to get rid of this error ?

-- Prep Socket for Data Send -- AT+CIPSEND=4,20

link is not valid

ERROR AT+CIPSEND=4,20

link is not valid

ERROR AT+CIPSEND=4,20

link is not valid

ERROR

-- Send String -- HelloHelloHelloHello -- Prep Socket for Data Send -- AT+CIPSEND=4,20

link is not valid

ERROR AT+CIPSEND=4,20

link is not valid

ERROR AT+CIPSEND=4,20

link is not valid

ERROR

-- Send String -- HelloHelloHelloHello

#include "mbed.h"
#include <stdio.h>
#include <string.h>

Serial serial(USBTX, USBRX);
Serial esp(P4_28, P4_29); // tx, rx
DigitalOut reset(P0_4);
DigitalOut blueLed(P2_3);
DigitalOut greenLed(P2_4);
DigitalOut redLed(P2_5);
DigitalOut vibr(P2_12);

Timer t;

int  count,ended,timeout;


char buf[4096];
char snd[255];
char sendStr[11];

char ssid[32] = "BTHub6-MPZT";     // enter WiFi router ssid inside the quotes
char pwd [32] = "DPx4PUpvdENF"; // enter WiFi router password inside the quotes

void SendCMD();
void getReply();

int main() {
    
    //Make sure all feedback mechanisms are turned off
    blueLed=0;
    greenLed=0;
    redLed=0;
    vibr=0;

    //set buad rates for comms
    esp.baud(115200);
    serial.baud(115200);

    
    // Setup a serial interrupt function to capture received data
    esp.attach(&getReply, Serial::RxIrq);
    

    //Device doesn't appear to need a reset upon waking up so the device is not reset (i.e. reset = 1);
    reset=1;

    // Give the user some to setup the virtual terminal
    wait(5);
    
    serial.printf("\n---------- Factory reset device ----------\r\n");
    strcpy(snd, "AT+RESTORE\r\n");
    timeout=10;
    SendCMD();
    getReply();
    serial.printf(buf);
    
    serial.printf("\n---------- Check status ----------\r\n");  //
    strcpy(snd, "AT+GMR\r\n");
    timeout=2;
    SendCMD();
    getReply();
    serial.printf(buf);
    
    serial.printf("\n---------- Check status ----------\r\n");  //
    strcpy(snd, "AT+CWMODE_CUR=1\r\n");
    timeout=2;
    SendCMD();
    getReply();
    serial.printf(buf);
    
    serial.printf("\n---------- List APs in Range ----------\r\n"); 
    strcpy(snd, "AT+CWLAP\r\n");
    timeout=10;
    SendCMD();
    getReply(); 
    serial.printf(buf);
    
    serial.printf("\n---------- Connecting to AP ----------\r\n"); 
    serial.printf("ssid = %s pwd = %s\r\n",ssid,pwd); 
    strcpy(snd, "AT+CWJAP_CUR=\""); 
    strcat(snd, ssid);
    strcat(snd, "\",\""); 
    strcat(snd, pwd); 
    strcat(snd, "\"\r\n"); 
    timeout=15;
    SendCMD();
    wait(3);
    getReply();
    serial.printf(buf);
    
    serial.printf("\n---------- Check APIP ----------\r\n");  //Verify that you still have a station IP address
    strcpy(snd, "AT+CIPSTA_CUR?\r\n");
    timeout=3;
    SendCMD();
    getReply();
    serial.printf(buf); 
    
    serial.printf("\n---------- Turn off DHCP for Station----------\r\n");  //
    strcpy(snd, "AT+CWDHCP_CUR=1,0\r\n");
    timeout=2;
    SendCMD();
    getReply();
    serial.printf(buf);  //Prints out response, should be "OK"
    
    serial.printf("\n---------- check DHCP settings----------\r\n");  //
    strcpy(snd, "AT+CWDHCP?\r\n");
    timeout=2;
    SendCMD();
    getReply();
    serial.printf(buf);  //Prints out response, should be "OK"
 
    
    
     serial.printf("\n---------- Turn on Multiple Connections  ----------\r\n");  //
    strcpy(snd, "AT+CIPMUX=1\r\n");
    timeout=2;
    SendCMD();
    getReply();
    serial.printf(buf); 
    
    /*serial.printf("\n---------- Establishing UDP Connection ----------\r\n");
    strcpy(snd, "AT+CIPSTART=4,\"UDP\",\"192.168.1.146\",1234,5678,0\r\n");
    SendCMD();
    timeout=3;
    getReply();
    wait(2);
   serial.printf(buf);*/
   
   serial.printf("\n---------- Set static Station IP Address  ----------\r\n");  //
    strcpy(snd, "AT+CIPSTA_CUR=\"192.168.1.146\",\"192.168.1.254\",\"255.255.255.0\"\r\n");
    timeout=5;
    SendCMD();
    getReply();
    serial.printf(buf);  //Prints out response, should be "OK"
    
    //Check to see that the connection has been established
    serial.printf("\n---------- Connection Information ----------\r\n");
    strcpy(snd, "AT+CIFSR\r\n"); //get local IP address
    timeout=5;
    SendCMD();
    getReply();
    serial.printf(buf); 
    
    while(1) {
        
         serial.printf("\n---------- Prep Socket for Data Send ----------\r\n"); 
        strcpy(snd, "AT+CIPSEND=4,20\r\n");
        SendCMD();
        timeout=1;
        getReply();
        wait(0.5); 
        serial.printf(buf);
        
        serial.printf("\n---------- Send String ----------\r\n"); 
        strcpy(snd, "Hello");
        serial.printf(snd);
        SendCMD();
        timeout=3;
        getReply(); 
        serial.printf(buf);
        
    }
}

//This function sends the command string to the ESP8266
void SendCMD()
{
    esp.printf("%s", snd);
}



//This function establishes the space for the received string, starts a timer so that if things get stuck, the 
//app won't hang.  
void getReply()
{

    memset(buf, '\0', sizeof(buf));
    t.start();
    ended=0;
    count=0;
    while(!ended) {
        if(esp.readable()) {
            buf[count] = esp.getc();
            count++;
        }
        if(t.read() > timeout) {
            ended = 1;
            t.stop();
            t.reset();
        }
        
    
    }
    
    serial.printf(buf);
   
}


Comment on this question
Be the first to answer this question.

To post an answer, please log in.