# Connecting without the cloud

**URL:** <https://community.particle.io/t/connecting-without-the-cloud/20311>\
**Category:** General\
**Created:** [February 21, 2016, 3:38pm UTC](https://community.particle.io/t/connecting-without-the-cloud/20311 "2016-02-21T15:38:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![matspeed](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matspeed](https://community.particle.io/u/matspeed)\
**Post date:** [February 21, 2016, 3:38pm UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/1 "2016-02-21T15:38:05Z")

</div>

Hi,

im trying to set up a Photon with Blynk in a school environment where I cannot use the spark cloud due to ports being closed.

I have already set up an local server for Blynk.

The problem is when I power my photon I tries and connects to the cloud first, can I solve this by creating a local server that doesn’t use the internet or by using the manual/semi manual modes

I’d like to still be able to upload code although i could do this using my phones internet via safe mode.

Thanks  
Matspeed

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 21, 2016, 7:03pm UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/2 "2016-02-21T19:03:30Z")

</div>

If you search for _“without cloud”_ or _“without internet”_ you’ll probably find loads of references to `SYSTEM_MODE(SEMI_AUTOMATIC)` and/or `SYSTEM_THREAD(ENABLED)`.

With either way you’ll be able to OTA flash when finding a stored cloud connected WiFi network.

---

<div class="post-metadata">

**Author:** ![matspeed](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matspeed](https://community.particle.io/u/matspeed)\
**Post date:** [April 25, 2016, 9:24am UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/3 "2016-04-25T09:24:15Z")

</div>

Hi,

I have looked into the manual and semi-automatic modes, but I still don’t understand how you’d program it in these modes.

I have a photon set up that I want it to run my code first then connect to wifi if its available and if not continue running code.

but this is only temporary and I want to be able to re-flash the app when it’s ready.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 25, 2016, 9:48am UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/4 "2016-04-25T09:48:24Z")

</div>

Hi @matspeed

> [@matspeed](#):
>
> I have looked into the manual and semi-automatic modes, but I still don't understand how you'd program it in these modes.

Using dfu-util to upload code over USB is a viable solution.

You may want to try [the script I created](https://github.com/nrobinson2000/po-util). It installs the the Particle toolchain, dfu-util and dependencies for you, and makes it easier to build your code. [GitHub - nrobinson2000/po-util: Classic Edition of po-util: The Ultimate Local Particle Experience for Linux and macOS](https://github.com/nrobinson2000/po-util)

[![Build Status](https://travis-ci.org/nrobinson2000/po-util.svg?branch=master)](https://travis-ci.org/nrobinson2000/po-util)

> [@matspeed](#):
>
> I have a photon set up that I want it to run my code first then connect to wifi if its available and if not continue running code.

This is possible with the [`Wifi.ready()` function](https://docs.particle.io/reference/firmware/photon/#ready-), which will return true once the device is connected to the network and has been assigned an IP address, which means that it's ready to open TCP sockets and send UDP datagrams. Otherwise it will return false.

This could work with something like this in `setup()`:

```cpp
if (WiFI.ready() == true)
{
     // Do some code for when there is WiFi
}
else
{
     // Do code for when there is no WiFi
}

```

If you want to run code before connecting to WiFi use [`STARTUP()`](https://docs.particle.io/reference/firmware/photon/#startup-) to call a function containing the code you want to run before the device connects to WiFi.

Typically an application will have its initialization code in the setup() function. This works well if a delay of a few seconds from power on/reset is acceptable.In other cases, the application wants to have code run as early as possible, before the cloud or network connection are initialized. The STARTUP() function instructs the system to execute the code early on in startup.

---

<div class="post-metadata">

**Author:** ![matspeed](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matspeed](https://community.particle.io/u/matspeed)\
**Post date:** [April 25, 2016, 11:02am UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/5 "2016-04-25T11:02:00Z")

</div>

Hi,

Cheers for the quick reply.

I will look into this and use semi-automatic mode, I was looking into using System Threading and just experimented with using this however i couldn’t get it run the actual code I’ve have written.

Thanks  
Mathew Speed

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 25, 2016, 11:57am UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/6 "2016-04-25T11:57:33Z")

</div>

> [@nrobinson2000](#):
>
> Using dfu-util to upload code over USB is a viable solution.

A less "invasive" way (since it doesn't require _anything_ installed at all) would be to put the device into Safe Mode, which doesn't run any application code but connects to the cloud for OTA flashing.  
Or even `particle flash --serial` which does not require dfu-util installed.

`WiFi.ready()` just tells you if your device is connected to your WiFi network, but not if it's connected to the cloud. For this you'd use `Particle.connected()`. For OTA flashing or Particle functions, variables, publish and subscriptions you'll need cloud connection.

But what exactly is so difficult to understand with `SYSTEM_MODE()`?

Try this

```cpp
SYSTEM_MODE(SEMI_AUTOMATIC) // your code will execute without attempting to connect to WiFi or cloud

const int pinButton = D0; // attach a button from D0 to GND
const uint32_t timeout = 15000; // 15sec before retries
uint32_t ms; // multi purpose timing variable  

void setup()
{
  pinMode(pinButton, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(pinButton) && millis() - ms > 5000) //
  {
    if (!WiFi.ready())
    { // first hit connect to WiFi network
      WiFi.on();
      WiFi.connect();
    }
    else if (!Particle.connected())
    { // once connected next hit connect to cloud
      Particle.connect();
    }
    else if (Particle.connected())
    { // if we currently are connected, next hit switches off the WiFi module
      WiFi.off();
    }
    ms = millis();
  }
}

```

* * *

@nrobinson2000, thanks for putting this script together, but you seem to be trumpeting this horn in way too many threads, even if that's not really the question like here.

---

<div class="post-metadata">

**Author:** ![matspeed](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matspeed](https://community.particle.io/u/matspeed)\
**Post date:** [April 25, 2016, 12:08pm UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/7 "2016-04-25T12:08:54Z")

</div>

Hi,

The problem is I’m facing is I can’t access the photon and can’t add a button to manually connected to the cloud, so I began to look at system threading and allowing my code to run independently of the wifi and cloud code. or if it could check for the wifi network and then if it’s available to connect and connect to the cloud which is what I thought wifi.ready with other code would be able to do.

My situation is a school environment where ports are blocked and I can’t use the schools wifi. I have a photon connected to 3RGB LEDs inside case I have made.

I have written code to fade these LEDs and want as soon as I turn on my photon for it to run the fading code and then if I use a hotspot on my phone it picks up and connects to that and then to the cloud.

Thanks  
Mathew

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [April 25, 2016, 12:48pm UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/8 "2016-04-25T12:48:40Z")

</div>

Yeah it might seem like I’m advertising it a bit, but I think it deserves a mention in the Particle documentation.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 26, 2016, 8:00am UTC](https://community.particle.io/t/connecting-without-the-cloud/20311/9 "2016-04-26T08:00:27Z")

</div>

@matspeed, after testing `SYSTEM_THREAD(ENABLED)` are you still facing problems or do you have any other questions in this regard?

`WiFi.scan()` and `WiFi.gerCredentials()` might be worth a look in order to find out if a known network is available, rather than just having the system thread permanently attempt and fail to connect.
