Last active
June 8, 2019 03:14
-
-
Save wmontg5988/9968564b1a39ffa4aea471dbf7eac931 to your computer and use it in GitHub Desktop.
PLduino MQTT A1
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 <Button.h> | |
#include <Drawable.h> | |
#include <GridLayout.h> | |
#include <Label.h> | |
#include <Layout.h> | |
#include <LinearLayout.h> | |
#include <Placeholder.h> | |
#include <PLDuinoGUI.h> | |
#include <Settings.h> | |
#include <Sizable.h> | |
#include <using_namespace_PLDuinoGUI.h> | |
#include <Utils.h> | |
#include <Widget.h> | |
#include <PLDTouch.h> | |
#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_CHANNEL2 2 | |
#define ACTUATOR_PIN2 2// Do not use digital pins 0 or 1 since those conflict with the use of Serial. | |
#define VIRTUAL_CHANNEL3 3 | |
#define ACTUATOR_PIN3 3 | |
#define VIRTUAL_CHANNEL4 4 | |
#define ACTUATOR_PIN4 4 | |
#define VIRTUAL_CHANNEL5 5 | |
#define ACTUATOR_PIN5 5 | |
#define VIRTUAL_CHANNEL6 6 | |
#define ACTUATOR_PIN6 6 | |
#define VIRTUAL_CHANNEL7 7 | |
#define ACTUATOR_PIN7 7 | |
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[] = ""; | |
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_PIN2, OUTPUT); | |
pinMode(ACTUATOR_PIN3, OUTPUT); | |
pinMode(ACTUATOR_PIN4, OUTPUT); | |
pinMode(ACTUATOR_PIN5, OUTPUT); | |
pinMode(ACTUATOR_PIN6, OUTPUT); | |
pinMode(ACTUATOR_PIN7, 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_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 (10000); | |
} | |
//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 (10000); | |
} | |
//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 (10000); | |
} | |
//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(10000); // delay in between reads for stability | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL2) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN2, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN2, LOW); | |
} | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL3) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN3, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN3, LOW); | |
} | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL4) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN4, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN4, LOW); | |
} | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL5) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN5, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN5, LOW); | |
} | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL6) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN6, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN6, LOW); | |
} | |
} | |
CAYENNE_IN(VIRTUAL_CHANNEL7) | |
{ | |
if (getValue.asInt() == 0) { | |
digitalWrite(ACTUATOR_PIN7, HIGH); | |
} | |
else { | |
digitalWrite(ACTUATOR_PIN7, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment