Skip to content

Instantly share code, notes, and snippets.

@wmontg5988
Created February 20, 2018 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wmontg5988/67b615c32fb8aecee808f9433dbb7351 to your computer and use it in GitHub Desktop.
Save wmontg5988/67b615c32fb8aecee808f9433dbb7351 to your computer and use it in GitHub Desktop.
For reading voltages and status for power inverter and solar panels on digital loggers PLduino
#include <CayenneESP8266Shield.h>
#include <TMRpcm_PLDuino.h>
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <PLDuino.h>
#include <PLDTouch.h>
#include <PLDuinoGUI.h>
#include <using_namespace_PLDuinoGUI.h>
#include <DS3232RTC.h>
#include <TimeLib.h>
#include <Wire.h>
#include <avr/io.h>
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A3;
Adafruit_ILI9341 tft = Adafruit_ILI9341(PLDuino::LCD_CS, PLDuino::LCD_DC);
PLDTouch touch(PLDuino::TOUCH_CS, PLDuino::TOUCH_IRQ);
Button btnVoltage("Front", ILI9341_YELLOW, ILI9341_BLACK);
Button btnVoltage1("Back", ILI9341_YELLOW, ILI9341_BLACK);
Button btnVoltage2("Battery", ILI9341_BLUE, ILI9341_BLACK);
Button btnVoltage3("Inverter", ILI9341_RED, ILI9341_BLACK);
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "opioxqafxw";
// Your network name and password.
char ssid[] = "free";
char password[] = "zzZw23521041";
// Set ESP8266 Serial object
#define EspSerial Serial2
ESP8266 wifi(EspSerial);
void setup()
{
PLDuino::init();
PLDuino::enableLCD();
PLDuino::enableESP();
tft.begin();
tft.setRotation(3);
touch. init(1);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
Serial.begin(9600);
delay(100);
// Set ESP8266 baud rate
EspSerial.begin(115200);
delay(100);
Cayenne.begin(token, wifi, ssid, password);
btnVoltage.setPositionAndSize(01, 01, 100, 75);
btnVoltage.draw(tft);
btnVoltage1.setPositionAndSize(01, 75, 100, 75);
btnVoltage1.draw(tft);
btnVoltage2.setPositionAndSize(01, 150, 100, 75);
btnVoltage2.draw(tft);
btnVoltage3.setPositionAndSize(175, 1, 150, 110);
btnVoltage3.draw(tft);
}
void loop()
{
Cayenne.run();
}
//front solar panel voltage
CAYENNE_OUT(V1)
{
int adcVal = analogRead(A0);
float adcVoltage = 55.0*adcVal/1024;
Cayenne.virtualWrite(V1,adcVoltage);
btnVoltage.setText(String(adcVoltage));
btnVoltage.draw(tft);
//delay (4000);
}
//back solar panel voltage
CAYENNE_OUT(V2)
{
int adcVal1 = analogRead(A1);
float adcVoltage1 = 55.0*adcVal1/1024;
Cayenne.virtualWrite(V2,adcVoltage1);
btnVoltage1.setText(String(adcVoltage1));
btnVoltage1.draw(tft);
//delay (4000);
}
//battery voltage
CAYENNE_OUT(V3)
{
int adcVal2 = analogRead(A2);
float adcVoltage2 = 55.0*adcVal2/1024;
Cayenne.virtualWrite(V3,adcVoltage2);
btnVoltage2.setText(String(adcVoltage2));
btnVoltage2.draw(tft);
//delay (4000);
}
//inverter voltage
CAYENNE_OUT(V4)
{
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings * 575.0 / 1024;
// send it to the computer as ASCII digits
//float average = 575.0*numReadings/1024;
Cayenne.virtualWrite(V4,average);
btnVoltage3.setText(String(average));
btnVoltage3.draw(tft);
//Serial.println(average);
delay(1); // delay in between reads for stability
}
/*int adcVal3 = analogRead(A3);
float adcVoltage3 = 575.0*adcVal3/1024;
Cayenne.virtualWrite(V4,adcVoltage3);
btnVoltage3.setText(String(adcVoltage3));
btnVoltage3.draw(tft);
//delay (4000);*/
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment