#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
// defines for display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // on nodeMCU: SCL = D1, SDA = D2
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//bitmaps
// 'Bomba', 9x12px
const unsigned char bitmap_Bomba [] PROGMEM = {
0x00, 0x00, 0x1c, 0x00, 0x22, 0x00, 0x5d, 0x00, 0x55, 0x00, 0x5d, 0x00, 0x22, 0x00, 0x1c, 0x00,
0x10, 0x00, 0x10, 0x00, 0x38, 0x00, 0x00, 0x00
};
// 'Tinaco1', 63x64px
const unsigned char bitmap_Tinaco [] PROGMEM = {
0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0,
0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c,
0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e,
0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e,
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe
};
//general variables
const int pushButton = 19;
int buttonStatus = 0, displayPage = 3, autoMan = 0, startSetPoint = 30, releStatus = 1;
int pumpStatus = 0, touchStatus = 0, inManualScreen = 0, inAutoScreen = 0;
int xkcLhStatus = 1, xkcLlStatus = 1;//sensores de abajo 0 es agua NO detectada
int xkcHhStatus = 0, xkcHlStatus = 0;//sensores de arriba 0 es agua detectada
long waitTime = 0, waitTime2 = 0, waitTime3 = 0, waitTime6 = 0, waitTime7 = 0, waitFor2 = 2000;
float levelPerc = 100;
String completeDate = "XX:XX", ntpString = "No Ok", stringMQTT = "No Ok";
int levelSensorStatus = 0, pumpProblem = 0, pumpTimeProblem = 0, negativePercProblem = 0;
float distanceCM = 133, distToBottom = 133;
void Screen0() {
display.clearDisplay();
//Tinaco Azotea
display.drawBitmap(0, 0, bitmap_Tinaco, 63, 64, 1);
display.fillRect(3, 60 - map(levelPerc, 0, 100, 0, 49), 59, map(levelPerc, 0, 100, 0, 49), SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_INVERSE);
display.setCursor(40, 11);
if (xkcHhStatus == 0) {
display.write("On");
}
else {
display.write("Off");
}
display.setCursor(40, 53);
if (xkcHlStatus == 0) {
display.write("On");
}
else {
display.write("Off");
}
display.setTextSize(3);
if (levelPerc < 100){
display.setCursor(13, 24);
}
else {
display.setCursor(3, 24);
}
display.print(levelPerc, 0);
//Tinaco Patio
display.drawBitmap(64, 0, bitmap_Tinaco, 63, 64, 1);
display.setTextSize(1);
if (xkcLhStatus == 1 && xkcLlStatus == 1) {
display.fillRect(67, 11, 59, 49, SSD1306_WHITE);
}
else if (xkcLhStatus == 0 && xkcLlStatus == 1){
display.fillRect(67, 36, 59, 24, SSD1306_WHITE);
}
display.setCursor(104, 11);
if (xkcLhStatus == 1) {
display.write("On");
}
else {
display.write("Off");
}
display.setCursor(104, 53);
if (xkcLlStatus == 1) {
display.write("On");
}
else {
display.write("Off");
}
display.setCursor(70, 32);
if (autoMan == 1) {
display.write("Auto ");
display.print(startSetPoint);
display.write("%");
}
else {
display.write("Man");
}
display.setCursor(80, 2);
if (releStatus == 1) {
display.write("R");
}
display.setCursor(100, 2);
if (pumpProblem == 1) {
display.write("!");
}
if (pumpStatus == 1) {
display.drawBitmap(91, 1, bitmap_Bomba, 9, 12, SSD1306_INVERSE);
}
display.display();
}
void Screen1() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
//estado
display.setCursor(0, 0); //start at top-left corner (x, y)
display.print(completeDate);
display.setCursor(0, 8);
display.write("NTP: ");
display.print(ntpString);
display.setCursor(0, 16);
display.write("MQTT: ");
display.print(stringMQTT);
display.setCursor(0, 24);
display.write("Wifi: Ok");
// if (WiFi.status() != WL_CONNECTED) {
// display.write("No Ok ");
// }
// else {
// display.write("Ok ");
// String myIP = WiFi.localIP().toString();
// display.print(myIP.substring(8));
// }
display.setCursor(0, 32);
display.write("PSis: ");
if (pumpProblem == 1) {
display.fillRect(33, 32, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 32, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 40);
display.write("PSen: ");
if (levelSensorStatus == 1) {
display.fillRect(33, 40, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 40, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 48);
display.write("PTim: ");
if (pumpTimeProblem == 1) {
display.fillRect(33, 48, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 48, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 56);
display.write("PNeg: ");
if (negativePercProblem == 1) {
display.fillRect(33, 56, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 56, 8, 8, SSD1306_WHITE);
}
display.setCursor(47, 32);
display.write("Dist: ");
display.print(distanceCM, 1);
display.write("cm");
display.setCursor(47, 40);
display.write("100%: ");
display.print(distToBottom, 1);
display.write("cm");
display.display();
}
void Screen2() {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.setCursor(0, 0);
display.write("PSis: ");
if (pumpProblem == 1) {
display.fillRect(33, 0, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 0, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 8);
display.write("PSon: ");
if (levelSensorStatus == 1) {
display.fillRect(33, 8, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 8, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 16);
display.write("PTmp: ");
if (pumpTimeProblem == 1) {
display.fillRect(33, 16, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 16, 8, 8, SSD1306_WHITE);
}
display.setCursor(0, 24);
display.write("PNeg: ");
if (negativePercProblem == 1) {
display.fillRect(33, 24, 8, 8, SSD1306_WHITE);
}
else {
display.drawRect(33, 24, 8, 8, SSD1306_WHITE);
}
display.display();
}
void Screen3() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.write("Screen 3");
display.display();
}
void BlackScreen() {
display.clearDisplay();
display.display();
}
void ScreenSelect() {
buttonStatus = digitalRead(pushButton);
if (buttonStatus == HIGH) {// && pumpStatus == 0
switch (displayPage) {
case 0:
displayPage = 1;
Screen1();
delay(500);
break;
case 1:
displayPage = 2;
Screen2();
delay(500);
break;
case 2:
displayPage = 3;
Screen3();
delay(500);
break;
case 3:
displayPage = 0;
Screen0();
delay(500);
break;
}
waitTime7 = millis();
}
if (millis() - waitTime7 > 10000 && pumpStatus == 0) {
BlackScreen();
displayPage = 3;
}
else if (inAutoScreen == 0 && pumpStatus == 1 && millis() - waitTime6 > waitFor2) {
displayPage = 0;
Screen0();
inAutoScreen = 1;
waitTime6 = millis();
}
else if (inAutoScreen == 1 && pumpStatus == 1 && millis() - waitTime6 > waitFor2) {
switch (displayPage) {
case 0:
displayPage = 0;
Screen0();
break;
case 1:
displayPage = 1;
Screen1();
break;
case 2:
displayPage = 2;
Screen2();
break;
case 3:
displayPage = 3;
Screen3();
break;
}
}
else if (inAutoScreen == 1 && pumpStatus == 0) {
BlackScreen();
inAutoScreen = 0;
}
}
void setup() {
// Serial.begin(115200);
pinMode(pushButton, INPUT);
// for display
Wire.begin(); /* Begin Wire GPI21 = SDA , GPI22 = SCL*/
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
//Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display(); // Show initial display buffer contents on the screen --
display.clearDisplay(); // Clear the buffer
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.cp437(true); // Use full 256 char 'Code Page 437' font
delay(500);
BlackScreen();
}
void loop() {
ScreenSelect();
}