Arduino info - internal volts/uptime/online

In the process of troubleshooting, I made some basic status trackers for the Arduino/Cayenne and thought I would share. It just provides some basic info but it helped me with my project I have randomly going offline now & then. I confirmed i was getting voltage drops into the 4.7V range.

The min and max are read using a special code that measures the internal voltage that Arduino references against your analog inputs. You an also use this to get your true top rail voltage rather than assuming its 5.0.

System up time is just using millis(), nothing special.

Online time is measured from the last connect to the Cayenne server in minuets.

Disconnects logs the number of times the program reconnected to the Cayenne servers since reset.

Code all together:

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(10, 11);
//#define CAYENNE_DEBUG
//#define CAYENNE_PRINT mySerial
#include <CayenneESP8266Shield.h>
#include <SimpleTimer.h>

SimpleTimer timer;

char token[] = "";
char ssid[] = "";
char password[] = "";

#define EspSerial Serial
ESP8266 wifi(EspSerial);

long result;

unsigned long time;
unsigned long onlineTime;
unsigned long prevTime;

int maxValue = 0;
int minValue = 6000;
int resetMinMax;
int disco = 0;



void setup()
{
	EspSerial.begin(9600);
	delay(10);

	// mySerial.begin(9600);
	// mySerial.println("softSerial running");

	pinMode(2, OUTPUT);
	pinMode(3, OUTPUT);
	pinMode(4, OUTPUT);
	pinMode(5, OUTPUT);
	pinMode(6, OUTPUT);
	pinMode(7, OUTPUT);


	Cayenne.begin(token, wifi, ssid, password);

	timer.setInterval(30000, runMoist);
}



void loop()
{
	Cayenne.run();
	timer.run();

	if (resetMinMax == true)
		{
			maxValue = 0;
			minValue = 6000;
		}

	getRefV();

	time = millis();
}



void runMoist()
{
	int x = 2;  //pwr pin
	int y = 0;  //data
	int z = 1;  //channel

	for (int i = 0; i <= 6; i++)
		{
			checkMoist(x, y, z);
			x++;
			y++;
			z++;

			if (y == 4)
				{
					y = 6;
				}
		}
}


void checkMoist(int pwrPin, int dataPin, int channel)
{
	digitalWrite(pwrPin, HIGH);
	delay(100);
	float plant = (1024 - analogRead(dataPin));
	int plantPer = (plant / 1024) * 100;
	digitalWrite(pwrPin, LOW);
	Cayenne.virtualWrite(channel, plantPer);
//	Cayenne.run();
}



void getRefV()
{
	#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
	ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #else
	ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  #endif

	delay(20);
	ADCSRA |= _BV(ADSC);
	while (bit_is_set(ADCSRA,ADSC));

	uint8_t low  = ADCL;
	uint8_t high = ADCH;

	result = (high << 8) | low;
	result = 1125300L / result;


	if (result > maxValue)
		{
			maxValue = result;
		}

	if (result < minValue)
		{
			minValue = result;
		}
}



CAYENNE_CONNECTED()
{
	prevTime = millis();
}

CAYENNE_DISCONNECTED()
{
	prevTime = millis();
	disco++;
}



CAYENNE_OUT(V10)
{
	Cayenne.virtualWrite(V10, maxValue);
	Cayenne.virtualWrite(V11, minValue);
}

CAYENNE_IN(V13)
{
	resetMinMax = getValue.asInt();
}

CAYENNE_OUT(V23)
{
	Cayenne.virtualWrite(V23, disco);
}

CAYENNE_OUT(V24)
{
	float time1 = (time / 1000) / 60;
	Cayenne.virtualWrite(V24, time1);
}

CAYENNE_OUT(V25)
{
	onlineTime = ((time - prevTime) / 1000) / 60;
	Cayenne.virtualWrite(V25, onlineTime);
}
5 Likes

This is excellent, thanks for sharing. Can I ask if you were able to do something to correct the voltage drops, and if that made a real impact in the device staying online for larger periods of time?

I originally added a capacitor which helped it but ultimately the power scheme needed to be redesigned. It was one of my earlier hardware projects and I just have the power part all messed up… lots of partial series rather than running things in parallel.

My aquarium “Tanker” project was having lots of disconnects originally thru the testing. I finally changed out the AC/DC power supply from a 900mA up to a 2A and that solved all my issues with disconnects.

3 Likes