Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

//TFT added

//RTC Added
//Box IO remaining
//Buzzer Remaining
//MODBUS remaining

//◘◘◘◘◘◘◘◘◘◘ Library ◘◘◘◘◘◘◘◘


#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SD.h>
#include <SPI.h>
#include <virtuabotixRTC.h>

//◘◘◘◘◘◘◘◘◘◘ Function Declaration ◘◘◘◘◘◘◘◘

void bmpDraw(char *filename, int x, int y);


//uint32_t read16(File f);
//uint32_t read32(File f);
void get_clientdata();
void updatestage();

//◘◘◘◘◘◘◘◘◘◘ Parameter ◘◘◘◘◘◘◘◘

//▒▒▒ BOX ▒▒▒


#define comm_led D0
#define fault_led D1
#define reset D2
#define automan D3
#define buzzer D4
boolean mode=false;

//▒▒▒ LCD ▒▒▒


char str = 0;
int pu=1;
int st=100;

#define BLACK 0x0000


#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF

#define LCD_CS A3 // Chip Select goes to Analog 3


#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define SD_CS 10 // Set the chip select line to whatever you use (10 doesnt
conflict with the library)
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, A4);
//▒▒▒ RTC ▒▒▒
virtuabotixRTC myRTC (6, 7, 8);
//▒▒▒ Client Data ▒▒▒
int level=0;
String levelstr;
bool ls1; // 20% Water 1= Active; 0=Inactive
bool ls2; // 40# Water 1= Active; 0=Inactive
bool ls3; // 70% Water 1= Active; 0=Inactive
bool ls4; // 85% water 1= Active; 0=Inactive
int LSVal=0;
int errorcode=0;
int stage=0;
int prev=99;

bool motor; // Motor On/Off Command 1=ON; 0=OFF


double timestamp;
long temp=0;

//▒▒▒ Payload ▒▒▒

String payload="";
String subpayload[]={"","","","",""};

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘ SETUP ◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
void setup() {
//▒▒▒ BASIC ▒▒▒
Serial.begin(9600);

//▒▒▒ LCD ▒▒▒


tft.begin(0x9341);
SDbegin();
tft_sethome();
get_clientdata();
}

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘ LOOP ◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
void loop() {
//myRTC.updateTime ();
get_clientdata();
updatestage();
Serial.print("Stage=");Serial.println(stage);
Serial.print("prev="); Serial.println(prev);
if (stage!=prev){
tft_tank();
}
else{

}
prev=stage;
delay(2000);
}
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘ USER FUNCTIONS ◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
void SDbegin(){
//Serial.print(F("Initializing SD card..."));
if (!SD.begin(SD_CS)) {
//Serial.println(F("failed!"));
return;
}
//Serial.println(F("OK!"));
}

void tft_sethome(){
tft.fillScreen(WHITE);
tft.setRotation(0);
bmpDraw("logo2.bmp", 0, 0);
delay(1000);
tft.drawCircle(13, 13, 9, RED);
tft.setTextColor(BLACK);
tft.setCursor(30, 7);
tft.setTextSize(2);
tft.println("WIFI NA");
tft_updatetime();
}

void tft_updatetime (){


tft.setCursor(175, 7);
tft.setTextSize(2);
tft.println("10:00");
}

void tft_tank(){
//Serial.println("Level="+level);
if (level>=95 && level<=100){
bmpDraw("tank100.bmp",40,90);
}
else if (level>=85 && level<95){
bmpDraw("tank80.bmp",40,90);
}
else if (level>=40 && level<70){
bmpDraw("tank60.bmp",40,90);
}
else if (level>=20 && level<40){
bmpDraw("tank40.bmp",40,90);
}
else if (level>=15 && level<20){
bmpDraw("tank20.bmp",40,90);
}
else if (level<15){
bmpDraw("tank0.bmp",40,90);
}
}

//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘ SYSTEM FUNCTIONS ◘◘◘◘◘◘◘◘◘◘
//◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘◘
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates. It's sped up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel). Increasing the buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster. 20 pixels seems a
// good balance.

#define BUFFPIXEL 20

void bmpDraw(char *filename, int x, int y) {

File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel in buffer (R+G+B per pixel)
uint16_t lcdbuffer[BUFFPIXEL]; // pixel out buffer (16-bit per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
uint8_t lcdidx = 0;
boolean first = true;

if((x >= tft.width()) || (y >= tft.height())) return;

//Serial.println();
//Serial.print(F("Loading image '"));
//Serial.print(filename);
//Serial.println('\'');
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
//Serial.println(F("File not found"));
return;
}

// Parse BMP header


if(read16(bmpFile) == 0x4D42) { // BMP signature
//Serial.println(F("File size: ")); //Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
//Serial.print(F("Image Offset: ")); //Serial.println(bmpImageoffset, DEC);
// Read DIB header
//Serial.print(F("Header size: ")); //Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
//Serial.print(F("Bit Depth: ")); //Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

goodBmp = true; // Supported BMP format -- proceed!


//Serial.print(F("Image size: "));
//Serial.print(bmpWidth);
//Serial.print('x');
//Serial.println(bmpHeight);

// BMP rows are padded (if needed) to 4-byte boundary


rowSize = (bmpWidth * 3 + 3) & ~3;

// If bmpHeight is negative, image is in top-down order.


// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}

// Crop area to be loaded


w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;

// Set TFT address window to clipped image bounds


tft.setAddrWindow(x, y, x+w-1, y+h-1);

for (row=0; row<h; row++) { // For each scanline...


// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}

for (col=0; col<w; col++) { // For each column...


// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
// Push LCD buffer to the display first
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
lcdidx = 0;
first = false;
}
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}

// Convert pixel from BMP to TFT format


b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
lcdbuffer[lcdidx++] = tft.color565(r,g,b);
} // end pixel
} // end scanline
// Write any remaining data to LCD
if(lcdidx > 0) {
tft.pushColors(lcdbuffer, lcdidx, first);
}
//Serial.print(F("Loaded in "));
//Serial.print(millis() - startTime);
//Serial.println(" ms");
} // end goodBmp
}
}

bmpFile.close();
if(!goodBmp); //Serial.println(F("BMP format not recognized."));
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}

uint32_t read32(File f) {
uint32_t result=0;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
//◘◘◘◘◘◘◘◘◘◘ RTC ◘◘◘◘◘◘◘◘

void dayofWeek (int day)


{
switch (day)
{
case 1:
//Serial.print
("Sunday");
break; case 2:
//Serial.print
("Second");
break; case 3:
//Serial.print
("Terca");
break; case 4:
//Serial.print
("Wednesday");
break; case 5:
//Serial.print
("Quinta");
break; case 6:
//Serial.print
("Friday");
break; case 7:
//Serial.print
("Saturday"); break;
}
}

void print_datetime(){
// Print the details in serial monitor
//Serial.print("Data"); // Call the routine that prints the day of the week
dayofWeek (myRTC.dayofweek);
//Serial.print (",");
//Serial.print (myRTC.dayofmonth);
//Serial.print ("/");
//Serial.print (myRTC.month);
//Serial.print ("/");
//Serial.print (myRTC.year);
//Serial.print ("");
//Serial.print("Time"); // Adds a 0 if the time value is <10
if (myRTC.hours <10)
{//Serial.print ("0");}
//Serial.print (myRTC.hours);
//Serial.print (":"); // Adds a 0 if the value of the minutes is <10
if (myRTC.minutes <10)
{//Serial.print ("0");}
//Serial.print (myRTC.minutes);
//Serial.print (":"); // Adds a 0 if the value of the latter is <10
if (myRTC.seconds <10)
{//Serial.print ("0");}
//Serial.println (myRTC.seconds);
}

void get_clientdata(){
if (Serial.available() > 0){
payload = Serial.readString();
//Serial.println(payload);
subpayload[0]= getValue(payload,',',0);
subpayload[1]= getValue(payload,',',1);
subpayload[2]= getValue(payload,',',2);
subpayload[3]= getValue(payload,',',3);
subpayload[4]= getValue(payload,',',4);
//Serial.print("SUBPAYLOAD 0="); //Serial.println(subpayload[0]);
//Serial.print("SUBPAYLOAD 1="); //Serial.println(subpayload[1]);
//Serial.print("SUBPAYLOAD 2="); //Serial.println(subpayload[2]);
//Serial.print("SUBPAYLOAD 3="); //Serial.println(subpayload[3]);
//Serial.print("SUBPAYLOAD 4="); //Serial.println(subpayload[4]);
// timestamp = subpayload[0].toDouble;
level = subpayload[1].toInt();
LSVal = subpayload[2].toInt();
motor = subpayload[3].toInt();
errorcode = subpayload[4].toInt();
//Serial.print("timestamp="); //Serial.println(timestamp);
//Serial.print("level="); //Serial.println(level);
//Serial.print("LSVal="); //Serial.println(LSVal);
//Serial.print("motor="); //Serial.println(motor);
//Serial.print("errorcode="); //Serial.println(errorcode);

//if (levelstr!=""){
//level = levelstr.toInt();
////Serial.println();
////Serial.print("Level=");
////Serial.println(level);
//}

}
}

// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
////Serial.print("maxIndex"); //Serial.println(maxIndex);
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
////Serial.print("Found="); //Serial.println(found);
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
////Serial.print("index0="); //Serial.println(strIndex[0]);
////Serial.print("index1="); //Serial.println(strIndex[1]);
}
}
////Serial.print("return data=");
//Serial.println(data.substring(strIndex[0],strIndex[1]));
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void updatestage(){
if (level>=95 && level<=100){
stage=5;
}
else if (level>=85 && level<95){
stage=4;
}
else if (level>=40 && level<70){
stage=3;
}
else if (level>=20 && level<40){
stage=2;
}
else if (level>=15 && level<20){
stage=1;
}
else if (level<15){
stage=0;
}

You might also like