WiFi.config doesn't work. Still used DHCP.

The hardware is ESP8266 11E NodeMCU 1.0

The thing still comes up with an .localIP being part of the DHCP pool, not what I set under WiFi.config. Earlier posts said I'd need to update my firmware but I believe this is much more up to date hardware so that may not be the problem.

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 = "c";

// 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);
}
  IPAddress ip(192, 168, 1, 6);
  IPAddress gw(192, 168, 1, 1);
  IPAddress dns(192, 168, 1, 1);
  IPAddress sn(255, 255, 255, 0);
  WiFi.config(ip, gw, sn,  dns);

and remove the Ethernet library includes

Why should I remove the ethernet.h and ethernetudp.h includes? That would cause the udp to stop working. Should I replace them with WiFi.h (caused more problems.) or wifiudp.h?

Thanks, now it has a fixxed IP address.

mattlogue:
Why should I remove the ethernet.h and ethernetudp.h includes? That would cause the udp to stop working. Should I replace them with WiFi.h (caused more problems.) or wifiudp.h?

ESP8266WFi.h has WiFiUdp

Good. Thing is I'll need to research ESP8266WiFi classes to find out how to send a UDP packet. It doesn't have members named .begin or .beginPacket, etc.

Juraj,

Thank you! A half hour later I get one talking to the other! Right now it's just random numbers to test with, but later it will be codes that actually do things.

Matt

all standard Arduino networking libraries for different networking hardware (Ethernet, WiFi, ESP8266WiFi, WiFiEsp, WiFiLink, WiFiNina, WiFi101, UIPEthernet ...) have classes derived from base classes Server, Client and Udp. this classes and some conventions define the standard Arduino networking API

You wouldn't know why this would stop like after a 100 or so times would you?

mattlogue:
You wouldn't know why this would stop like after a 100 or so times would you?

what this?

For some reason it, that is the ESP 12E, stops sending "ping" or any packet after a few hundred times OR a few minutes. It still replies to ICMP ECHO requests, however. Here is the new code:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "JMR";
const char* password = "c5";

WiFiUDP Udp;
unsigned int localUdpPort = 8888;  // local port to listen on
char receivedChars[255];  // buffer for incoming packets
char  replyPacket[] = "Hi there! Got the message :-)";  // a reply string to send back
boolean newData;
int numChars = 255;

void setup()
{
  Serial.begin(115200);
  Serial.println();

  Serial.printf("Connecting to %s ", ssid);

  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);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  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();
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}
void sendUDP() {
    Udp.beginPacket("192.168.1.139", 8888);
    Udp.write(receivedChars);
    Udp.endPacket();
}
void pingOut() {
  Udp.beginPacket("192.168.1.139", 8888);
  Udp.write("ping");
  Udp.endPacket();
}

void loop() {
  recvWithEndMarker();
  showNewData();
  sendUDP();
  pingOut();
  delay(500);
}

I could just use one of my sacred Arduino pins to reset the 12E every 5 minutes but I'd rather get at the root of the problem.

Well I counted the packets and it sends just about 500 each time (that is roughly two & half minutes) if that tells you anything significant.