V.062 11/3

Dependencies:   FT6206 SDFileSystem ILI9341_t3

Fork of ATT_AWS_IoT_demo_v06 by attiot

Revision:
30:8c979815f533
Parent:
29:f71a0be59b99
Child:
31:255a2c6f8f47
--- a/main.cpp	Fri Nov 03 20:28:02 2017 +0000
+++ b/main.cpp	Wed Nov 15 19:28:37 2017 +0000
@@ -3,14 +3,6 @@
  */
 #include "mbed.h"
 
-//TFT
-#include "SPI_TFT_ILI9341.h"
-#include "FT6206.h"
-#include "Arial12x12.h"
-#include "Arial28x28.h"
-#include "BookAntiqua19x19-14.h"
-
-#include "logo.h"
 
 // SD File System
 #include "SDFileSystem.h"
@@ -42,6 +34,27 @@
 #include "mbedtls/net_sockets.h"
 #include "pem.h"
 
+#include "TextField.h"
+
+//TFT + Touch Panel
+#include "ILI9341_t3.h"
+#include "font_Arial.h"
+#include "font_ArialBold.h"
+#include "font_LiberationSans.h"
+#include "font_LiberationSansBold.h"
+#include "font_LiberationSansNarrow.h" 
+#include "font_LiberationSansNarrowBold.h"
+
+#define MAIN_FONT           Arial_14
+//#define MAIN_FONT           LiberationSansNarrow_14_Bold
+//#define MAIN_FONT           LiberationSans_14_Bold
+#define BUTTON_FONT         LiberationSansNarrow_24_Bold
+#define DEFAULT_TEXT_COLOR  ILI9341_CYAN
+
+#define ILI9341_ATT     0x04fb
+
+#include "logo.h"
+#include "FT6206.h"
 
 // Sensors
 #include "HTS221.h"
@@ -51,7 +64,6 @@
 #endif
 
 
-
 //=====================================================================================================================
 //
 // Defines
@@ -97,13 +109,13 @@
 // Sensor defines
 #define CTOF(x)  ((x)*1.8+32) // Temperature
 
-//TFT
+//TFT SPI
 #define PIN_SCLK        PTD5
 #define PIN_MISO        PTD7
 #define PIN_MOSI        PTD6
 #define PIN_CS_TFT      PTD4  // chip select pin
 #define PIN_DC_TFT      PTB20  // data/command select pin.
-#define PIN_RESET_TFT   PTB20  //we don't need reset so just use DC instead. Could modify library
+#define PIN_RESET_TFT   PTC18  //we don't need reset so just use DC instead. Could modify library
 
 #define PORTRAIT        0
 #define LANDSCAPE       1
@@ -125,8 +137,7 @@
 //#endif
 
 
-
-SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "TFT"); // mosi, miso, sclk, cs, reset, dc 
+ILI9341_t3 tft(PIN_CS_TFT, PIN_DC_TFT, PIN_RESET_TFT, PIN_MOSI, PIN_SCLK, PIN_MISO);
 FT6206 ft6206(PIN_SDA_FT6206, PIN_SCL_FT6206, PIN_INT_FT6206); // sda, scl, int
 
 //=====================================================================================================================
@@ -155,9 +166,7 @@
 // Sensor data
 float temperature = 0.0;
 int   humidity    = 0;
-unsigned int count = 1;
-
-char iccid[] = "89011702278124165220";
+unsigned int count = 0;
 
 //mqqt host name
 char *aws_iot_mqtt_host;
@@ -169,6 +178,10 @@
 HTS221 hts221; 
 
 
+/* Mapping between COLOR_XXX defnes and the color/text for display */
+int16_t colourMap[] = { ILI9341_BLACK, ILI9341_RED, ILI9341_GREEN, 0, ILI9341_BLUE, 0, 0, ILI9341_WHITE };
+char*   colourNameMap[] = {"OFF", "RED", "GREEN", "", "BLUE", "INVALID", "INVALID", "WHITE" };
+
 
 //=====================================================================================================================
 //
@@ -248,131 +261,123 @@
 void printData()
 {
     INFO("Temperature is: %0.2f F", temperature);
-    INFO("Humidity    is: %02d", humidity);    
-    
-    switch (ledColor) {
-         case COLOR_OFF:
-             INFO("LED: Off");
-             
-             break;
-         case COLOR_RED:
-             INFO("LED: Red");
-             
-             break;
-         case COLOR_GREEN:
-             INFO("LED: Green");
-             break;
-         case COLOR_BLUE:
-             INFO("LED: Blue");
-             break;
-         case COLOR_WHITE:
-             INFO("LED: White");
-             break;
-    }
-    
+    INFO("Humidity    is: %02d", humidity);
+    INFO("LED: %s", colourNameMap[ledColor]);
 }
 
-/*
-void printDatatoTFT()
+    
+#if 1
+void printDatatoTFT(bool bFirstTime)
 {
-    //TFT.cls();
-    TFT.locate(0,10);
-    TFT.printf("ATT LTE: %d dBm      AWS: Connected\n\n", dbm);
-    //TFT.line (0,21, 320,21, White);
-    TFT.printf("Certificate Name: %s\n\n", cSubject);
-    //TFT.printf("Sending Device Data...\n\n");
-    TFT.printf("ICCID:      %s\n", iccidName);
-    TFT.printf("Temperature: %0.2f F    \n", temperature);
-    TFT.printf("Humidity:    %02d %%\n", humidity);
-    TFT.printf("LED:       ");
+    static TextField texts[7];
     
-    switch (ledColor) {
-         case COLOR_OFF:
-             TFT.foreground(White);
-             TFT.printf("OFF     \n");
-             TFT.fillcircle (240,160, 25, Black);
-             TFT.circle (240,160, 25, White);
-             break;
-         case COLOR_RED:
-             TFT.foreground(Red);
-             TFT.printf("RED     \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Red);
-             break;
-         case COLOR_GREEN:
-             TFT.foreground(Green);
-             TFT.printf("GREEN   \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Green);
-             break;
-         case COLOR_BLUE:
-             TFT.foreground(Blue);
-             TFT.printf("BLUE   \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Blue);
-             break;
-         case COLOR_WHITE:
-             TFT.foreground(White);
-             TFT.printf("WHITE   \n");
-             TFT.fillcircle (240,160, 25, White);
-             break;
+    if (bFirstTime) {        
+        tft.fillScreen(ILI9341_BLACK);
+
+        tft.setCursor(0, 2);
+        tft.printf("ATT LTE: ");
+        texts[0].setFieldInt(&tft, "%d dbM", -99);    
+
+        tft.setCursor(200, 2);
+        tft.printf("AWS: ");
+        texts[1].setFieldInt(&tft, "%d", 99);
+        tft.printf("\n\n");
+
+        tft.setTextWrap(false);     //no line wrap for CN or ICCID
+        
+        tft.printf("CN: ");
+        texts[2].setFieldLine(&tft, "%s");
+        tft.printf("\n");
+        
+        tft.moveCursor(0, 3);
+        tft.printf("ICCID: ");
+        texts[3].setFieldLine(&tft, "%s");
+        tft.printf("\n");
+        
+        tft.setTextWrap(true);    
+
+        tft.moveCursor(0, 3);
+        tft.printf("TEMPERATURE: ");
+        texts[4].setFieldFloat(&tft, "%0.1fF", 100.0f);
+        tft.printf("\n");
+
+        tft.moveCursor(0, 3);
+        tft.printf("HUMIDITY: ");
+        texts[5].setFieldInt(&tft, "%02d%%", humidity);
+        tft.printf("\n");
+
+        tft.moveCursor(0, 3);
+        tft.printf("LED: ");
+        texts[6].setFieldStr(&tft, "%s", "GREEN");
+        tft.printf("\n");
     }
-    TFT.printf("\nUpdate Count: %d\n", count++);
+    
+    /* Now update. We leave the more static ones (eg ICCID) to the end */
+    texts[0].drawFieldInt(dbm);    
+    texts[1].drawFieldInt(++count);    
+    texts[4].drawFieldFloat(temperature);
+    texts[5].drawFieldInt(humidity);
+    
+    /* handle special case for "OFF" */
+    int8_t textColor = (ledColor == COLOR_OFF) ? DEFAULT_TEXT_COLOR : colourMap[ledColor];
+    
+    tft.setTextColor(textColor);
+    texts[6].drawFieldStr(colourNameMap[ledColor], true);
+
+    tft.fillCircle (240,160, 27, ILI9341_DARKGREY);
+    tft.fillCircle (240,160, 25, colourMap[ledColor]);
+    tft.setTextColor(DEFAULT_TEXT_COLOR);
+
+    tft.setTextWrap(false);
+    texts[2].drawFieldStr((const char*)cSubject, false);    
+    texts[3].drawFieldStr(iccidName, false);  
+    tft.setTextWrap(true);
+
 }
-*/
 
-void printDatatoTFT()
+#else
+void printDatatoTFT(bool bFirstTime)
 {
-    //TFT.cls();
-    TFT.locate(0,10);
-    TFT.printf("ATT LTE: %d dBm AWS: %d\n\n", dbm, count++);
-    //TFT.line (0,21, 320,21, White);
-    TFT.printf("CN: %s\n", cSubject);
-    //TFT.printf("Sending Device Data...\n\n");
-    TFT.printf("ICCID: %s\n", iccidName);
-    TFT.printf("TEMPERATURE: %0.2f F    \n", temperature);
-    TFT.printf("HUMIDITY: %02d %%\n", humidity);
-    TFT.printf("LED: ");
+    if (bFirstTime) {        
+        tft.fillScreen(ILI9341_BLACK);
+        tft.setCursor(0, 2);
+        tft.printf("ATT LTE:    dBm");
+    }
+    tft.fillRect(70, 2, 70+40, 2+14, ILI9341_BLACK);
+    tft.setCursor(70, 2);
+    tft.printf("%d", dbm);
+     
+    tft.setCursor(200, 2);
+    tft.printf("AWS: %d\n\n", ++count);
+    tft.printf("CN: %s\n", cSubject);
+    tft.moveCursor(0, 3);
+    tft.printf("ICCID: %s\n", iccidName);
+    tft.moveCursor(0, 3);
+    tft.printf("TEMPERATURE: %0.2f °F\n", temperature);
+    tft.moveCursor(0, 3);
+    tft.printf("HUMIDITY: %02d %%\n", humidity);
+    tft.moveCursor(0, 3);
+    tft.printf("LED: ");
+ 
+    /* handle special case for OFF */
+    int8_t textColor = (ledColor == COLOR_OFF) ? COLOR_WHITE : ledColor;
     
-    switch (ledColor) {
-         case COLOR_OFF:
-             TFT.foreground(White);
-             TFT.printf("OFF     \n");
-             TFT.fillcircle (240,160, 25, Black);
-             TFT.circle (240,160, 25, White);
-             break;
-         case COLOR_RED:
-             TFT.foreground(Red);
-             TFT.printf("RED     \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Red);
-             break;
-         case COLOR_GREEN:
-             TFT.foreground(Green);
-             TFT.printf("GREEN   \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Green);
-             break;
-         case COLOR_BLUE:
-             TFT.foreground(Blue);
-             TFT.printf("BLUE   \n");
-             TFT.foreground(White);
-             TFT.fillcircle (240,160, 25, Blue);
-             break;
-         case COLOR_WHITE:
-             TFT.foreground(White);
-             TFT.printf("WHITE   \n");
-             TFT.fillcircle (240,160, 25, White);
-             break;
-    }
-    //TFT.printf("\nUpdate Count: %d\n", count++);
+    tft.setTextColor(colourMap[textColor]);
+    tft.printf("%s     \n", colourNameMap[ledColor]);
+    tft.fillCircle (240,160, 27, ILI9341_DARKGREY);
+    tft.fillCircle (240,160, 25, colourMap[ledColor]);
+    //tft.drawCircle (240,160, 25, colourMap[COLOR_WHITE]);
+    //tft.drawCircle (240,160, 26, colourMap[COLOR_WHITE]);
+    tft.setTextColor(DEFAULT_TEXT_COLOR);
 }
+#endif
+
 
 void ShowINFO(const char *sInfo)
 {
     INFO(sInfo);
-    TFT.printf(sInfo);
-    TFT.printf("\n");
+    tft.printf(sInfo);
+    tft.printf("\n");
 }
    
 
@@ -388,7 +393,7 @@
         const char *pReceivedJsonDocument, void *pContextData) {
 
     INFO("Shadow Update Status Callback");
-    TFT.printf("\n");
+    tft.printf("\n");
     if (status == SHADOW_ACK_TIMEOUT) {
         INFO("Update Timeout");
     } else if (status == SHADOW_ACK_REJECTED) {
@@ -473,7 +478,6 @@
     INFO("Running Out-of-Box Function (alternate demo).");
     
     IoT_Error_t rc = NONE_ERROR;
-    int32_t i = 0;
     int publishCount = 0;
     bool infinitePublishFlag = true;
     char cPayload[100];
@@ -598,169 +602,43 @@
 void InitTFT()
 {
     //Configure the display driver
-    TFT.claim(stdout);
-    TFT.background(Black);
-    TFT.foreground(White);
-    TFT.set_orientation(LANDSCAPE_R);
-    TFT.cls();
-    TFT.set_font((unsigned char*) Arial12x12);
-    //TFT.set_font((unsigned char*) Courier10x13-12B);
-    TFT.locate(0,0);
+    tft.begin();
+    tft.setTextColor(DEFAULT_TEXT_COLOR, ILI9341_BLACK);
+    tft.setRotation(3);   //EBJ TODO name LANDSCAPE_R);
+    tft.fillScreen(ILI9341_BLACK);    
+
+    tft.setFont(MAIN_FONT);
+    tft.setCursor(0, 1);
 }
 
-void TFTStuff()
-{
-    INFO("DBG> main\r\n");
-    
-    //FT6206 ft6206(PIN_SDA_FT6206, PIN_SCL_FT6206, PIN_INT_FT6206); // sda, scl, int
-
-    //Configure the display driver
-    TFT.claim(stdout);
-    TFT.background(Black);
-    TFT.foreground(White);
-    
-    //TFT.background(White);
-    //TFT.foreground(Black);
-
-    
-    TFT.set_orientation(LANDSCAPE_R);
-    TFT.cls();
-
-    //Print a welcome message
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(0,0);
-    TFT.printf("Hello mbed!\n");
-    TFT.printf("Touch Screen to Continue!\n");
-    INFO("Hello mbed!\r\n");
-    INFO("Touch Screen to Continue!!\r\n");
-    
-    int X1, Y1, X2, Y2;
-    TS_Point p;
-    
-    while(1) {
-        
-        if(ft6206.touched())
-        {
-            INFO("Touched\r\n");
-            p = ft6206.getPoint();
-            
-            
-            X1 = TFT.width()-p.x;
-            Y1 = TFT.height()-p.y;
-            pc.printf("Touched at x=%3d y=%3d\n", p.x, p.y);
-            pc.printf("Touched actual at x=%3d y=%3d\n", X1, Y1);
-            
-            //ft6206.clearPoint();
-            //return;
-        }
-/*
-        if (ft6206.getTouchPoint(p)) {
-            X1 = X2;
-            Y1 = Y2;
-            X2 = TFT.width()-p.x;
-            Y2 = TFT.height()-p.y;
-            TFT.locate(0,12);
-            INFO("Touch %3d %3d\n", p.x, p.y);
-            return;
-            if ((X1 > 0) && (Y1 > 0) && (X2 > 0) && (Y2 > 0)) {
-                TFT.line(X1, Y1, X2, Y2, RGB(255,128,255));
-            }
-        }
-*/
-    }
-    
-}
-/*
-//=====================================================================================================================
-// setupShield
-// setup the Adrafruit 1947
-//=====================================================================================================================
-void setupShield(bool touch) {
-    //Configure the display driver
-    TFT.claim(stdout);
-    TFT.background(Black);
-    TFT.foreground(White);
-    TFT.set_orientation(LANDSCAPE);
-        
-    TFT.cls();
-    INFO("TFT.cls()");
-    
-    TFT.circle(120, 120, 50, Red);
-
-    //Print a welcome message
-    TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(120,160);
-    TFT.printf("Hello G+D!\n");
-
-   if (touch) {
-       while (0)
-        if(pc.readable())
-                pc.putc(pc.getc());    
-
-       int X1, Y1, X2, Y2 = 0;
-        X2 = -100;
-        while(1) {
-            if (FT6206.touched()) {
-    //        if (FT6206.dataReceived()) {
-    //            led1 = !led1;
-                // Retrieve a point  
-                TS_Point p = FT6206.getPoint();
-                X1 = X2;
-                Y1 = Y2;
-                X2 = p.x;
-                Y2 = p.y;
-    //            printf("Touch %3d %3d\n", p.x, p.y);
-                if ((X1 > 0) && (Y1 > 0) && (X2 > 0) && (Y2 > 0)) {
-                    TFT.line(X1, Y1, X2, Y2, Green);
-                }
-            }
-    //        TFT.printf("Count: %d\n", count++);
-            wait(0.05);
-        }
-    }
-}
-*/
 
 int DoAWSThingMenu()
-{
-    
-    //FT6206 ft6206(PIN_SDA_FT6206, PIN_SCL_FT6206, PIN_INT_FT6206); // sda, scl, int
-    
-    TFT.cls();
-    TFT.locate(0,3);
-    INFO ("AWS Host Selection\n");
-    TFT.set_font((unsigned char*) Book_Antiqua19x19);
-    TFT.printf ("     SELECT AWS HOST\n");
+{   
+    tft.fillScreen(ILI9341_BLACK);    
+    INFO("AWS Host Selection\n");
+    tft.setCursor(65, 1);
+    tft.printf("SELECT AWS HOST\n");
     
-    TFT.set_font((unsigned char*) Arial28x28);
-    //TFT.circle (160,65, 40, Blue);
-    TFT.fillcircle (160,75, 40, Blue);
-    TFT.locate(130,63);
-    //TFT.foreground(Blue);
-    TFT.background(Blue);
-    TFT.foreground(Red);
-    TFT.printf("ATT");
-    //TFT.line (0,39, 319, 39, White);
-    //TFT.circle (160,150, 40, Green);
-    TFT.fillcircle (160,160, 40, Green);
-    TFT.locate(137, 148);
-    //TFT.foreground(Green);
-    TFT.background(Green);
-    TFT.foreground(Black);
-    TFT.printf("GD");
-    //TFT.line (0,79, 319, 79, White);   
+    tft.setFont(BUTTON_FONT);
+    tft.fillCircle (160,75, 40, ILI9341_ATT);
+    tft.setCursor(132, 63);
+    tft.setTextColor(ILI9341_WHITE);
+    tft.printf("ATT");
+    tft.fillCircle (160,160, 40, ILI9341_OLIVE);
+    tft.setCursor(140, 148);
+    tft.printf("GD");
+    tft.setFont(MAIN_FONT);
     
-    int X1, Y1, X2, Y2;
+    int X1, Y1;
     TS_Point p;
     
-    
     while(1) 
     {
          if(ft6206.touched())
          {
             p = ft6206.getPoint();
-            X1 = TFT.width()-p.x;
-            Y1 = TFT.height()-p.y;
+            X1 = tft.width()-p.x;
+            Y1 = tft.height()-p.y;
 
             //pc.printf("Touched at x=%3d y=%3d\n", p.x, p.y);
             //pc.printf("Touched actual at x=%3d y=%3d\n", X1, Y1);
@@ -768,24 +646,24 @@
             if ((X1 > 120) && (X1 < 200) && (Y1 > 35) && (Y1 <115))
             {
                 INFO("ATT selected\r\n");
-                return 1;
+                return AWS_HOST1;
             }
             
             if ((X1 > 120) && (X1 < 200) && (Y1 > 120) && (Y1 < 200))
             {
                 INFO ("GD selected\n");
-                return 2;
+                return AWS_HOST2;
             }
          
             //ft6206.clearPoint();
         }
         
     }
+    
+    /* unreachable code */
     pc.printf ("leaving menu\n"); 
     wait (10.0);
-    //TFT.set_font((unsigned char*) Arial12x12);
-    TFT.set_font((unsigned char*) Book_Antiqua19x19);
-    TFT.foreground(White);
+    tft.setTextColor(DEFAULT_TEXT_COLOR);
     return 0;    
 }
 
@@ -803,12 +681,14 @@
     
     // Set baud rate for PC Serial
     pc.baud(115200);
-    ShowINFO("AT&T AWS IoT Demo V.06");
+    ShowINFO("AT&T AWS IoT Demo V.08T");    //T for test AWS account config
     
-    TFT.drawBitmap(43, 10, att, 234, 96);
-    TFT.drawBitmap(0, 150, gd, 320, 56);  
+    // Draw logos
+    tft.writeRect2BPP(38, 33, 234, 96, att, att_palette);
+    tft.writeRect2BPP(0, 165, 320, 56, gd, gd_palette);  
+               
+    tft.setFont(MAIN_FONT);
 
-               
     int i;          
     IoT_Error_t rc = NONE_ERROR;  
     char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
@@ -840,7 +720,6 @@
     iccidHandler.pData = iccidName;
     iccidHandler.type = SHADOW_JSON_STRING;
     
-    
     INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
     
 
@@ -866,8 +745,7 @@
     wait(.5);
     SetLedColor(COLOR_OFF);
     
-    //TFTStuff();
-    
+
     // Initialize sensors
     INFO("Init sensors...");
     void hts221_init(void);
@@ -876,59 +754,58 @@
         WARN(RED "HTS221 NOT DETECTED!!\n\r");
     }
     
-    //TFTStuff();
     wait (2.0);
-    int iSelection = 0;
     string sMQTTHostName;
-    
-    iSelection = DoAWSThingMenu();
+    int iSelection = DoAWSThingMenu();
     
-    TFT.locate(0,0);
-    //TFT.set_font((unsigned char*) Arial12x12);
-    TFT.set_font((unsigned char*) Book_Antiqua19x19);
-    TFT.background(Black);
-    TFT.foreground(White);
-    TFT.cls();
+    tft.setCursor(0, 1);
+    tft.fillScreen(ILI9341_BLACK);    
+    tft.setTextColor(DEFAULT_TEXT_COLOR, ILI9341_BLACK);
+ 
     switch (iSelection)
     {
-        case 1:
+        case AWS_HOST1:
             aws_iot_mqtt_host = AWS_IOT_MQTT_HOST1;
             sMQTTHostName = "ATT";
-            TFT.printf ("ATT AWS Host Selected\n");
+            tft.printf ("ATT AWS Host Selected\n");
             break;
-        case 2:
+            
+        case AWS_HOST2:
             aws_iot_mqtt_host = AWS_IOT_MQTT_HOST2;
             sMQTTHostName = "GD";
-            TFT.printf ("GD AWS Host Selected\n");
+            tft.printf ("GD AWS Host Selected\n");
             break;
+            
         default:
             ShowINFO ("Unknown MQTT HOST\n");
-            break;
-            
+            break;           
     }
       
-    // Setup SW3 button to falling edge interrupt
-    
+    // Setup SW3 button to falling edge interrupt    
     INFO("Init interrupts...");
     Interrupt.fall(&sw3ButtonHandler);
-    
-    //TFTStuff();
           
     // Boot the Avnet Shield before any other operations
     INFO("Net Boot...");
-    TFT.printf ("Connecting to ATT LTE Network....\n");
+    tft.printf("Connecting to ATT LTE Network....\n");
     if (net_modem_boot() != 0)
     {
-        TFT.printf ("Unable to Connect to ATT LTE Network. Please try again by rebooting the device.\n");
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf("\nUnable to Connect to ATT Network.\n");
+        tft.moveCursor(0, 3);
+        tft.printf("Please try again by rebooting the\ndevice.\n");
         return 0;
     }
-    TFT.printf ("Connected to ATT LTE Network.\n");
-    wait (3.0);
+    tft.printf("Connected to ATT LTE Network.\n");
+    wait(3.0);
     
     
     if (GetSignalStrength(&dbm) != 0)
     {
-        TFT.printf ("Unable to Signal Strength. Please try again by rebooting the device.\n");
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf("\nUnable to read Signal Strength.\n");
+        tft.moveCursor(0, 3);
+        tft.printf("Please try again by rebooting the\ndevice.\n");
         return 0;
     }
     
@@ -941,7 +818,7 @@
     //==========================================================================
 
  restart1:   
-
+    count = 0;
           
     // Intialize MQTT/Cert parameters
     ShadowParameters_t sp = ShadowParametersDefault;
@@ -986,11 +863,15 @@
     ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *)AWS_IOT_CERTIFICATE, AWS_IOT_CERTIFICATE_LENGTH);
     if (ret != 0) {
         ERROR(" failed\n  !  mbedtls_x509_crt_parse IOT returned 1 -0x%x,  %d\n\n", -ret, AWS_IOT_CERTIFICATE_LENGTH);
-        TFT.printf ("Invalid Certificate. Please check the certificate and reboot the device.");
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf ("\nInvalid Certificate.\n");
+        tft.moveCursor(0, 3);
+        tft.printf("Please check the certificate and\nreboot the device.");
         return ret;
     }
     else
     {
+        //EBJ TODO read from card -- or is it already ?
         for (int i = 0; i < clicert.subject_raw.len; i++)
         {
             if (clicert.subject_raw.p[i] == 0x0C)
@@ -1010,28 +891,32 @@
         }
     }   
     
-    TFT.printf("Logging into %s AWS\n", sMQTTHostName); 
-    TFT.printf("CN: %s\n", cSubject);     
+    tft.moveCursor(0, 9);
+    tft.printf("ICCID: %s\n", iccidName);
+    tft.printf("Logging into %s AWS\n", sMQTTHostName); 
+    tft.printf("CN: %s\n", cSubject);     
     INFO("Initialize the MQTT client...");
     MQTTClient_t mqttClient;
     aws_iot_mqtt_init(&mqttClient);
 
     string sAWSError = "\nUnable to Log into AWS. Invalid certificate. Please make sure the certificates in the SIM card are valid and reboot the device.\n";
-    TFT.printf(".");
+    tft.printf(".");
     INFO("Shadow Init...");
     rc = aws_iot_shadow_init(&mqttClient);
     if (NONE_ERROR != rc) {
         ERROR("Shadow Init Error %d", rc);
-        TFT.printf(sAWSError.c_str()); 
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf(sAWSError.c_str()); 
         return rc;
     }
     
     INFO("Shadow Connect...");   
-    TFT.printf(".");
+    tft.printf(".");
     rc = aws_iot_shadow_connect(&mqttClient, &sp);
     if (NONE_ERROR != rc) {
         ERROR("Shadow Connection Error %d", rc);
-        TFT.printf(sAWSError.c_str()); 
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf(sAWSError.c_str()); 
         return rc;
     }
 
@@ -1041,7 +926,8 @@
     rc = mqttClient.setAutoReconnectStatus(true);
     if (NONE_ERROR != rc) {
         ERROR("Unable to set Auto Reconnect to true - %d", rc);
-        TFT.printf(sAWSError.c_str()); 
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf(sAWSError.c_str()); 
         return rc;
     }
     
@@ -1049,11 +935,12 @@
     //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true);
 
     INFO("Shadow Register Delta...");
-    TFT.printf(".");
+    tft.printf(".");
     rc = aws_iot_shadow_register_delta(&mqttClient, &ledController);
     if (NONE_ERROR != rc) {
         ERROR("Shadow Register Delta Error");
-        TFT.printf(sAWSError.c_str()); 
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.printf(sAWSError.c_str()); 
         return rc;
     }
     
@@ -1068,10 +955,12 @@
         if (NETWORK_ATTEMPTING_RECONNECT == rc) 
         {
             // If the client is attempting to reconnect we will skip the rest of the loop.
+            tft.setTextColor(ILI9341_YELLOW);
+            tft.setCursor(0, 195);
             ShowINFO("Attempting to reconnect...");
+            tft.setTextColor(DEFAULT_TEXT_COLOR);
             wait(1);
             bFirstTime = true;
-            //TFT.cls();
             continue;
         }
         
@@ -1123,9 +1012,9 @@
         {
             if (cUpdateStatus == 0xFF)
             {
-                TFT.cls();
-                TFT.locate(0,10);
-                ShowINFO ("Certifcate Update Detected.");   
+                tft.fillScreen(ILI9341_BLACK);
+                tft.setCursor(0, 10);
+                ShowINFO ("Certificate Update Detected.");   
                 ShowINFO("Disconnecting AWS");
                 rc = aws_iot_shadow_disconnect(&mqttClient);
                 if (NONE_ERROR != rc) 
@@ -1136,7 +1025,7 @@
                 
                 if (GetAllObjects() != 0)
                 {
-                    ShowINFO ("Read Certficate Error. Check hardware and Reboot the device..");
+                    ShowINFO ("Read Certificate Error. Check hardware and Reboot the device..");
                     return 0;   
                 }
                 
@@ -1146,14 +1035,10 @@
             }   
         }
         
-        
         // Print data
-        if (bFirstTime == true)
-        {
-            TFT.cls();   
-            bFirstTime = false;
-        }
-        printDatatoTFT(); 
+        printDatatoTFT(bFirstTime);
+        bFirstTime = false;
+         
         printData();
         INFO("*****************************************************************************************");
          
@@ -1164,7 +1049,9 @@
         if (count > 50)
         {
             INFO ("Max Upload reached. Please reset the device to start again");
-            TFT.printf ("Max upload reached. Please reset the device to continue.");
+            tft.setTextColor(ILI9341_YELLOW);
+            tft.setCursor(0, 195);
+            tft.printf("Max upload reached.\nPlease reset the device to continue.");
             return rc;
         }
             
@@ -1173,7 +1060,9 @@
     if (NONE_ERROR != rc) 
     {
         ERROR("An error occurred in the loop %d", rc);
-        TFT.printf("Fatal Error. Please reboot the device.\n");
+        tft.setTextColor(ILI9341_YELLOW);
+        tft.setCursor(0, 195);
+        tft.printf("Fatal Error. Please reboot the device.\n");
     }
 
     INFO("Disconnecting");