MQTT doesn't like static IP address

It would seem problems are going to keep happening to me.

I have got the PING issue resolved.

Moving on from that, with the NodeMCU arduino and the next problem arises.

As it was originally - and only for testing - I used DHCP.
Now I am wanting to move on to FIXED IP addresses so it is easier to find/see machines/states.

The other quirk is that I am using MQTT as well. I say quirk only because something is happening that I don't understand HOW it is happening.

Status quo, the sketch works. The Arduino/ModeMCU connects to my WAP.
It gets an IP and it talks to my MQTT quite happily. Sending the dummy test messages which for now are ok.

But when I make it a STATIC IP address, the MQTT fails.

This is the wifi-setup part of the code.

void setup_wifi() 

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");


  WiFi.begin(ssid, password);

//   With these lines as they are:  It works.
//   If I make them active and set the IP address, MQTT won't talk.
//  IPAddress ip(192,168,1,2);   
//  IPAddress gateway(192,168,1,1);   
//  IPAddress subnet(255,255,255,240);   
//  WiFi.config(ip, gateway, subnet);

  while (WiFi.status() != WL_CONNECTED) 

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

I've added comments in the code, but here is the story:
The network is 192.168.1.x/28
The WAP is .1, I want the Arduino thingy to be .2 and the broadcast address is .15

With the lines active this is what I see in the serial window:

Connecting to PiNet
WiFi connected
IP address:
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

But it is being PINGed as .2

How is changing the IP from DHCP to static changing the fact MQTT won't talk?

Have you updated the WAP DHCP address range to exclude from the address pool? If not then you may have two devices with the .2 address.

I don't know how configurable your WAP is, but my own setup uses DHCP for everything with static IP/MAC assignments setup in the WAP. This allows you to manage all IP/MAC address combinations in one place rather than having a mix of DHCP and non-DHCP assignments.

From the RPI config files:


allow-hotplug wlan0
iface wlan0 inet static
##  14 usable addresses.  (Well 13, as this is one of them)
#  network


interface eth0
static routers=
static domain_name_servers=
static domain_search=

static routers=

denyinterfaces wlan0


cat hostapd/hostapd.conf 

The wpa_supplicant file is nothing special.

This is getting a bit more "Ras Pi" than Arduino.

But that is what is on the RPI.

I have (from the other thread) a ping test and when I connect the Arduino, it only connects one IP address.
via DHCP it is 7. Via static it is 2.
via DHCP the MQTT works. I get messages coming through and I can send messages to the board. All good.
via static, it pings. But MQTT just won't have a bar of it.

Anything else needed?

Just found this which I think is contributing to the problem:

WiFi config()

And it shows this bit of code:

#include <SPI.h>
#include <WiFi.h>

// the IP address for the shield:
IPAddress ip(192, 168, 0, 177);    

char ssid[] = "yourNetwork";    // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;

void setup()
  // Initialize serial and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);  // don't continue


  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:

  // print your WiFi shield's IP address:
  Serial.print("IP Address: ");

void loop () {}

How ever, if I try to do that I get an error:

 WiFi.begin(ssid, password);

  // the IP address for the shield:
  IPAddress ip(192, 168, 1, 2);    

exit status 1
no matching function for call to 'ESP8266WiFiClass::config(IPAddress&)'


allow-hotplug wlan0
iface wlan0 inet static
##  14 usable addresses.  (Well 13, as this is one of them)
#  network

The network value is definitely wrong. The commented out value is the right one.
I don't know how the Raspberry Pi interprets these conflicting values but you might have a misconfigured network interface, so the Arduino probably cannot reach the Raspi.