Wifi mesh-like network using nodeMCU

#nodemcuesp8266esplorerluawifi

I have 2 nodeMCU modules, which I want to connect to an MQTT broker and send some data every 5 seconds.

The topology I am trying to achieve is sth like [router]<==[nodeMCU#1]<==[nodeMCU#2]

It looks like [nodeMCU#1] is a wifi extender, but at work we are planning on using multiple nodeMCU's to use in a mesh-like way for an IoT application.

On both of them I flashed the latest (float) release that I downloaded from here https://github.com/nodemcu/nodemcu-firmware/releases/tag/0.9.6-dev_20150704 using nodeMCU-flasher.

For this project wrote 2 lua scripts and I have uploaded them to the modules using ESPlorer.

For the first module it connects in STATIONAP mode to my work wifi, and creates its own network with SSID nodeMCUwifi, with a basic password of 10 characters "1234567890". After it connects the script sends random values to an MQTT topic.

For the second module connects to nodeMCUwifi correctly, it is assigned an IP address, but it cannot connect to the MQTT broker.

When I try to connect to nodeMCUwifi using my smartphone (just to test the connection, I have no intention of using this system for heavy internet load, only MQTT messages) I get a message "authentication error occured" even though I have typed the password correctly, or (in rarer cases)it connects but disconnects immediately.

I would appreciate any ideas to resolve this issue. Thank you.

EDIT: At the AP configuration I added the auth parameter set to 3, I am pasting a part of my code below.

cfg.ssid="ESP8266_"..node.chipid(); 
cfg.pwd="1234567890"
cfg.auth=3
wifi.ap.config(cfg);

Now my laptop and my smartphone connect to the wifi created by the module, but still have no connectivity to the internet. I can ping the module, but I can't ping 8.8.8.8 or the MQTT broker IP or anything else.

Best Answer

As I understand, [nodeMCU#1] creates a wifi network named nodeMCUwifi. Then the others are trying to connect to nodeMCUwifi. As You stated:

Now my laptop and my smartphone connect to the wifi created by the module, but still have no connectivity to the internet. I can ping the module, but I can't ping 8.8.8.8 or the MQTT broker IP or anything else.

I assume your laptop and smartphone connects to the nodeMCUwifi network. Now you want this [nodeMCU#1] act like layer 2 switch or something like router(since SOFTAP creates its own ip network).

With all this information on hand, I can say that the purpose you are trying to achieve is not possible with esp. Since esp has not enough resources to act like switch or similar. Also (currently) there is no software implemented in esp to achieve that.

However; if only you want the system to transfer certain type of traffic (such as MQTT) over a certain protocol (such as UDP) it is possible to relay the messages like a mesh structure but this will work one-way only. In this configuration the nodes will act more like repeater but on upper layers of OSI. For this idea i cannot provide sample code but I will provide pseudo-code below:

Configuration

> Each node must be configured to build a chain structure. That means each SOFTAP node should have different SSID or TCP settings. For example:

router(192.168.0.0)<----[NODE#1(SOFTAP0)]<----[NODE#2(SOFTAP1)]<---...
...
...
...<----[NODE#n(STATION)]

> Each node must have a TARGET IP address according to setup. This means every node will send the MQTT message to that address.

> If a node is configured as a SOFTAP, this means this system is a TARGET and must listen to a specific port.

> If a SOFTAP node receives some data on the port that is listening to, it should send the data immediately (or buffering) to its TARGET node.

> As a result of this chaining operation the message, no matter what is the source, will reach to the end of the chain.

CODE : SOFTAP

...soft ap config...
listento.port: udp.965
on("receive"):
    create.conn:TARGET
    send(DATA_RECEIVED)
    close.conn:TARGET

CODE : STATION

...station config...
...Do some logic...
data=gather.data()
create.conn:TARGET
send(data)
close.conn:TARGET
Related Topic