Can a TCP web server work without Internet connection?

Hi everyone,
I’ve the following question for you: I’ve a developed a small software which enables a Photon to become a small web server, which reply some TCP request. If an Internet connection is available, there is no problem with the system, but one it goes away, some problems occurs. My question is: Can the photon works without Internet? My system need just to work within a LAN, so the photon will be connected to a demo WiFi network without Internet for the most time. Thank you in advance for your reply, and I hope to found a simple solution for this problem!

1 Like

Yes, it is possible.
Just have a look at the docs for SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL)

Thank you for your reply! That’s what I was thinking about. Is there any problem if I put the photon in SYSTEM_MODE(MANUAL) for develop application of the web build? I mean, Can I upload my code over the OTA if the device is connected to Internet and in SYSTEM_MODE(MANUAL)? If put the device in SYSTEM_MODE(MANUAL), the only things I should added is wifi.connect() and I can delete the Particle.process(), isn’t it? Thank you in advance for your reply.

To use TCPServer with SYSTEM_MODE(MANUAL) you need to do something like this


void setup()
{
  WiFi.connect();
  waitUntil(WiFi.ready);
  // from now on you can use WiFi
}

And for OTA flashing you need to either call Particle.connect() and call Particle.process() every few seconds, or you put the device into Safe Mode.

1 Like

Thank you for your reply. If I follow your hints, will be there any problems if an internet connection isn’t available? I mean will the function Particle.process() be blocking if not internet connetion is available? If I don’t put any Particle function the only way to update ota firmware is to put Photon in safe mode? The last question: if i put photon in manual mode, should I set credentials of the WiFi in code? Thank you again in advance for your reply

Particle.process() is non-blocking, so you are safe there.

If your WiFi has no internet connection at all, you’d have a hard time doing any OTA flashing anyway, but if you have the option to have internet, there are several ways besides Safe Mode to connect your device to the cloud

  • You can add a button to do so
  • You can send a TCP packet to your server to trigger this
  • You could have the device perform a cloud connected Particle.process() loop for about half a minute after reboot, and after that Particle.disconnect()

About WiFi credentials. The device remebers up to five sets of credentials, so there should not be any need to provide them via code, unless you have an explicit use case for this, but then you should be aware of the fact of flash wear, so you should not perform WiFi.clearCredentials()/WiFi.setCredentials() on a regular basis.
WiFi credentials can be set, not only via the Particle apps, but also via USB serial connection (e.g. Particle CLI or PuTTY or any other serial terminal program).

1 Like

Thank you for your very clear reply. It’s a good news about not blocking for Particle.process();. My last doubt; what’s the difference between manual mode and automatic mode if no internet connection is avaiable, just for the purpose of a WiFi enviroments? The idea is to work just in LAN and connect the lan to internet just for ota service.

As the docs state, in MANUAL you have to call Particle.process() on a regular basis to keep the cloud connection happy, while in SEMI_AUTOMATIC this will be done - once cloud-connected - each time your code drops out of loop() or inplicitly when the sum of delay() times exceedes 1sec.
In non-cloud-bound mode there is as good as no difference.

1 Like

Thank you. In the docs there is this statement: “Particle.process() is a blocking call, and blocks for a few milliseconds” in the documentation for all the function. So if no internet connection is available, will the function blocking the other process, because can’t reach the cloud?

Unless you have very time critical tasks you can easily ignore that statement since in non-cloud-bound situations the time lost is due to some if statements that will check system mode and connection status in a manner that won’t give you much headache :wink:

If it’s not meant to be connected (since you never called Particle.connect()) it will bail out in a rather early if block - reducing “blocking” periode to a few hundred nanosec.

Thank you for your reply. For my situation, a simple TCP server, will be fine to keep in automatic way, without internet connection to the cloud, but just with a WiFi LAN? In this case I will not lost the ota firmware updates, when I connect it to Internet.
Have you got some hints to comunicate without problem of security within a WiFi lan, from an Android application to photon over TCP? Thank you in advance for your support

No, I’d advise against AUTOMATIC, since your code will only start running after your device got cloud connected at least once.
The easiest is SEMI_AUTOMATIC - as suggested earlier.

What do you consider secure?
WPA2 Personal is the “most secure” WiFi encryption supported by the Particles, but if you want more then that, you’d need to add encryption to your code since TCPServer/TCPClient are not encrypted beyond what the WiFi layer does while “in air”.
But you could have a look at the HTTPS library provided by the glowfi.sh team
HTTPS client is here for the Photon! - by the glowfi.sh Team

1 Like

Thank you for your reply. So with SEMI_AUTOMATIC, the initial connection with WiFi will be made and then if no internet connection is available, the software will run, isn’t it? So I’ll just need to put SYSTEM_MODE(SEMI_AUTOMATIC); and no Particle.process() in the loop, because it will be called at each loop() end, but due to the fact that no internet connection is available, it will just few msec lost in the if, isn’t it?

I saw about https client and I think is a very good solution and this is good to comunicate from Photon to cloud. My idea is to let Photon work as server and not as client. Is there the same library for https server? Thank you in advance for your support, you’re very good!

Yes, now you pretty much got it all :+1:

I’m not really into HTTPS, but I’d assume that HTTPS Client can be used as the active core of a HTTPS server, just like TCPServer uses TCPClient internally.

Thank you for your reply. By the way, with SEMI-AUTOMATIC, the connection with the WiFi lan works fine, but if I’m attaching an Internet connection, the photon don’t seem to connect to the cloud and so I can’t load the firmware over OTA. The only solution is to put in safe mode the photon. Do you think is normal this situation?

In order to hop onto the cloud you’d need to call Particle.connect().
But since there is no prebuilt way for the device to be informed if your WiFi has or has no internet connection. The only way to know is to try to contact some outside address (e.g. WiFi.ping()), try to connect and Particle.disconnect() again once you are done (or if Particle.connect() fails for a given periode).

1 Like

Hi. Did you successfully finished your code?