8266 As AP and Client?

Is it possible for an 8266 to be an AP and client at the same time? Ideally, using several ESP-01s, I would like to have one or more 8266s acting as APs, but also connecting as clients to another AP. The former is straight-forward, and already working. The latter, I'm not sure if it's even possible, though I can't see why it would not be.

For physical "site" reasons (range, etc.), the network I'm trying to create can be thought of as a hierarchical arrangement of APs and clients:

The main "system controller" would be an AP running on a NODEMCU, managing a network with SSID "Controller"
There would me one, two or three ESP-01s connected as clients to the "Controller" network, each of those also acting as APs of their own networks with SSIDs "Slave1", "Slave2", "Slave3". There would be only one or two clients connected to each of the "Slave" networks.

Is this possible? They could all use either DHCP, or fixed IPs. Doesn't really matter, since this is a completely closed, isolated network, with no connectivity to the outside world - I simply need to move data between the controller and all the slaves, but keeping EVERYONE connected to the network at all times.

Regards,
Ray L.

BTW - I am hoping to be able to do this using the standard AT firmware, not a custom 8266 application.

Regards,
Ray L.

According with the oficial Instruction Set:

You can use the command "AT+CWMODE_CUR=3" / "AT+CWMODE_DEF=3" to enable both AP and STA modes.

Handling sockets won't change, as sockets from AP and STA will be treated the same way in the same code.

I don't think it's possible to distinguish between sockets from AP and sockets from STA, if necessary you can create two servers with different ports at the cost of consuming more sockets.

Sounds like you need a WiFi mesh.

Given the complexity of your project, I would recommend considering to program the ESP-01's directly, instead of using the AT firmware.

Pieter

giova014:
According with the oficial Instruction Set:
https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf

You can use the command "AT+CWMODE_CUR=3" / "AT+CWMODE_DEF=3" to enable both AP and STA modes.

Handling sockets won't change, as sockets from AP and STA will be treated the same way in the same code.

I don't think it's possible to distinguish between sockets from AP and sockets from STA, if necessary you can create two servers with different ports at the cost of consuming more sockets.

Yeah, I've actually now got some test code to try it out, but won't be able to give it a go until tonight. If it actually works, it would make my day. With a max of two clients per "Slave" AP, ports should not be a problem. With potentially three clients into the "Controller", waiting in line, or dealing with retries would not be a problem, since the data rate is very low. Everybody want to send data at roughly the same time, but the messages are very short - perhaps 16 characters or so.
One thing I'm curious about - perhaps you could answer this: When using UDP, as I understand it, I need to setup a listener for incoming messages, and send outgoing messages to a port on the receiving device. Do those two ports need to be different? i.e. - Can I send TO another device on, say, port 1234, and also receive FROM that device on port 1234?
I don't even need the port information to do the transfers. Each message will be tagged, so I know where is originated, and where it needs to go, just from the message contents.
Regards,
Ray L.

PieterP:
Sounds like you need a WiFi mesh.

Given the complexity of your project, I would recommend considering to program the ESP-01's directly, instead of using the AT firmware.

Pieter

I have looked at that, but want that to be my last resort. If I can do it without getting into actual 8266 firmware, it will make my life easier, and, more importantly, make life easier for the guy I'm doing this for, as he won't have to re-FLASH all the 8266s when he builds a new system.
If I do have to go that way, do you have experience with any particular mesh network for 8266? I've seen several, and they all seem very new, and perhaps not terribly robust. That would be a great approach conceptually but I don't want to end up doing a science fair experiment. This has already take up FAR more of my time than I'd hoped.
Regards,
Ray L.

One thing I'm curious about - perhaps you could answer this: When using UDP, as I understand it, I need to setup a listener for incoming messages, and send outgoing messages to a port on the receiving device. Do those two ports need to be different? i.e. - Can I send TO another device on, say, port 1234, and also receive FROM that device on port 1234?

Sending port and receiving port are independent, so they can be equal or diferent.
To receive UDP packets you don't need to (can't) create server (command "AT+CIPSERVER").

When receiving from UDP, you will get a socket, without any port configuration or info.
If you need the port, you can check in the "remote port" (sender port) from command "+IPD" or using the command "AT+CIPSTATUS", parameter "local port" (receiver port).

UDP is less reliable than TCP, because it doesn't have the arrival checks (ACK) and other integrity checks, but you can implement in your program. If you don't need speed or packets are small / sporadic, maybe you can consider using TCP.

  • I mirror the suggestion of direct programming of the ESP's...

Its sounds like you might need/want/benefit from a MQTT type of set-up.

With a subscribe/push type of platform? (message in.. trigger... message out...etc)..

I set up mine on a RaspPi 3.. both MQTT server -AND- installed LAMP.. (for php support and MySQL database logging)..

It is very easy to not only set-up but implement... and the added bonus of data logging and PHP makes its hard to beat.

Can TCP be done using sockets, exactly like with an Ethernet shield? I've done telnet servers and clients with Ethernet, so I could handle that easily, as I already have the necessary code. The attraction of UDP was simplicity, but it's not really turning out to be any simpler.

Regards,
Ray L.

Both Ethenet and Wifi implement the TCP/IP stack, so their socket behave the same way.

To do it exactly equal would need a library for ESP-01 that is equal to Ethernet library.
NodeMCU already comes with it.

TCP is a good choice over UDP in this case.

Woo Hoo! I now have an ESP-01 acting as both AP and client, with other ESP-01s connected to it, and itself connected to my existing WiFi network!

Regards,
Ray L.

What is the trick to disabling DHCP? I'm trying to disable it for both AP and Station modes, but no matter what I do, the IP always comes back with the default DHCP IP, and I am unable to change it.

I am sending:

AT+CWDHCP=2,0 // Disable DHCP
AT+CIPAP=10.0.0.50 // Set AP IP
AT+CIPSTA=10.0.0.50 // Set Station IP

But, no matter what I do, the IP always comes back 192.168.4.1.

Update: Got it. For some reason this has to be done BEFORE creating the WiFiEspClass object. It now works correctly.

Regards,
Ray L.

Something else I don't understand. I am able to connect to my home WiFi network, but pings to the router on that network are not returned. In fact, there appears to be no response whatosever to the ping command. It works fine between clients connected to the 8266. This is really not a problem for my application, but still seems to me it should work.

Regards,
Ray L.

Can you please post what exactly commands are you sending (in order) and what are the exactly responses?

For example:

AT+CIPAP=10.0.0.50

must have double quotes:

AT+CIPAP="10.0.0.50"

I don't know if you purposely left it out when posting or if you are forgetting it.
Better yet, use the commands with "_CUR" or "_DEF" at the end, because they are [@deprecated] without it:

AT+CIPAP_DEF="10.0.0.50"

But, no matter what I do, the IP always comes back 192.168.4.1.

AP or STA IP? Maybe they can't be equal.
Do you test the IP right after in your code? Or after a power cycle? Could be related to using "_CUR" or "_DEF" at the ends of the commands.

Sorry. The commands are generated programatically, and DO have the address in quotes. As I updated above, it seemed to be WHERE in the sequence I was executing the command. If I do it before creating the WiFiEspClass object, it works as expected. Good point on using CUR or DEF. I hadn't noticed I was using the deprecated syntax, though it seems to work exactly as CUR does.

Regards,
Ray L.

Something else I don't understand. I am able to connect to my home WiFi network, but pings to the router on that network are not returned. In fact, there appears to be no response whatosever to the ping command. It works fine between clients connected to the 8266. This is really not a problem for my application, but still seems to me it should work.

Did you configure the proper IP, Gateway and Netmask in the commands "AT+CIPAP" and "AT+CIPSTA"?
In your previous posts, I can't see the gateway and netmask, which are important for the TCP/IP stack work.

Maybe it works between 8266s because the default netmask is the same, but different from the router.

I hadn't noticed I was using the deprecated syntax, though it seems to work exactly as CUR does.

If you see in the link I posted before, without "_CUR" or "_DEF", it will behave as with "_DEF":

• The configuration changes will be saved in the user parameter area in the flash.

I think it's safer to use with "_CUR", as it is guaranteed it will return to a known (working) state every power cycle, and will take only a few lines in setup().

giova014:
Did you configure the proper IP, Gateway and Netmask in the commands "AT+CIPAP" and "AT+CIPSTA"?
In your previous posts, I can't see the gateway and netmask, which is important for the TCP/IP stack work.

Maybe it works between 8266s because the default netmask is the same, but different from the router.

IP and netmask are correct, the the 8266 IS connected to the router/gateway. How do set the gateway in the 8266? I assumed that was done as part of establishing the connection.
Regards,
Ray L.

IP and netmask are correct, the the 8266 IS connected to the router/gateway. How do set the gateway in the 8266? I assumed that was done as part of establishing the connection.

When using DHCP, it is done automatically.

You can set them in the commands "AT+CIPAP" and "AT+CIPSTA" at the second (gateway) and third (netmask) paramater.
An example, from the Instruction Set:

AT+CIPSTA_DEF="192.168.6.100","192.168.6.1","255.255.255.0"
AT+CIPAP_DEF="192.168.5.1","192.168.5.1","255.255.255.0"

It seems the gateway was NOT getting set by the router. I now set it explicitly, and I CAN ping outside domains by name, but NOT by IP. Can't even ping the router itself by IP. Odd, but I can live with it...

Regards,
Ray L.