Cannot port forward to Spark listening with TCPServer [SOLVED]


I am very new to Spark, and I am having some trouble I could use help with.

I have flashed the following example sketch from documentation:

#include "application.h"

// Do not connect to the Cloud

const int LED = D0;

#define LISTEN_PORT 6666

TCPServer server(LISTEN_PORT);
TCPClient client;

void init_serial_over_usb() {
	digitalWrite(LED, HIGH);


	while (!Serial.available()) SPARK_WLAN_Loop(); 

	digitalWrite(LED, LOW);

void connect_to_wifi() {
	Serial.println("Connecting to WiFi...");

	Serial.println("Acquiring DHCP info:");
	while (!WiFi.ready()) SPARK_WLAN_Loop();
	Serial.print("SSID: ");	Serial.println(WiFi.SSID());
	Serial.print("IP: "); Serial.println(WiFi.localIP());
	Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());

void setup() {
	pinMode(LED, OUTPUT);


	// start listening for clients
	Serial.print("Listening on "); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.println(LISTEN_PORT);

void loop() {
	if (client.connected()) {
		Serial.println("Client connected!");
		// echo all available bytes back to the client
		while (client.available()) {
	} else {
		// if no client is yet connected, check for a new connection
		client = server.available();
		Serial.println("Waiting for connection...");

Here’s the network info printed to the serial terminal when Spark starts:

Connecting to WiFi...
Acquiring DHCP info:
SSID: vulcan
Listening on

The test program work fine, if I connect to it from a device on the same LAN. So something like this works well:

nc 6666

The connection succeeds and my input is echoed back to me. So far so good.

Next, I try to perform exactly the same test, but from a device on another LAN. In order to achieve this I configured the WiFi router to forward port 6666 to, which is reserved for the Spark’s MAC address. This does not work - the following simply times out:

nc 6666

In order to make sure that my port forward is correct, I changed the forward IP address to another Linux box on the same network as the Spark, and made sure that port forwarding is working properly with:

nc -l 6666

Switching the IP back to the Spark, once again leads to time outs.

So, I can successfully port forward to a Linux box, but not to Spark running TCPServer to listen for connections. Any idea what might be going wrong here?

I wonder if this might be related to this issue:

Any advice would be appreciated.


If you port forward… it should be reached via the same IP address that the core is on am I right?

How is the network setup like?

Hi Kenneth,

Sorry, I am not sure what you are asking in the first sentence.

My network setup is as follows:

There are 2 networks in the house. First is a Gigabit switch wired network, with a Linux box (gatekeeper) acting as the router at Gatekeeper has a lot of network cards in it, but the 2 we care about is the one for the LAN (, and one for the WiFi router (

Second network is formed with a WiFi switch - that’s where the Spark lives.
Gateway: (this points to a network interface on gatekeeper)

DHCP is configured to assign addressed in range.

Gatekeeper has some iptables rules which take the traffic from the WiFi router ( and forward it in such a way that wireless devices can connect to anything on the wired LAN or Internet.

B/c the WiFi router exposes only its own WAN IP address (, accessing any of the wireless devices requires a port forward to get past the NAT.

So if the Spark has IP address of, I can create a mapping for port 6666 and access it at At least that’s how it worked with all other wireless devices I have used with this setup for about a decade. Spark is the first device I ever had trouble with.

This setup is documented in detail and with some pictures here:

Access credentials: wiki/wiki

I don’t know where you problem is but it is most likely in the NAT in the WiFi router. You even say in your wiki that this is the big problem with this setup.

Are you sure port 6666 is open on your wireless router? That port (IRC) is used by lots of old virus/trojan programs and is often blocked by default. You have to open it inbound and outbound on the D-Link router. The wireless router will not typically block outgoing ports but inbound traffic is a different story.

Can you try a different port number temporarily? Maybe one that is already in use for another device to test? Do your other devices on the wireless side that use port forwarding initiate an outbound connection or do they also wait for an inbound one like you want your Spark to?

You don’t say what the serial output of the Spark in the failed case–is it no output? That would mean that the forwarding isn’t working.

Can you sniff the wire with Wireshark or tcpdump on the wireless network? I would think that the packets never got there, but that will tell you for sure.


@bko You were totally right! It was a wireless router security configuration problem.

This is kind of embarrassing now, but the problem was that the last time I added a new wireless device was so long ago, that I forgot that I had MAC address filtering turned on! This is why my test worked when I directed the port forward to an old wifi connected laptop.

After adding Spark’s MAC address to the list, everything worked as expected.