Advertisement
cymplecy

TFT-T-DISPLAY Cheerlights Clock

Jul 24th, 2019
1,874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // based on this paste from @martinbateman
  2. // https://pastebin.com/LNUvv1En
  3. #include <WiFi.h>
  4. #include <NTPClient.h>
  5. #include <WiFiUdp.h>
  6. #include <PubSubClient.h>
  7.  
  8.  
  9. #include <TFT_eSPI.h>
  10. #include <SPI.h>
  11.  
  12. #define TFT_BL          4  // Dispaly backlight control pin
  13.  
  14.  
  15. // Replace with your network credentials
  16. const char* ssid     = "xxxxxx";
  17. const char* password = "yyyyyy";
  18. const char* mqtt_server = "simplesi.cloud";
  19.  
  20. int cheer_red = 0;
  21. int cheer_green = 0;
  22. int cheer_blue = 0;
  23. unsigned int rgb565Decimal = 0x8410;
  24. unsigned int newrgb565Decimal;
  25. String colourString = "not set yet";
  26. String newColourString;
  27.  
  28. String strData;
  29. String topicStr;
  30.  
  31.  
  32.  
  33. WiFiUDP ntpUDP;
  34. NTPClient timeClient(ntpUDP);
  35. WiFiClient espClient;
  36. PubSubClient client(espClient);
  37.  
  38. TFT_eSPI tft = TFT_eSPI();
  39.  
  40. void callback(char* topic, byte* payload, unsigned int length) {
  41.  
  42.   Serial.print("Message arrived in topic: ");
  43.   Serial.println(topic);
  44.   topicStr = topic;
  45.  
  46.   Serial.print("Message:");
  47.  
  48.   strData = "";
  49.   for (int i = 0; i < length; i++) {
  50.     Serial.print((char)payload[i]);
  51.     strData += (char)payload[i];
  52.   }
  53.  
  54.   Serial.println();
  55.   Serial.println("-----------------------");
  56.  
  57.   if (topicStr.endsWith("cheerlights/rgb565Decimal")) {
  58.    
  59.     colourString = newColourString;
  60.     rgb565Decimal = strData.toInt();
  61.     Serial.println("*******");
  62.  
  63.     Serial.println(rgb565Decimal);
  64.   }  
  65.   if (topicStr.endsWith("cheerlights")) {
  66.    
  67.     newColourString = "Cheerlights:" + strData + "                         ";
  68.     //sixteenBitHex = newSixteenBitHex;
  69.     Serial.println(strData);
  70.   }  
  71. } // end callback
  72.  
  73. void reconnect() {
  74.   // Loop until we're reconnected
  75.   while (!client.connected()) {
  76.     Serial.print("Attempting MQTT connection...");
  77.     // Create a random client ID
  78.     String clientId = "ESP8266Client-";
  79.     clientId += String(random(0xffff), HEX);
  80.     // Attempt to connect
  81.     if (client.connect(clientId.c_str())) {
  82.       Serial.println("connected");
  83.       client.subscribe("cheerlights",1);
  84.       client.subscribe("cheerlights/rgb565Decimal",1);
  85.     } else {
  86.       Serial.print("failed, rc=");
  87.       Serial.print(client.state());
  88.       Serial.println(" try again in 5 seconds");
  89.       // Wait 5 seconds before retrying
  90.       delay(5000);
  91.     }
  92.   }
  93. }
  94.  
  95. void setup() {
  96.   Serial.begin(115200);
  97.  
  98.   if (TFT_BL > 0) {
  99.     pinMode(TFT_BL, OUTPUT);
  100.     digitalWrite(TFT_BL, HIGH);
  101.   }
  102.  
  103.   tft.init();
  104.   tft.setRotation(1);
  105.   tft.fillScreen(TFT_BLACK);
  106.  
  107.   tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
  108.  
  109.   WiFi.begin(ssid, password);
  110.   while (WiFi.status() != WL_CONNECTED) {
  111.     delay(500);
  112.   }
  113.  
  114.   timeClient.begin();
  115.   timeClient.setTimeOffset(3600);
  116.   client.setServer(mqtt_server, 1883);
  117.   client.setCallback(callback);
  118. }
  119. void loop() {
  120.   if (!client.connected()) {
  121.     reconnect();
  122.   }
  123.   client.loop();
  124.    
  125.   while(!timeClient.update()) {
  126.     timeClient.forceUpdate();
  127.   }
  128.   char timeString[25];
  129.   char colourString2[25];
  130.   sprintf (timeString, "%02i:%02i:%02i", timeClient.getHours (), timeClient.getMinutes (), timeClient.getSeconds ());
  131.   colourString.toCharArray(colourString2,25);
  132.   //sprintf (colourString2, "%d", sixteenBitHex);
  133.  
  134.  
  135.   tft.setTextColor(0x39C4, TFT_BLACK);  // Leave a 7 segment ghost image, comment out next line!
  136.   tft.drawString("88:88:88",10,10,7); // Overwrite the text to clear it
  137.   tft.setTextColor(rgb565Decimal, TFT_BLACK);
  138.   tft.drawString (timeString, 10, 10, 7);
  139.   tft.drawString (colourString2, 0, 80, 4);
  140.  
  141.   delay(1000);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement