Photon Machine Utilization Monitor

Hello,
Just getting going with the Particle Photons and very impressed with the devices so far. I am trying to develop a simple way to log when one of our production machines are cycling by using an output from their PLC. Since my C++ isn’t strong I’ve been having a couple of issues:

  1. How to set the time to the variables I have created

  2. Converting the time for publishing to the cloud

This is the code I have so far:

   //Machine XXX IOT device
//This program will monitor for machine status then post the runtime to the cloud

// First, we're going to make some variables.
// This is our "shorthand" that we'll use throughout the program:

int machineinput = D7;
//D7 has a blue led on the board to help identify status

unsigned long machinestart;
//time_t machinestart;
//Variable to log cycle start time

unsigned long machineend;
//time_t machineend;
//variable to log cycle end time

unsigned long elapsedtime;
//elapsed runtime

int machinestatus = -1;

String machinenumber = "295";

// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.

void setup() {

pinMode(machineinput, INPUT);

}

void loop() {

machinestatus = machineinput;

if (machinestatus > 0 && machinestart == 0)  {
//If machine is running and start time is blank set machine start time

//machinestart = ; //Set the machine start time here

}
else if (machinestatus == 0 && machinestart > 0) {
//If machine has logged that it has started and the machine is no longer running set the end time and elapsed time and publish.


//machineed = ; //set the machine cycle end time
elapsedtime = (machinestart-machineend); //calculate the elapsed time


//publish data
Particle.publish("Data", elapsedtime, PRIVATE); // publish to cloud


//reset variables
machinestatus = -1;
machinestart = 0;
machineend = 0;

}
}

Any guidance is greatly appreciated.

The available time functions are listed in the Particle documentation. https://docs.particle.io/reference/firmware/photon/

You can get both Unix time in seconds and HH MM SS returns easily. Time synchronization functions are also available as part of the cloud functions. I’d really recommend spending time reading through the function documentation as the time functions are quite well done. The only “hard” work one might need to do is converting Unix time to a standard date/time format using C library functions if you’re doing time deltas in seconds but want to report back in human readable format.

If you’re just wanting to know the uptime and not necessarily using that information in your code, upgrading your device to system firmware version 0.8.0 + gives you this and more in the console.

Just a note: millis() reports the amount of milliseconds the device has been up.
For run times less than 2^32ms this would be the simplest timer.

Time.format() does help with that. You can pass a Unix timestamp in as first parameter and get back a string of your desired format - just be aware that the Time.zone() setting will be applied, so if you have set it to anything other than 0.0 you need to substract that value from your timestame (odd, but there is a pending issue about this).

Interesting subject. I have some thoughts. You haven’t stated the type of machine or typical running up-times. But, you might want to consider:

You are only reporting up-time when the machine turns off. What if it is a long-running machine (hours, days, weeks, months, etc.)? You may want to report your up-time on, say, an hourly basis. So every hour you would report the number of minutes or seconds that the machine was running for. The Photon could also keep track of a cumulative up-time since the last reset or since being placed into service in addition to those hourly increments. The advantage is that you can add up all those incremental reports at any time (instead of when it turns off) and you have some sort of “I’m still running” status notification.

What about the case where the machine and Photon lose power? You will lose all up-time statistics if you don’t periodically store the up-time into a retained variable or EEPROM. What about when the machine comes back up after a power outage? Should the Photon immediately report the interruption and up-time recorded before crashing?

1 Like

Thanks! I saw that had been added, but haven’t used it yet. There has been a considerable amount of development in the time convenience functions on the Particle platform. I don’t think there’s anything left that would push someone into using the c mktime library unless they wanted to now.

I think the OP is looking for equipment uptime rather than the photon.

1 Like

Great questions! I am actually trying to monitor machine utilization rather than up time. I changed the subject for clarity. We can have a machine on but not producing parts or creating value. Our typical cycle will be about 1 to 2 minutes on the machines that we will use for testing. As this advances we may want to plan for power outages and store the data but we are just trying to get a proof of concept first.