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.
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.
This is possible with the Wifi.ready() function, 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():
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() 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.
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.
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
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.
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.
@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.