Only works once. Serial.read loop and UDP packet sending

The code is supposed to take what it receives over Serial and send it to another ESP8266 via UDP. I get successful packet sends, but only ONCE. It ignores any further serial data.

int code, i, x;
char* codeChar;

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
//#include <WiFi.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>


IPAddress ip(192, 168, 1, 139);
const char* ssid = "JMR";
const char* password = "crystal2965";

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:


unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.config(192, 168, 1, 137);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Node7b: Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Udp.begin(localPort);


}

void loop() {
  codeChar = "";
  i = 0;
  while (Serial.available()) {
    codeChar[i] = Serial.read() ;
    i++;
  }
  // send a reply, to the IP address and port that sent us the packet we received
  x = Udp.beginPacket(ip, 8888);
  if (x == 1) {
    Serial.print("beginPacket status: Success ");
  }
  else {
    Serial.print("beginPacket status: FAILURE ");
  }
  Serial.print("Sent: "); Serial.println(code);
  Udp.write(codeChar);
  x = Udp.endPacket();
  if (x == 1) {
    Serial.print("Packet Send status: Success ");
  }
  else {
    Serial.print("Packet Send status: FAILURE ");
  }
delay(100);
}

Your arrangement for receiving serial data is not robust. Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

You have a few print() statements in your code. Can you tell from them the point at which your program is failing?

...R

Before any of the print statements. I write in a number and get nothing back in SM

char* codeChar;

You never actually allocate memory for this. So you instantly shoot randomly into your RAM stack/heap. Never a good thing.

-jim lee

Here is the current code. I utizlized the Example 2 and pasted it making necessary changes. I may have a few issues but nothing that should stop the code cold.

It stops on x = Udp.endPacket();

Any reason why this would halt? The destination, btw, isn't there, but UDP unlike TCP doesn't wait for an acknowledgement. The thing although not continuing executing the code still replies to ICMP ECHO requests.

const byte numChars = 8;
char receivedChars[numChars];   // an array to store the received data
char codeChar[numChars];
boolean newData = false;



void setup() {
  // start the Ethernet and UDP:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  
  IPAddress ip(192, 168, 1, 137);
  IPAddress gw(192, 168, 1, 254);
  IPAddress dns(192, 168, 1, 254);
  IPAddress sn(255, 255, 255, 0);
  WiFi.config(ip, gw, sn,  dns);
  
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Node7b: Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Udp.begin(localPort);
}

void loop() {
     recvWithEndMarker();
    showNewData();
delay(100);
}
void sendUDP(char codeChar[numChars]) {
    x = Udp.beginPacket(ip, 8888);
  if (x == 1) {
    Serial.print("beginPacket status: Success ");
  }
  else {
    Serial.print("beginPacket status: FAILURE ");
  }
  Serial.print("Sending: "); Serial.println(codeChar);
  Udp.write(codeChar);
  x = Udp.endPacket();
  if (x == 1) {
    Serial.print("Packet Send status: Success ");
  }
  else {
    Serial.print("Packet Send status: FAILURE ");
  }
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;

Serial.print("recvWithEndMarker");
    
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
      sendUDP(receivedChars);
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Still not working. I even took off the x = lvalue.

SOLVED - Juraj recommended using the UDP classes present in ESP8266Wifi instead of EthernetUDP. It works now! No crashes.