ESP8266 NodeMCU won't receive packets on static IP

Hi all,

I’m working on a huge network project which has wearables with an ESP8266 NodeMCU.
Unfortunately, the devices won’t send/receive, or sometimes when sending the packet 4 or more times unless you disable the IP config, or if you put the IP in a 192.168.1.100 range.

However, I prefer the IP in a 10.0.0.1 range – or 172.16.1.1

So I’ve tried a couple of things.
I’m almost certain the issue is IP related.

So I’ve hooked one of my own routers to the network and created a separate network - no succes
Then I pulled the ‘main’- network out of my router (so a standalone router) - no succes
Then I configured the router even more with disabling all of the filters that I could find, then tried again with ‘main’-network and without…
Normally I use PacketSender to send and receive UDP, however, PacketSender is not 100% reliable, so I’ve also tested with Hercules.
Also used PING to ping my device, if using my desired IP - I get a ‘request timed out’ back.

I’m really curious if someone knows how to fix this issue, it is the first time I do a network project with ESP8266 module, so maybe I’m missing something

Some explanation for the code;
I have a V1, which is almost my completed code. When I did some testing I had these problems but ignored it for ‘to be solved later’. Also, I was not sure yet was caused the issue.

Today was my intention to fix the issue, I did first a V2 with removing stuff from my V1 code, then did a V3, where I wrote my code again but checked it piece by piece, then I discovered it was the IP adress.

So this is the V3 code — do notice: it is the Udp example code from the ESP8266 library, with minor changes.

/*
  THEBULB.
  TERRASLAMP

  V1.0b
  08-2020

  Budget version. 
  Please refer to version a for extra functionalities such as:
  - check firmware version
  - detection of connection loss

  Based on nano 33 IoT

*/
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <FastLED.h>

#include "secret.h"

#define LED_PIN     6
#define COLOR_ORDER GRB
#define CHIPSET     WS2812B
#define NUM_LEDS    3
CRGB leds[NUM_LEDS];

/* Unique ID AND IP */
IPAddress staticIP(172, 16, 1, 101);          <----- IF I DISABLE THESE LINES THE UPD WORKS GREAT
IPAddress gateway(172,16,1,1);                <----- OR IF I SET THE IP IN A 192.168.1.100-255 range
IPAddress subnet(255,255,255,0);              <----- THEN IT ALSO WORKS
//int tableID = 01;   <--- to be added in later, packetbuffer should contain variable tableID
/* END */

int status = WL_IDLE_STATUS;

/* BOOLEANS */
bool candleAllowance = false;
bool connectionLive = false;

bool dark = false;
bool light = false;
bool stateIdle = false;
bool stateAvailable = false;
bool stateOrdered = false;
bool stateOff = true;
/* END */

/* LIGHT SENSOR */
int lightSensor;
long makingSureItIsDark = 0;
/* END */

/* BLINK LED VARIABLES */
const int ledPin =  LED_BUILTIN;// the number of the LED pin
int ledState = HIGH;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
unsigned long interval = 5000;           // interval at which to blink (milliseconds)
/* END */

/* WiFi TESTER VARIABLES */
unsigned long startTime;
unsigned long comparisonTime = 0;
/* END */

/*Data is stored in Secret tab*/
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

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

char packetBuffer[256]; //buffer to hold incoming packet
char  ReplyBuffer[] = "table 01: acknowledged";       // a string to send back

WiFiUDP Udp;

void setup() {
  pinMode(ledPin, OUTPUT);
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.clear(true);
  // FastLED.setBrightness( BRIGHTNESS );

  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  delay(1000);

  Serial.println();
  Serial.println("******* NEW SESSION STARTED *******");
  Serial.println("THE BULB. terraslamp -- V01 2020");
  Serial.println("Setting up network now, hold your drinks high..!");
  Serial.println();
  delay(100); 
       Serial.print("MAC: ");
   Serial.println(WiFi.macAddress());
   delay(100); 
  WiFi.mode(WIFI_STA);
  WiFi.config(staticIP, gateway, subnet);       <----- THIS LINE SHOULD BE DISABLED AS WELL
  WiFi.begin(ssid, pass);
   Serial.println();
   delay(100); 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());
  Serial.printf("UDP server on port %d\n", localPort);
  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.printf("Received packet of size %d from %s:%d\n    (to %s:%d, free heap = %d B)\n",
                  packetSize,
                  Udp.remoteIP().toString().c_str(), Udp.remotePort(),
                  Udp.destinationIP().toString().c_str(), Udp.localPort(),
                  ESP.getFreeHeap());

    // read the packet into packetBufffer
    int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    packetBuffer[n] = 0;
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }

}

/*
  test (shell/netcat):
  --------------------
    nc -u 192.168.esp.address 8888
*/

Oh and yes, I've tested it with my firewall OFF. :slight_smile:

has the router IP 172.16.1.1?

use
WiFi.persistent(false);
WiFi.setAutoConnect(false);

to disable automatic connection with remembered ssid and password and storing of the ssid and password.
static config is not remembered by SDK.

Try putting Wifi.mode after Wifi.config and before Wifi.begin as:

WiFi.config(staticIP, gateway, subnet); <----- THIS LINE SHOULD BE DISABLED AS WELL
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);

Awesome, thanks for the suggestions. Will test this monday first thing. I’ll post an update by then! Thanks!

Hi all,

So I did test it this morning, but still no results.
I'm doing some further testing to see if I can find anything.

Ive tried both solutions which gave the same results as before.

If you have any other suggestions, I would love to hear them

darthvader072:
Hi all,

So I did test it this morning, but still no results.
I'm doing some further testing to see if I can find anything.

Ive tried both solutions which gave the same results as before.

If you have any other suggestions, I would love to hear them

the esp8266 must be in STA mode to apply static IP so Paul's advice doesn't seem right.

config must be called in STA mode but before joining the network.

I am not sure if your net mask is valid for this IP range or if the esp8266 SDK considers it valid

You could try static IP from router with device on DHCP.

What do you get from serial?

Have you tried printing network gateway, subnet, DNS?

Create a new project without anything other than networking. Put a lot of serial outputs.

My bet? Your subnet mask is no actually 255.255.255.0, print it to see if that is the case.

However, I prefer the IP in a 10.0.0.1 range -- or 172.16.1.1

Maybe I missed it but does those subnets exist on the router you are using? Although you don't explicitly state it one way or the other (or I missed it) I get the impression that you have not considered that the router needs to be configured with the required subnet.

Also, if you are using static IP addresses I suggest you configure the ESP8266s to use dynamic IP addresses the configure the router to lock specific IP addresses to specific ESP8266s using their MAC addresses. The advantage being you can use the same code on each ESP8266 without the need to put a unique IP address on each one, you just configure them all on the router.

Then I configured the router even more with disabling all of the filters that I could find, then tried again with 'main'-network and without..

Irrelevant, firewalls, filters etc apply to WAN traffic, not to traffic on the same local subnet. Also, you should not have a router behind another router.

woistmeinauto:
You could try static IP from router with device on DHCP.

What do you get from serial?

Have you tried printing network gateway, subnet, DNS?

Create a new project without anything other than networking. Put a lot of serial outputs.

My bet? Your subnet mask is no actually 255.255.255.0, print it to see if that is the case.

So I truly sorry if I do it wrong, but I did this;

#include <ESP8266WiFi.h>

void setup()
{
  Serial.begin(115200);
  Serial.println();
  delay (500);
    Serial.setDebugOutput(true);
  delay(500); 
  WiFi.begin("The BULB", "Ledisvet2020");

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  delay (500); 
  Serial.printf("Wi-Fi mode set to WIFI_STA %s\n", WiFi.mode(WIFI_STA) ? "" : "Failed!");
  delay (500); 
  Serial.printf("Connection status: %d\n", WiFi.status());
  delay (500); 
  Serial.setDebugOutput(true);
}

void loop() {
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
delay(5000);
Serial.print("Subnet Mask: ");
Serial.println(WiFi.subnetMask());
delay (5000); 
Serial.print("Gateway IP: ");
Serial.println(WiFi.gatewayIP());
delay(5000);
Serial.print("DNS 1: ");
Serial.println(WiFi.dnsIP(0));
Serial.print("DNS 2: ");
Serial.println(WiFi.dnsIP(1));
delay(5000);
}

Which gave as serial output;
15:23:25.950 → Connecting…scandone
15:23:29.754 → state: 0 → 2 (b0)
15:23:29.754 → .state: 2 → 3 (0)
15:23:29.754 → state: 3 → 5 (10)
15:23:29.754 → add 0
15:23:29.754 → aid 1
15:23:29.754 → cnt
15:23:29.789 →
15:23:29.789 → connected with The BULB, channel 1
15:23:29.789 → dhcp client start…
15:23:30.239 → …ip:192.168.0.11,mask:255.255.255.0,gw:192.168.0.1
15:23:36.756 → .
15:23:36.756 → Connected, IP address: 192.168.0.11
15:23:37.236 → Wi-Fi mode set to WIFI_STA
15:23:37.754 → Connection status: 3
15:23:38.268 → Local IP: 192.168.0.11
15:23:39.774 → pm open,type:2 0
15:23:43.249 → Subnet Mask: 255.255.255.0
15:23:48.237 → Gateway IP: 192.168.0.1
15:23:53.259 → DNS 1: 192.168.0.1
15:23:53.259 → DNS 2: (IP unset)

Most surprising to me is that DHCP client starts, I don’t want dhcp, since i want static IP.

I don't want DHCP, since I want static IP addressing.

I suggest you read my reply #8, the bit about static IP addresses. Unless there is a good reason not to I recommend you do as I have suggested.

When trying your own router, what sort of router is it?
A basic router can have 2 ethernet connections and nothing else, but often when refering to a router people mean a combined unit that has routing between ethernet and WAN (broadband), an ethernet switch and an access point (WiFi). The ethernet and Wifi are on one side of the routing functionality and the WAN on the other - more sophisticated ones can have multiple WAN connections and multiple (independant) Ethernet connections.

If you have placed your router acting as an AP connected via Ethernet to the main router then your router will be simply acting as an ethernet switch and AP - though it may give out DHCP addresses.

When you have your router and the main router (both as APs) have you checked to make sure which one is giving your ESP its IP address?

As Woistmienauto said print out everything once you have a connection, including the IPaddress given to the ESP, and details of the AP used.

Using 2 routers has its place such as one suporting the WAN connection and the other supporting multiple networks but in your case if both routers are acting as routers it will be making life more complicated than needed.

Try with both DHCP and static addresses to see if they give the same results other than IP

countrypaul:
When trying your own router, what sort of router is it?
A basic router can have 2 ethernet connections and nothing else, but often when refering to a router people mean a combined unit that has routing between ethernet and WAN (broadband), an ethernet switch and an access point (WiFi). The ethernet and Wifi are on one side of the routing functionality and the WAN on the other - more sophisticated ones can have multiple WAN connections and multiple (independant) Ethernet connections.

If you have placed your router acting as an AP connected via Ethernet to the main router then your router will be simply acting as an ethernet switch and AP - though it may give out DHCP addresses.

When you have your router and the main router (both as APs) have you checked to make sure which one is giving your ESP its IP address?

As Woistmienauto said print out everything once you have a connection, including the IPaddress given to the ESP, and details of the AP used.

Using 2 routers has its place such as one suporting the WAN connection and the other supporting multiple networks but in your case if both routers are acting as routers it will be making life more complicated than needed.

Try with both DHCP and static addresses to see if they give the same results other than IP

It is a TP-LINK TL-WR702N, configured as Router. Basically I did a quick setup, after that made sure my laptop can connect to it as well as the ESP. Also tested UDP packets in default setup (without static IP), worked. Then disconnected WAN (which originally was connected to my router for my ‘normal’ WIFI). Then tested it again, works fine.

The ESP get’s IP 192.168.0.101. MAC-address: C8:2B:96:30:01:8F

So I’m sure that the TP-Link gives this IP address, since it is not connected to my other router.

I did learned this:
My TP-Link router has DHCP settings, which leases adresses in the range of 192.168.0.100 to 192.168.0.199
Which makes sense since my ESP is …101

So what I’ve tried, is to set it into my desired range. However, the interface tells me it isn’t valid.
So I’ve tried multiple unique IP-pools, always same response: not valid…

Then I disabled the DHCP server, and added a static routing, I added my ESP with:
IP: 192.168.10.101
Gateway: 192.168.10.1
Subnet: 255.255.255.0

Then restarted all the devices, no luck still.
Even had to reset my router since I could not reach the webinterface any more…

See images attached

2020-09-02 10_17_18-TP-LINK Wireless N Nano Router WR702N.png

PerryBebbington:
I suggest you read my reply #8, the bit about static IP addresses. Unless there is a good reason not to I recommend you do as I have suggested.

This works actually quite alright. But I'm facing the issue like posted in reply #12, I can not make a unique IP-pool somehow..

I had a quick (I stress quick) read of TP Link router manual and I would expect you to be able to set one (only one) IP address range (page 42). It seems to default to 100 to 199, which is daft. Should be the complete range for the subnet, try changing it.

Failing that get a Draytek router, what you want to do will be possible.

PerryBebbington:
I had a quick (I stress quick) read of TP Link router manual and I would expect you to be able to set one (only one) IP address range (page 42). It seems to default to 100 to 199, which is daft. Should be the complete range for the subnet, try changing it.

Failing that get a Draytek router, what you want to do will be possible.

Okay thanks, I did not look at the LAN menu, since my thought was that it only was using WAN..
I'm not an network expert as you might have noticed, haha.

With this I'm able to make a more unique IP pool.
Still I'm facing a issue. The wearables will be placed on a table.
Each table should have a unique ID, originally the plan was that my ID matches the IP. so for instance:

Table number 1 = .101
Table number 10 = .110
etc etc

So the way to do it now is to set al the MAC-addresses reservations in the router with the matching IP/ID of table. Is this the quickest and best way?

Secondly, if a battery runs empty the device should be exchanged for one with a full battery.
Originally my plan was to use a local computer to adress the device through RFID.

So for example;
Table 1 is empty --> pick a unassigned device --> select on computer table number and click pair to send the data --> device reads and stores it's IP (.101) and is ready to be used.

But now with using MAC-adresses this is not possible, am I right?
Is there any suggestion on how to do this?

In short terms: The device should be exchangeable but also be unique, what is a good approach to do this?

Okay thanks, I did not look at the LAN menu, since my thought was that it only was using WAN.

This is nothing to do with the WAN, WAN is what upstream of the router.

So the way to do it now is to set al the MAC-addresses reservations in the router with the matching IP/ID of table. Is this the quickest and best way?

Exactly what I would do, that way each device has the same code and you change IP address allocations in the router, which is a lot easier than changing them on each device.

Table 1 is empty --> pick a unassigned device --> select on computer table number and click pair to send the data --> device reads and stores it's IP (.101) and is ready to be used.

You can't do that, you can't send it data until it has an IP address, so you can't send it its IP address until it has an IP address... You need some other identifier that does not depend on the IP address but that you can send to it.

In short terms: The device should be exchangeable but also be unique, what is a good approach to do this?

The MAC address is unique. You can change the IP address in the rotuer configuration, so if you want 2 diffrent devices to have the same IP address then you change in the router but you must be careful to only ever power one at once. I think you are getting too hung up on using the IP address as the identifier, maybe invent your own identification scheme and write it into your code.

Static IP addresses are a major source of headaches. I avoid using them at all. In fact, a larger network expands the headaches exponentially. If I need an IP to not change, I do it in the router. Technically it's a permanent DHCP lease but some router front-ends call it static or fixed IP.

So, the unasked question:

Why?

Why do you "need" static IP addresses?

darthvader072:
In short terms: The device should be exchangeable but also be unique, what is a good approach to do this?

Can you explain you require from this? What features need to be unique and whatfeatures need to be exchangeable?

countrypaul:
Can you explain you require from this? What features need to be unique and whatfeatures need to be exchangeable?

Well, I do have some challenges still on this project. Sorry for my late response, I had to think about Perry his response (#16), thanks for that! I think I found a way to make it work.

The device is a lamp, placed on each table(currently I have no maximum in mind, average should be around 50 pieces), the lamp is multicolor so it can change to different colors.
It changes color through UDP, so for example:

packet from computer "01_red" = table 01 is red (so this is my next issue: the packet is sending 01_red, actually I would like to read "01" as tableVariable, in that way I can address each table without worrying about their IP's)

If the battery of the lamp runs out, the complete lamp should be replaced for a spare one with full battery.
Since the users are not technical, I want to make the system convenient.

I was thinking to build a webinterface where the user can address a new device. For example:
Page in front of you --> Select "Replace device" --> "Please select table number" --> Click "01" --> "Are you sure want to replace this device?" --> "Yes" --> "Okay, you can pair the lamp now. Hold it close to the pairing device.." (countdown of 10seconds) --> Holding lamp against RFID writer --> response from webinterface "You have succesfully paired the device to table numer 01"

In the lamp I will add a RFID reader

In short terms:

  • The device should have a ID that is exhangeable. I should be able to read the ID from the UDP packet as variable.

The ID is also unique since it belongs to one table, if the lamp is replaced, the empty device has no/or non-existing ID.