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;
}
}