Sometimes flashing green then back to blue

From time to time, my photon will flash green for a couple hours and then go back to cyan. It does not matter how close it is to my router…

THANK YOU IN ADVANCE FOR ANY HELP!!! IT IS DRIVING ME CRAZY!!!

here is the code-

    String lastrun = "not yet";
    String whatsup ="";
    String xml_result;
    boolean sleeptime = false;
    String whatwasup = "";
    String whatwasb4 = "";
    boolean requested = false;

    String ender =", Q"; //adds at the end of the meeitn
    String ender2 =", Q ";

    int ErrorCount  =0;
    boolean Dreben= false; // holds value of if things got messed up and the Arduino did not find a Q
    String commander = "";  // holds value of last command
    int Type = 5; // holds value of which function was called


    void setup() {
        String data = String(10);

        
        Time.zone(-6);

        Particle.subscribe("hook-response/google_doc_webhook", myHandler, MY_DEVICES);

     	Particle.variable("XML_UPDATE", xml_result);
    	Particle.variable("timer", lastrun);
    	Particle.variable("whatsup", whatsup);
    	Particle.variable("whatwasup", whatwasup);
    	Particle.variable("whatwasb4", whatwasb4);
    	Particle.variable("Dreben", Dreben);


    	Spark.function("sendData", sendData);
        Spark.function("sendLocation", sendLocation);
        Spark.function("sendSteps", sendSteps);
        Spark.function("sendReset", sendReset);
    	Spark.function("sendXML", sendXML);

    	

    	Serial.begin(9600);
    	Serial1.begin(9600);
    	
    	int ttime = Time.hour();
        Particle.publish("google_doc_webhook", data, PRIVATE);
        lastrun= String(ttime);


    }


    void loop() {
        String data = String(10);
    	
    	
    	if (Dreben == true && ErrorCount<5)
    	{
    	    ErrorCount ++;
    	    ender = ender + "reseted X";
    	    delay(7000);

            switch (Type)
                {
                    case 1:
                    sendReseter(commander);
                    break;
                    
                    case 2:
                    sendLocationer (commander);
                    break;
                    
                    case 3:
                    sendStepser (commander);
                    break;
                    
                    case 5:
                    sendXMLer(commander);
                    break;
                }
            delay(100);
          whatsup = "Dreben fewer than 5 but really " + ErrorCount;

        }

        else if (Dreben == true)
        {
            whatsup = "This had an error x " + ErrorCount;
            ErrorCount = 0;
            Dreben = false;
            ender =", Q";
            ohno();
        }


    	if(Serial1.available()) 
    	{
    	String holder = Serial1.readString();
    	    if (holder.indexOf("Dreben") > 0)
    	       {
    	        holder=holder.indexOf("Dreben");
    	        Dreben = true;
    	       delay(1000);
    	       }
        	else
        	{
    	    (Dreben = false);
    	   ender =", Q";
        	}
    	  
    	whatwasb4 = whatwasup;
    	whatwasup = whatsup;
    	whatsup = holder;
    //	    Serial1.flush();

    	}
    	
    	Particle.publish("google_doc_webhook", data, PRIVATE);


    	int ttime = Time.hour();
    	
    	
    	if ((( ttime % 2) == 0) && !requested)
    	{
    	Particle.publish("google_doc_webhook", data, PRIVATE);
    	requested = true;
    	lastrun= String(ttime);
    	}
        
        if (!(( ttime % 2) == 0) && requested)
    	{
    	Particle.publish("google_doc_webhook", data, PRIVATE);
    	requested = false;
    	lastrun= String(ttime);

    	}
    		
    	
    	
       /*
        
        
        
        if ((ttime > 8) && (ttime < 18) ){
          tomorrow=false;
         }
         else
         {
           tomorrow = true;
         }
        */
        
        //don't show if it is late

        if ((ttime > 7) && (ttime < 20) ){
              sleeptime=false;
        }
            else
         {
          sleeptime = true;
        }

    }


    int sendReset(String command) {
    //	Serial.print("INCOMING: ");
    	//Serial.println(command);
    	commander = command;
    	Type = 1;
    	command = "RRRRRRR" + command + ender;
    	Serial1.println(command);
    	return 1;
    }

    int sendLocation(String command) {
    //	Serial.print("INCOMING: ");
    //	Serial.println(command);
    	commander = command;
    	Type = 2;
    	command = command + ",0" + ender2;
    	Serial1.println(command);
    	return 1;
    }


    int sendSteps(String command) {
        commander = command;
    	Type = 3;
    	command = command + ",4" + ender;
    	Serial1.println(command);
    	return 1;
    }


    int sendData(String command) {
        commander =command;
    	Type = 4;
    	Serial1.println(command);
    	return 1;
    }

    int sendXML(String command) {
        commander = command;
    	Type = 5;
        xml_result="PPPPPP" + xml_result + ender;
        Serial1.print(xml_result);
        return 1;
    }


    void myHandler(const char *event, const char *data)
    {
      // Handle the integration response
        xml_result = String(data);
        Particle.publish("XML Result", xml_result, PUBLIC);
        xml_result = tryExtractString(0, xml_result, "thread", "Stepper");

    }

    //
    //
    //


    String tryExtractString(int matchNum, String str, const char* start, const char* end) {

        if (str == NULL) {
            return "";
         }
         
        int count = 0;
        int lastIdx = 0;
             
             while (count <= matchNum) {
            int idx = str.indexOf(start, lastIdx);
            int endIdx = str.indexOf(end, lastIdx + idx + strlen(start));
            lastIdx = endIdx;
            if (count == matchNum) {
                return str.substring(idx + strlen(start), endIdx);
            }
            count++;
        }
    }



    void sendReseter(String command) {
    	commander = command;
    	Type = 1;
    	command = "RRRRRRR" + command + ender;
    	Serial1.println(command);
    }

    void sendLocationer (String command) {
    	commander = command;
    	Type = 2;
    	command = command + ",0" + ender2;
    	Serial1.println(command);
    }

    void sendStepser (String command) {
        commander = command;
    	Type = 3;
    	command = command + ",4" + ender;
    	
    	Serial1.println(command);
    }


    void sendXMLer(String command) {
        commander = command;
    	Type = 5;
        xml_result="PPPPPP" + xml_result + ender;
        Serial1.print(xml_result);
    }

    void ohno()
    {
        String Stringer="PPPPPP" + ender;
        Serial1.print(Stringer);   
    }

My usual suspects are String objects that eventually produce enough heap fragmentation that there won’t be enough space to perform a succesful reconnect after a connection loss.
Consequently I’d replace String objects with good old C-strings (char arrays).

Anyhow, I’d reduce the use of strings of any sort and rather go with “numeric” types (e.g. enum) and maybe employ const char arrays if you really need some predefined texts.

BTW, for consistency, replace the deprecated Spark.xxxx() with Particle.xxxx()

1 Like

Thanks! I will give it a try!!!

Looks like it was actually a power issue. I redid the code using char’s, but still got the green light. When I powered the Photon through the usb, rather than the VIN, it seems (so far) to work. Probably related to:
https://docs.particle.io/datasheets/photon-datasheet/

Going to try using a capacitor or a diode while keeping it powered from the VIN and see if that helps.

THANKS FOR THE HELP!

@jpalay, how are you powering it through Vin (voltage, amps available)?

I am using this power adaptor - https://www.adafruit.com/product/658 - so 5V - 10 Amp.
=BUT=
I am plugging this adapter into an arduino with two motorsheilds (https://www.adafruit.com/product/1438) and then connecting the photon from the 5v from the motor shield.

@jpalay, that supply should be fine if you are not drawing more than 10A. I suggest adding a large (470 - 1000uF) capacitor near the entry point of your power to the Photon. Add a 0.1uF capacitor close to the Vin pin as well. These two caps will filter your power into the Photon and hopefully resolve the issue. You can add a diode in line with the positive side of the supply if you want to add reverse voltage protection but otherwise it should not be needed.

I will give it a try! THANKS!!!

Actually - quick follow-up question: do I wire the capacitor in series or in parallel? From what you said, it looks like it series…no? +5 to capacitor to VIN?

Thanks so much!
Josh

@jpalay, both capacitor are connected between Vin and GND of the Photon. Make sure to have the polarity correct on the large cap. Ideally, both are close to the Vin pin, with the smaller cap closest if possible. Now that I think of it, the larger cap could be a 330uF to 470uF cap.

Perfect - thank you!

1 Like

The 470uF cap did not work, but a 1000uf cap with a 0.1uf Cap seems to have done the trick. The wifi still goes out, but that only occurs now when I am sitting right by it and moving metal stuff around (my guess interferring with the signal). Also, now, it comes right back after dropping. Thanks again! Josh

1 Like