Photon intermittent connection


#1

Hello,

I’ve been trying to figure this out for a while now and I just cant seem to find an answer. I currently have 8 photon connected to different machines. They take a reading whenever the machine starts and stops. It counts the interval between the start and stop, then publishes that number to a thinkspeak page.

I am having trouble with one in particular, it will constantly disconnect and reconnect to the cloud. The photon has a constant supplied voltage through the Vin pin, and has an external wifi antenna.

Any suggestions or help with this would be greatly appreciated.

This is the code we are running on all of the Photons we have.

/*This application measures the up and down times of the machines throughout Diehl steel.
Signal wiring is as follows:
Green: D2
Yellow: D0
Ground: Left side pad
Vin: Top left pad
*/

// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>


TCPClient client;

int sendDelay = 60000;
//Be certain to change the channel number and API key for each device
unsigned int myChannelNumber = ;
const char * myWriteAPIKey = "";


//These two variables are the pins sending and receiving signal, respectively

const int sigIn = D0;
const int sigRead = D2;

//These variables track up and down time on each machine; time is measured in fifteen second intervals

int upTime = 0;
int upMinutes = (upTime / 4);
int downTime = 0;
int downMinutes = (downTime / 4);
int sigSent = LOW;
int inLoopRead = 0;
int idle_time = 0;
double runTme = 0;

bool isRunning = false;


//The following functions are available to call via the cloud console
int TiFunction(String command);

//This sets the photon to utilize the external antenna
STARTUP(WiFi.selectAntenna(ANT_EXTERNAL));

void setup() 
{
pinMode(sigIn,OUTPUT);
pinMode(sigRead,INPUT_PULLDOWN);
pinMode(D7,OUTPUT);
Particle.function("Ti-function", TiFunction);
Particle.variable("Uptime",upMinutes);
Particle.variable("Downtime",downMinutes);
Time.zone(-4);
ThingSpeak.begin(client);
}

void loop() 
{
    upMinutes = (upTime / 4);
    downMinutes = (downTime / 4);
    
    if(upMinutes >= 1 || downMinutes >= 1) 
    {
        runTme = upMinutes / (downMinutes + upMinutes) * 100;
    }
    
 pinResetFast(sigRead);
 digitalWrite(sigIn,HIGH);
 
 sigSent = LOW;
    
 sigSent = digitalRead(sigRead);
    
    if(pinReadFast(sigRead) == HIGH) 
    {
        
        isRunning = true;
        
        Particle.publish("machine-start","Machine started.", PRIVATE);
        
        
        while(isRunning) 
        {
             digitalWrite(sigIn,HIGH);
            
           int inLoopRead = digitalRead(sigRead);
           
             digitalWrite(sigIn,LOW);
            
            if(inLoopRead == HIGH)
            {
                delay(15000);
                upTime++;
                ThingSpeak.writeField(myChannelNumber, 1, upMinutes, myWriteAPIKey);
            }
            else 
            {
                isRunning = false;
            }

        }
          Particle.publish("machine-stop","Machine stopped.", PRIVATE);
          idle_time = 0;
          ThingSpeak.writeField(myChannelNumber, 1, upMinutes, myWriteAPIKey);
    }
    else 
    {
        if(idle_time == 0) 
        {
         Particle.publish("idle","Machine idle.",PRIVATE);
         idle_time++;
        }

    }
        ThingSpeak.writeField(myChannelNumber, 1, upMinutes, myWriteAPIKey);
        delay(15000);
        downTime++;
        ThingSpeak.writeField(myChannelNumber, 2, downMinutes, myWriteAPIKey);
        delay(15000);
        downTime++;
        
//This statement clears the up and down time variables at midnight and 5 PM

    if(Time.hour() == 0 || Time.hour() == 17) 
    {
        upTime = 0;
        downTime = 0;
        upMinutes = 0;
        downMinutes = 0;
    }

}

    //This function resets the up and down time counters
    
    int TiFunction(String command) {
        
        String _up = String(upMinutes);
        String _down = String(downMinutes);
        
        if(command == "timer-reset") {
            upTime = 0;
            downTime = 0;
                 Particle.publish("time-reset","Up and down timers have been reset.",PRIVATE);
                 Particle.publish("up-time-minutes",_up,PRIVATE);
                 delay(1000);
                 Particle.publish("down-time-minutes",_down,PRIVATE);
            return 1;
        }
        if(command == "time") 
        {
         Particle.publish("up-time-minutes",_up,PRIVATE);
         delay(1000);
         Particle.publish("down-time-minutes",_down,PRIVATE);
         Particle.publish("Current Hour",String(Time.hour()),PRIVATE);
            return 1;
        }
        if(command == "percentage")
        {
            Particle.publish("Percentage-Up",String(runTme),PRIVATE);
            return 1;
        }
        else 
        {
            return -1;
        }
    }

#2

You could try SYSTEM_THREAD(ENABLED) which makes sure the cloud connection is serviced more regularly especially during your 15 second delays.

How often is your TiFunction() called?


#3

@ScruffR Adding SYSTEM_THREAD(ENABLED) seems to have done nothing. Still disconnecting every minute or so.

The TiFunction() is never really called. It was used on the device console before we started calling variables.

image


#4

Maybe try an “empty” project on that device at its current location to see whether it’s a hardware problem or code related.

Looking at your timing code, I’m not quite sure why you have all these delay(15000) statements in there and also that while(isRunning) isn’t too reassuring.

These devices are happiest when you stay in loop() as shortly as possible.

The way I see your intent I’d rather go for an FSM approach and for the timing values I’d use millis() or Time.local() instead of counting the number of delay(15000) statements your code has passed.


#5

Dang, just uploaded a blank project on it and it’s still disconnecting like before. Would electrical interference or electrical noise be a possible culprit? This device is within an “newer” machine than all the others.

This is the setup right now, The Photon is top middle of the picture with a “1” on it.

The antenna placement is the second picture.


#6

EMI can definetly cause connection troubles.

But looking at that contraption direct interference via the power supply may also play a role - better filtering may be required.


#7

We are actually conditioning 240V power down to 5V with a separate power supply. During initial testing we found that it supplies 5V without dropping or spiking when the machine start / stops.

3 phase power --(240V)–> Power Supply --(5V)–> Photon

Would wrapping the enclosure in Faraday fabric possibly help? Or would replacing the plastic enclosure with a metal enclosure be better suited to help with EMI.


#8

How exact did you measure the ripple of the created DC? Did you use an oscilloscope?

A grounded metal enclosure may help against EMI.


#9

What is the DIN rail-mounted device directly next to the electron enclosure? Perhaps adding a little distance between those would help. I can’t make out what it is though so I’m guessing. I second @ScruffR on adding additional filtering on the Electron Vin (or whatever power pin you’re using.)

Also, does this Electron work as expected when removed from the machine on, say, a bench power supply?


#10

@ScruffR As far as the “ripple” we just used a multi meter, I personally don’t have access to an oscilloscope.

@ninjatill That DIN rail-mounted thing next to it is the separate power supply that the enclosure is Velcro’d to. As far as is working outside of the machine, yes. I tested each “package” before installing them; power supply, photon, and wifi antenna. All of them we assembled in their final configuration and tested. Everything worked as intended with no connection problems.

My personal opinion is that a grounded metal enclosure may be the best option, along with some filtering.


#11

As far as adding filtering, I’ve seen diagrams where people add a small capacitor in parallel with what they are trying to filter. Would something along those lines be correct, adding a capacitor in parallel with the 5V coming from the power supply?


#12

Yes, parallel caps of multiple sizes (i.e. a 10uF in parallel with a 0.1uF) should be used. The size depends on the frequencies that you want to filter out but without an oscilliscope, the values of 10uF and 0.1uF are a good baseline. This is a good, quick explanation for decoupling capacitors: https://learn.sparkfun.com/tutorials/capacitors/application-examples


#13

Thanks all for your help. I will be getting everything to try out these solutions. Sadly, this is the last day of my internship at my company. I will be passing all this information on to someone who can hopefully get everything squared away for a final product. This site has really been helpful throughout my stay, and I will definitely be back in the future.