Grove UART WiFi sending UDP

I have a test sketch for the Uno using the Grove connector shield and their UART WiFi module. I can get it to recognize incoming TCP data but I can't get it to actually reply via UDP (or TCP for that matter) to my PC. The serial monitor shows normal completion of the UDP transmission. I use Wireshark on my PC to watch all TCP and UDP data flow between the Arduino and my PC. It shows the TCP connection and data transmission from the PC but never registers any UDP reply from the Arduino. Please note that this a test sketch and contains a lot of debugging output that would not make it into the final sketch.

Any thoughts on why it won't send UDP? The Seeed Grove forum has been no help.

#include <Wire.h>

// emulate Serial1 for WiFiEsp on pins 5/6
#ifndef HAVE_HWSERIAL1
  #include "SoftwareSerial.h"
  SoftwareSerial Serial1(5, 6); // digital pins for WiFi RX = 5, TX = 6
#endif

#include "arduino_secrets.h"
char ssid[] = SECRET_SSID;    // network SSID (name)
char pass[] = SECRET_PASS;    // network password (use for WPA, or use as key for WEP)

void setup() {
  Serial.begin(9600); // monitor
  while(!Serial);
  
  Serial1.begin(115200); // Wifi UART
  while(!Serial1);
  
  Wire.begin();

  // disable DHCP
  if (cmd_OK(F("Disable DHCP..."), F("AT+CWDHCP_CUR=1,0"), F("OK")))
    Serial.println(F("   DHCP disabled..."));
  else {
    Serial.println(F("   DHCP status unknown..."));
    while(1);
  }
    
  // set IP config
  if (cmd_OK(F("Config IP..."), F("AT+CIPSTA=\"192.168.1.252\",\"192.168.1.1\",\"255.255.255.0\""), F("OK")))
    Serial.println(F("   IP config OK..."));
  else {
    Serial.println(F("   IP config unknown..."));
    while(1);
  }

  // set WiFi mode as station
  if (cmd_OK(F("Setting WiFi mode..."), F("AT+CWMODE=1"), F("OK")))
    Serial.println(F("   WiFi mode OK..."));
  else {
    Serial.println(F("   WiFi mode unknown..."));
    while(1);
  }

  // connect to WiFi
  Serial.println(F("Connecting to WiFi..."));
  bool connected = false;
  while (!connected) {
    String logon = "AT+CWJAP=\"" + String(ssid) + "\",\"" + String(pass) + "\"";
    char* logonCmd = logon.c_str();
    cmd_send(logonCmd);
    delay(15000);
    if (get_result(F("WIFI CONNECTED"))) connected = true;
  }
  Serial.println(F("   Wifi connected..."));

  // create listening TCP server on port 2390
  if (cmd_OK(F("Creating listening server..."),F("AT+CIPSERVER=1,2390"),F("OK")))
    Serial.println(F("Listening server created..."));
  else {
    Serial.println(F("Listening server NOT created..."));
    while(1);
  }
  
  // show the remote IP and port with +IPD
  if (cmd_OK(F("Configure show remote IP & port..."), F("AT+CIPDINFO=1"), F("OK")))
    Serial.println(F("Show remote IP & port configured..."));
  else {
    Serial.println(F("Show remote IP & port NOT configured..."));
    while(1);
  }

  Serial.println(F("Listening..."));
} // setup
  
void loop() {
  // read any data received
  String lineRx = "";
  while (Serial1.available() > 0) {
    byte rx = Serial1.read();
    if (char(rx) == '+') {
      lineRx = char(rx);
      while (Serial1.available() > 0) {
        rx = Serial1.read();
        lineRx = lineRx + char(rx);
      }
    }
  }
  
  if (lineRx.length() > 0) { // process incoming TCP data
    Serial.println("Received: " + lineRx);
    
    char* msg = extractMsg(lineRx.c_str()); // everything after colon
    char cmd = msg[0]; // first char of msg
    String param = extractParam(msg); // msg purged of first char
    
    Serial.println("msg = " + String(msg) + "; cmd = " + cmd + "; param = " + param);

    // create UDP transmission
    if (cmd_OK(F("Creating UDP transmission..."), "AT+CIPSTART=\"UDP\",\"192.168.1.15\",49152", "OK"))
      // send UDP data
      if (cmd_OK(F("Sending UDP data..."), "AT+CIPSEND=7", ">")) {
        Serial1.println("UDPTest"); // data to send
        if (get_result("Recv 7 bytes"))
          // close UDP transmission
          if (cmd_OK(F("Closing UDP transmission..."), "AT+CIPCLOSE", "CLOSED"))
            Serial.println("UDP connection closed..."); 
      }
  }
} // loop

bool get_result(String msg) {
  while(Serial1.available() == 0);
  String line = "";
  while (Serial1.available() > 0) {
    byte rx = Serial1.read();
    if (rx > 31 && rx < 127) { // printable ASCII
      line = line + char(rx);
    }
    else {
      //Serial.println("Line = " + line);
      if (line == msg) {
        return(true);
      }
      else {
        line = "";  
      }
    }
  }
}

// send command
void cmd_send(char *cmd) {
  if (NULL == cmd) return;
  Serial.println(" Sent: " + String(cmd));
  Serial1.println(cmd);
  Serial.println(F("  Waiting for response..."));
}

bool cmd_OK(String prompt,  String cmd,  String response) {
  Serial.println(prompt);
  cmd_send(cmd.c_str());
  delay(500);
  if (get_result(response)) {
    return(true);
  }
  else {
    return(false);
  }
}

char* extractMsg(char* cmdLine) {
  char *colon = ":";
  char *s = strtok(cmdLine, colon);
  char *s1 = strtok(NULL, colon);
  return(s1);
}

String extractParam(char* cmdLine) {
  cmdLine++;
  return(String(cmdLine));
}

wait for "OK" after/instead of "Recv 7 bytes"
close even if send wasn't successful
you could send repeatedly without starting and closing

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.