I have multiple photons I am flashing with identical code. When they power up, I want to be able to select whether there is an internet connection available or just wifi. In the setup program I have the following code:
RGB.control(true);
RGB.brightness(64);
RGB.color(255, 0, 0); //red
delay(3000); //wait 3 seconds for me to push the button
if(digitalRead(button1)==1){
RGB.color(0,255,0); //flash green
delay(250);
if (Particle.connected())
Particle.disconnect();
}
RGB.color(255,255,0); //yellow
delay(250);
RGB.control(false);
Most of the code just cycles the color of the LED so I can see what’s going on, but when I am holding down a button - to enter wifi only mode, sometimes I end up with an led breathing green, and sometimes, it flashes green indicating it is still looking for a connection.
I am in system mode(automatic). When I tried semi-automatic, then turned on wifi, I could never get the TCPclient to connect. Oddly enough, in that case, UDP worked fine. - but that is a different issue.
Hi @TimS,
Can you share all of your code? Also, you are making the Photon wait for a button press, the if(digitalRead(button1) == HIGH) stuff should be in loop().
The button read is in the setup code as part of my debug process. My goal is to have the system functional on a local network when there is no internet access. On startup, when the light turns red, I can switch off the cloud functions so I can test it. Ultimately this will be the default, but while I am re-flashing the code regularly, that would be a huge pain.
Here is all of the code up to the point where it is failing. Not much except for initializing variables and setting up the network:
unsigned int localPort = 7234;
UDP udp;
const size_t bufferSize = 1;
unsigned char buffer[bufferSize];
int button1 = D4;
int button2 = D5;
int button3 = D6;
int pwrOff = D7;
char dump='Z';
int rssi = 0;
unsigned long startTime = millis();
unsigned long powerOn = millis();
TCPClient client;
byte server[] = { 172, 27, 0, 111 }; //the ip address of the server pc
IPAddress remoteIP(239,1,1,234);
int remotePort = 7234;
IPAddress multicastAddress(239,1,1,234);
void setup() {
RGB.control(false); //reset normal rgb operation
//pwrTime.start(); //this is the auto off timer
pinMode(button1, INPUT_PULLDOWN);
pinMode(button2, INPUT_PULLDOWN);
pinMode(button3, INPUT_PULLDOWN);
pinMode(pwrOff, OUTPUT);
RGB.control(true);
RGB.brightness(64);
RGB.color(255, 0, 0); //red
delay(2500);
if(digitalRead(button1)==1){
RGB.color(0,255,0); //flash green
delay(250);
if (Particle.connected())
Particle.disconnect();
}
RGB.color(255,255,0); //yellow
delay(250);
RGB.control(false);
udp.begin(0);
buffer[0] = 'A';
IPAddress localIP = WiFi.localIP();
server[0] = localIP[0]; //{ 192, 168, 0, 111 }; //the ip address of the server pc
server[1] = localIP[1];
server[2] = localIP[2];
server[3] = 111;
}
void loop() {
If you want to use SEMI_AUTOMATIC you need to call WiFi.connect() in setup() and should waitUntil(WiFi.ready) before you do any tasks that need the network.
And after that you should add one or two calls to Particle.process() to allow for the WiFi.localIP() to get updated.
It is also better style to use if (digitalRead(button1) == HIGH) or even if (digitalRead(button1)).
Just to confirm, your buttons are closing to 3.3V and not 5V?