View difference between Paste ID: AnhDKhBq and 4Ec6d4xY
SHOW: | | - or go back to the newest paste.
1
// based on @martinbateman clock code
2
// https://t.co/1gRc56wNOE
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
#include <TimeLib.h>
13
14
#define TFT_BL          4  // Display backlight control pin
15
16
// Replace with your network credentials
17
const char* ssid     = "xxxxx";
18
const char* password = "yyyyy";
19
const char* mqtt_server = "simplesi.cloud";
20
21
int cheer_red = 0;
22
int cheer_green = 0;
23
int cheer_blue = 0;
24
unsigned int rgb565Decimal = 0x8410;
25
unsigned int newrgb565Decimal;
26
String colourString = "not set yet";
27
String newColourString;
28
29
String strData;
30
String topicStr;
31
32
33
34
WiFiUDP ntpUDP;
35
NTPClient timeClient(ntpUDP);
36
WiFiClient espClient;
37
PubSubClient client(espClient);
38
39
TFT_eSPI tft = TFT_eSPI();
40
SemaphoreHandle_t serialMutex = NULL;
41
void Task1 (void *pvParams);
42
void Task2 (void *pvParams);
43
44
void callback(char* topic, byte* payload, unsigned int length) {
45
46
  Serial.print("Message arrived in topic: ");
47
  Serial.println(topic);
48
  topicStr = topic;
49
50
  Serial.print("Message:");
51
  
52
  strData = "";
53
  for (int i = 0; i < length; i++) {
54
    Serial.print((char)payload[i]);
55
    strData += (char)payload[i];
56
  }
57
  
58
  Serial.println();
59
  Serial.println("-----------------------");
60
61
  if (topicStr.endsWith("cheerlights/rgb565Decimal")) {
62
   
63
    colourString = newColourString;
64
    rgb565Decimal = strData.toInt();
65
    Serial.println("*******");
66
  
67
    Serial.println(rgb565Decimal);
68
  }  
69
  if (topicStr.endsWith("cheerlights")) {
70
   
71
    newColourString = "Cheerlights:" + strData + "                         ";
72
    //sixteenBitHex = newSixteenBitHex;
73
    Serial.println(strData);
74
  }  
75
} // end callback
76
77
void reconnect() {
78
  // Loop until we're reconnected
79
  while (!client.connected()) {
80
    Serial.print("Attempting MQTT connection...");
81
    // Create a random client ID
82
    String clientId = "ESP8266Client-";
83
    clientId += String(random(0xffff), HEX);
84
    // Attempt to connect
85
    if (client.connect(clientId.c_str())) {
86
      Serial.println("connected");
87
      client.subscribe("cheerlights",1);
88
      client.subscribe("cheerlights/rgb565Decimal",1);
89
    } else {
90
      Serial.print("failed, rc=");
91
      Serial.print(client.state());
92
      Serial.println(" try again in 5 seconds");
93
      // Wait 5 seconds before retrying
94
      delay(5000);
95
    }
96
  }
97
}
98
99
void setup() {
100
  // put your setup code here, to run once:
101
  Serial.begin (115200);
102
  serialMutex = xSemaphoreCreateMutex ();
103
104
  if (TFT_BL > 0) {
105
    pinMode(TFT_BL, OUTPUT);
106
    digitalWrite(TFT_BL, HIGH);
107
  }
108
  
109
  tft.init();
110
  tft.setRotation(1);
111
  tft.fillScreen(TFT_BLACK);
112
113
  tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
114
115
  WiFi.begin(ssid, password);
116
  while (WiFi.status() != WL_CONNECTED) {
117
    delay(500);
118
    Serial.println (".");
119
  }
120
  Serial.println(WiFi.localIP());
121
122
  timeClient.begin();
123
  timeClient.setTimeOffset(3600);
124
  client.setServer(mqtt_server, 1883);
125
  client.setCallback(callback);
126
127
128
  xTaskCreate (updateNTP, "NTP Client", 4096, NULL, 2, NULL);
129
  xTaskCreate (updateScreen, "Screen", 4096, NULL, 1, NULL);
130
}
131
132
void loop() {
133
  if (!client.connected()) {
134
    reconnect();
135
  }
136
  client.loop();
137
}
138
139
void updateNTP (void *pvParameters) {
140
  (void) pvParameters;
141
142
  for (;;) {
143
    if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
144
      while(!timeClient.update()) {
145
        timeClient.forceUpdate();
146
      }
147
      setTime (timeClient.getEpochTime ());
148
      xSemaphoreGive (serialMutex);
149
    }
150
    vTaskDelay ((1000/portTICK_PERIOD_MS) * 60 * 60);  // update every hour
151
  }
152
}
153
154
void updateScreen (void *pvParameters) {
155
  (void) pvParameters;
156
157
  for (;;) {
158
    if (xSemaphoreTake (serialMutex, (TickType_t)10) == pdTRUE) {
159
      char timeString[25];
160
      char colourString2[25];
161
      colourString.toCharArray(colourString2,25);
162
163
      time_t t = now (); 
164
      sprintf (timeString, "%02i:%02i:%02i", hour (t), minute (t), second (t));
165
      xSemaphoreGive (serialMutex);
166
167
      tft.setTextColor(0x39C4, TFT_BLACK);
168
      tft.drawString("88:88:88",10,10,7);
169
      tft.setTextColor(rgb565Decimal, TFT_BLACK);
170
      tft.drawString (timeString, 10, 10, 7);
171
      tft.drawString (colourString2, 0, 80, 4);
172
    }
173
    vTaskDelay (1000/portTICK_PERIOD_MS);
174
  }
175
}