Last active
March 18, 2018 01:36
-
-
Save wmontg5988/cc1bb7d69ba92b57c5d690a177b5aac8 to your computer and use it in GitHub Desktop.
PLDuino MQTT with Smooth voltage readings test 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CayenneMQTTESP8266Shield.h> | |
#include <SPI.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> | |
//MQTT Authentication | |
char username[] = ""; | |
char password[] = ""; | |
char clientID[] = ""; | |
#define VIRTUAL_CHANNEL 6 | |
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial. | |
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 WiFipassword[] = ""; | |
// 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); | |
pinMode(ACTUATOR_PIN, OUTPUT); | |
// Set ESP8266 baud rate | |
EspSerial.begin(115200); | |
delay(100); | |
Cayenne.begin(username, password, clientID, wifi, ssid, WiFipassword); | |
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.loop(); | |
} | |
// This function is called when data is sent from Cayenne. | |
CAYENNE_IN(VIRTUAL_CHANNEL) | |
{ | |
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open. | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN, LOW); | |
} | |
} | |
CAYENNE_IN_DEFAULT() | |
{ | |
CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString()); | |
} | |
//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