//MQTT
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
//Display
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
#define WiFi_name "Moja WIFI" //WiFi
#define WiFi_password "Bohtamiluje" //WiFi
#define MQTT_server "broker.hivemq.com" //MQTT
WiFiClient espClient;
PubSubClient client(espClient);
#define TFT_CS 15 // Chip select
#define TFT_RST 4 // Reset
#define TFT_DC 2 // Data/Command
#define TFT_MISO 12 // MISO (Master In Slave Out)
#define TFT_MOSI 13 // MOSI (Master Out Slave In)
#define TFT_SCK 14 // SCK (Clock)
// Nastavenie pinov pre dotykový modul
#define TOUCH_CS 5 // Chip select pre dotykový modul
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
// Inicializácia TFT displeja
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Inicializácia dotykového modulu
XPT2046_Touchscreen ts(TOUCH_CS);
int sensitiveYStart = SCREEN_HEIGHT - 75;
int sensitiveYEnd = SCREEN_HEIGHT;
int long buducnost = 0;
byte pages = 0;
byte firstState = HIGH;
void setup() {
SPI.setFrequency(20000000);
WiFi.mode(WIFI_STA);
Serial.begin(9600);
client.setServer(MQTT_server, 1883);
client.setCallback(callback);
Serial.println("Inicializacia displeja...");
tft.begin();
tft.fillScreen(ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
if (!ts.begin()) {
Serial.println("Chyba inicializacie dotykoveho panela!");
while (1);
}
WiFi.begin(WiFi_name, WiFi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.print("WIFI uspesne pripojena: ");
Serial.println(WiFi.localIP());
while (!client.connected()) {
Serial.println("Pripájam sa na MQTT...");
tft.setCursor(0, 0);
tft.print("Pripajam sa na MQTT");
if (client.connect("HiveMQClient")) {
Serial.println("Pripojené na MQTT!");
tft.fillRect(0, 0, SCREEN_WIDTH, sensitiveYStart, ILI9341_WHITE);
tft.setCursor(0, 0);
tft.print("Pripojene na MQTT!");
client.subscribe("METHER"); // Subscribe na topic
} else {
Serial.print("Chyba pripojenia: ");
Serial.println(client.state());
}
}
tft.fillRect(0, sensitiveYStart, SCREEN_WIDTH, sensitiveYEnd, ILI9341_DARKGREY);
tft.setCursor(50, sensitiveYStart+30);
tft.print("Dalsia str.");
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
int16_t x = tft.width() - map(p.y, 200, 3900, 0, tft.width()); // Prepočítanie osí
int16_t y = map(p.x, 200, 3900, 0, tft.height());
Serial.print("Touch detected at X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.println(y);
if (y >= sensitiveYStart && y <= sensitiveYEnd) {
pages++; // Pripočítanie čísla
Serial.print("pages: ");
Serial.println(pages);
if(pages > 1){
pages = 0;
}
// Aktualizácia textu na displeji
tft.fillRect(0, 0, SCREEN_WIDTH, sensitiveYStart, ILI9341_WHITE); // Vymaže predchádzajúci text
}
}
if (!client.connected()) {
while (!client.connected()) {
Serial.println("Pripajanie na MQTT...");
tft.fillRect(0, 0, SCREEN_WIDTH, sensitiveYStart, ILI9341_WHITE);
tft.setCursor(0, 0);
tft.print("Pripajam na MQTT");
if (client.connect("HiveMQReceiver")) {
Serial.println("Spojenie obnovene!");
tft.fillRect(0, 0, SCREEN_WIDTH, sensitiveYStart, ILI9341_WHITE);
tft.setCursor(0, 0);
tft.print("Spojenie obnovene!");
client.subscribe("METHER");
tft.fillRect(0, 0, SCREEN_WIDTH, sensitiveYStart, ILI9341_WHITE);
}
}
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
char message[length + 1];
memcpy(message, payload, length);
message[length] = '\0';
/*Serial.print("Správa z topicu ");
Serial.print(topic);
Serial.print(": ");
Serial.println(message);*/
// Parsovanie JSON správy
StaticJsonDocument<200> jsonDoc;
DeserializationError error = deserializeJson(jsonDoc, message);
if (error) {
Serial.print("Chyba parsovania JSON: ");
Serial.println(error.c_str());
return;
}
// Čítanie údajov z JSON
float currentTime = jsonDoc["currentTime"];
float flightTime = jsonDoc["flightTime"];
float altitude = jsonDoc["altitude"];
float temperature = jsonDoc["temperature"];
float pressure = jsonDoc["pressure"];
float x = jsonDoc["x"];
float y = jsonDoc["y"];
float z = jsonDoc["z"];
float rotation = jsonDoc["rotation"];
// Výpis údajov na sériový monitor
Serial.println(message);
if(pages == 0){
tft.fillRect(60, 10, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 10);
tft.setTextColor(ILI9341_BLACK);
tft.print("Cas: ");
tft.print(currentTime);
tft.fillRect(110, 50, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 50);
tft.setTextColor(ILI9341_BLACK);
tft.print("Zac. Cas: ");
tft.print(flightTime);
tft.fillRect(80, 90, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 90);
tft.setTextColor(ILI9341_BLACK);
tft.print("Vyska: ");
tft.print(altitude);
tft.fillRect(100, 130, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 130);
tft.setTextColor(ILI9341_BLACK);
tft.print("Teplota: ");
tft.print(temperature);
tft.fillRect(70, 170, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 170);
tft.setTextColor(ILI9341_BLACK);
tft.print("Tlak: ");
tft.print(pressure);
tft.fillRect(40, 210, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 210);
tft.setTextColor(ILI9341_BLACK);
tft.print("X: ");
tft.print(x);
}
else if(pages == 1){
tft.fillRect(40, 10, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 10);
tft.print("Y: ");
tft.print(y);
tft.fillRect(40, 50, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 50);
tft.print("Z: ");
tft.print(z);
tft.fillRect(100, 90, SCREEN_WIDTH, 16, ILI9341_WHITE);
tft.setCursor(10, 90);
tft.print("Rotacia: ");
tft.print(rotation);
}
}